Friday Programming Quiz [modified]
-
Matt Gerrans wrote:
Isn't this a programming question
This is a programming quiz. The purpose is to have fun. The problems presented here are too trivial.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
Sorry, I intended that to be a (good natured) parody of the Lounge posting police, but that apparently didn't work very well. :-O I do like the Programming Quiz and encourage you to continue! In fact, I'll provide a Python[^] (as usual) answer:
def ExecuteActions( actions, onAllActionsComplete ):
def doActions(actions):
if actions:
actions[0]( lambda: doActions(actions[1:]) )
else:
onAllActionsComplete()
doActions(actions)(My first answer worked, but was a lot more clunky; this is what occurred to me after pondering it a bit more.) Also, here is the code to test drive it:
def onAllActionsComplete():
print 'onAllActionsComplete!'actions = []
for l in 'abc':
exec( 'def %s(o):\n\tprint "%s!"\n\to()\nactions.append(%s)' % ((l,)*3) )ExecuteActions( actions, onAllActionsComplete )
(This could be made a little more terse by using lambda more, but that wouldn't necessarily be better.) By the way, thanks to IronPython[^], this would be an easy way to do it in .NET.
Matt Gerrans