Saturday, November 7, 2009

List Comprehension

I am enrolled in an Artificial Intelligence class this quarter which consumes about 20 hours per weekend every other weekend for the fortnightly programming assignment. (This is just how long it takes to complete the programming assignments, and doesn't include time spent preparing for the weekly quizzes, doing the required reading, completing the regular homework assignments, and studying for tests. Not that I'm complaining. I signed up for this, after all. :)) The programming part isn't usually too bad -- maybe eight hours of work -- but then I have to write a paper about the assignment. The writing usually takes quite a bit longer. Anyway, this is one of those weekends, but I'm taking a short break to talk about my current assignment and this sweet use of list comprehension I just whipped up.

We are studying constraint satisfaction problems and our latest assignment is to solve the game of Sudoku using some specific heuristics. We can use any programming language we choose, so I have used Python for all of the assignments so far. Python is so awesome that sometimes I feel like I'm cheating. For example, for this assignment, in order to implement one of the heuristics, I had to get a list of the coordinates for each block in the puzzle.

The "board" is basically a 2-dimensional array, where the top-left cell is at (0, 0) and the bottom-right cell is at (8, 8). Recall that the board is broken into 9 blocks of 9 cells each. The first block consists of the first 3 cells in each of the first 3 columns. What I was trying to get was a list of the coordinates for each cell in each of the blocks. The first block would look like this:

[(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)]


The standard way to do this kind of thing, and the approach I would have taken if I were writing this in Java or C++, is to put together a couple of for loops and iteratively populate a big array. The whole thing might take 3-5 lines of code. But in Python, we can take advantage of some really cool features, like list comprehension, that can save the programmer a lot of time.

So here it is, the coordinates of each block in a Sudoku puzzle, in one line of Python:


>>> x = [[(i + a * 3, j + b * 3) for i in range(3) \
for j in range(3)] for a in range(3) \
for b in range(3)]
>>> x
[[(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)],
[(0, 3), (0, 4), (0, 5),
(1, 3), (1, 4), (1, 5),
(2, 3), (2, 4), (2, 5)],
[(0, 6), (0, 7), (0, 8),
(1, 6), (1, 7), (1, 8),
(2, 6), (2, 7), (2, 8)],
[(3, 0), (3, 1), (3, 2),
(4, 0), (4, 1), (4, 2),
(5, 0), (5, 1), (5, 2)],
[(3, 3), (3, 4), (3, 5),
(4, 3), (4, 4), (4, 5),
(5, 3), (5, 4), (5, 5)],
[(3, 6), (3, 7), (3, 8),
(4, 6), (4, 7), (4, 8),
(5, 6), (5, 7), (5, 8)],
[(6, 0), (6, 1), (6, 2),
(7, 0), (7, 1), (7, 2),
(8, 0), (8, 1), (8, 2)],
[(6, 3), (6, 4), (6, 5),
(7, 3), (7, 4), (7, 5),
(8, 3), (8, 4), (8, 5)],
[(6, 6), (6, 7), (6, 8),
(7, 6), (7, 7), (7, 8),
(8, 6), (8, 7), (8, 8)]]
>>> len(x)
9
>>> x[0]
[(0, 0), (0, 1), (0, 2),
(1, 0), (1, 1), (1, 2),
(2, 0), (2, 1), (2, 2)]
>>> x[1]
[(0, 3), (0, 4), (0, 5),
(1, 3), (1, 4), (1, 5),
(2, 3), (2, 4), (2, 5)]
>>> x[8]
[(6, 6), (6, 7), (6, 8),
(7, 6), (7, 7), (7, 8),
(8, 6), (8, 7), (8, 8)]

Tuesday, October 27, 2009

Go Blazers!

This is SO EXCITING! We are watching the Blazers' first game of the season and LOVING it. Love this team. Rian even made a special sign.

Tuesday, September 29, 2009

Ethan loves toy cars and trains. More than anything else right now. He's been known to carry one around ALL DAY. I've even put him to bed and he'll still be hanging onto one.






This Thomas the Train bedtime shirt is Ethan's favorite shirt. He LOVES it! He even carried that around one day. (I got this thing for free from a clothing donation place where my mom has been volunteering. Bonus!!)







He wore it to bed the other night and I take it off to change him the next day. He started throwing the biggest fit. As soon as he's dressed he takes off for the bedroom and comes back out carrying the Thomas shirt. He's still wailing and saying "Dis? Dis? Choo-choo?" It was pretty sad so we changed his shirt. He immediately stopped crying, laughed (all the while saying "choo-choo") and lifted his shirt up so that he could kiss Thomas.

After all this excitement I gave him a snack. I put a handful of goldfish crackers on the table and turned away to get his water. I come back to see him stuff all of them in his mouth. He didn't chew, just shoved them all in. He turns to look at me and immediately starts signing "more".

The boys and I have been going for walks a lot lately. We found this awesome path that is away from the road so Ethan can get out of the stroller and walk (try to run) with Rian and I. This is what happened during our walk on Sunday....

Ethan got out, Rian got in. Ethan then started pulling Rian in the stroller.....

Then changed his mind and found that pushing was much more efficient.
(He's a little nutty!)

Saturday, July 18, 2009

My Boat

I've never used craigslist to sell anything before, but I realized today that I need to get rid of my row boat. We are moving next weekend and we need to get rid of all this stuff. So I decided to put the boat on craigslist this week and if it wasn't gone in a week, I'd take it to the dump. It's been a good boat for me, but it's just not practical to live in an apartment with a boat (or so I hear).

So I took the ol' boat out of the garage, took a few pictures of it, and even found a couple pictures from years ago when I took it to the beach. I adjusted the photos to improve contrast, add a vignette, etc. I wrote a short little blurb about the boat and posted it all to craigslist.

The result? I received the first call 10 minutes after submitting the post. I ended up getting 8 calls (two after I took this screenshot) about the boat and having someone show up on my doorstep to haul it away in a truck. All of this happened within 45 minutes of making my first craigslist post.

Thanks, craigslist!

Thursday, July 9, 2009

Lunch



He came inside long enough to get his plate and then rushed back to his post.



When I grow up...

We have a couple guys building a Tuff Shed in our backyard right now. When they first started working Rian told me, "Hey mommy! I know it. I know what to be when I get big. A Tuff Shed guy! Yay! I know it." He's out there right now in his folding chair watching them work.




I just heard him yell, "Good job, guys!" Man, I love this kid.



Tuesday, June 16, 2009

8.2, baby!

I should be folding laundry. I have this awful habit of dumping clean, dry loads of laundry on our living room couch with every intention of folding it right away but doing something else instead. It sits there taking up our couch, sometimes for a couple days, until I realize that it won't fold itself or we run out of things to wear. Bah. I don't really mind doing laundry but today I'm staring at the mountain of it on the couch and feeling inspired to do this instead. Crazy....

Rian's sixth birthday was on the fourth. I can't believe we have a six year old! He's growing up so fast. He's such incredible, fun, caring boy. I love you, Rian!! 

Monday was my birthday. The boys and I spent a lazy day at home playing outside. We broke out the sidewalk chalk, ran in the sprinklers and played a ton of basketball. Eric came home from work early so that I could go for a run. I ran 8.2 miles and finished in an hour and twenty minutes! (Turned 28 so ran 8.2 miles.... maybe a little silly) It felt awesome. It's the farthest I've run since the half-marathon in October. (Ok, that's a little pathetic but I've been busy.... putting off laundry and stuff) I was totally expecting to be so sore the next day but it wasn't bad at all. 
 
Alright, I guess those clothes aren't going to fold themselves and if I don't do it before Ethan gets up from his nap I'm in trouble.