May 11th, 2008

List Comprehensions in python

veloso had an interesting python puzzle for me in python. How could you take a tuple in python, such as (1,2,3) and transform it into the tuple (1,1,2,2,3,3)? For the examples below assume orig = (1,2,3)

His original stab as presented to me was follows:
    newTuple = tuple(chain(*(repeat(x, 2) for x in orig)))

I was able to modify as follows:
    newTuple = tuple(chain(*((x, x) for x in orig)))

Which is slightly cleaner, but his coworker had the best solution:
    newTuple = tuple(i for x in orig for i in (x,x))

I am not a big fan of list comprehensions (I think they make the code harder to read), so I came up with the follow:
   def repeatElements(list, count):
      for x in list:
         for y in range(count):
            yield x

   newTuple = tuple(repeatElements(orig, 2))

This has the added benefit of being able to repeat any number of times, but of course requires more lines and a generator function.

Parallel Computing

At work the other day Bucky, Kilroy, and I were discussing Parallel computing. I made the comment that all parallel computing was multi-threaded, but not all multi-threaded programs were parallel computing. I was like setting of a bomb in an ammo store. We got in this long discussion about what is and what is not a thread. I think I am still right, but Bucky and Kilroy felt I was abusing the definition of thread and threading. I did get Bucky to admit that at the processor level it was multi-threaded, but he still thought it was nebulous.



I went to a recital today hosted by chikuru and raaga123. I had to take Titan with me, since it was mother's day and I had promised dragado a day off. I went first, because I was not sure Titan would be quiet for much longer. I played for songs on the 2-in-1 Jungle Jamboree Tiger all while holding the boy.

My set list was:
  • Mary Had a Little Lamb
  • Twinkle Twinkle Little Star
  • Are you Sleeping?
  • Row Your Boat
  • The first few bars of In-A-Gadda-Da-Vida

I think it went well, though I did fumble some of the keys. Next I got to hear chikuru play a nice George Winston song which ended in C# minor, followed by three then four violinists. At that point Titan, though being good in general, was getting too loud to be at a recital and I had to head for home. It is too bad though, because I really wanted to hear more people play. Maybe next time.