from re import match def isBinaryRepresentation(s): """Decide whether a string contains the binary rep. of a number >1. Leading zeros are allowed.""" #match puts us at the start of the string #ensures we end at the end of the string (not fooled by test case 4) return match("0*1[01]+\$", s) def testIsBinaryRepresentation(): assert not isBinaryRepresentation("00") assert not isBinaryRepresentation("1") assert not isBinaryRepresentation("01") assert not isBinaryRepresentation("1010 is the binary rep. of a number.") assert not isBinaryRepresentation("this is CS: 1011") assert isBinaryRepresentation("10") assert isBinaryRepresentation("0010") assert isBinaryRepresentation("11") assert isBinaryRepresentation("10010110") testIsBinaryRepresentation() from Tkinter import * from random import randint, randrange, choice def randomColor(): """Return a random color formatted in Tkinter's #XXXXXX style.""" #eg orange is "#ff007f" (full red and half green) return "#%2x%2x%2x"%tuple(randint(0,255) for i in xrange(3)) def randomCoords(width, height): a=randrange(width/4) b=randrange(height/5) left=-width/10 top=randrange(-height/10, height) return left, top, left+a, top+b def timerFired(canvas): width, height=canvas.winfo_width(), canvas.winfo_height() canvas.delete(canvas.data.bg) #ugle hack to get around the issue that the winfo_ functions get the #dimensions wrong the first time we call them flag=canvas.data.bg==0 canvas.move(ALL, 2, 1) #apologies--Steven's fault canvas.data.bg=canvas.create_rectangle(0, 0, width, height, width=0, fill=randomColor()) canvas.tag_lower(canvas.data.bg, ALL) #canvas.after(delay, function, arguments...) #after delay milliseconds, call function with arguments canvas.after(200, timerFired, canvas) if flag: return #the color I use for highliting lambdas periwinkle="#8833ff" canvas.create_oval(randomCoords(width, height), outline="white", fill=choice(("red", "blue", periwinkle, "goldenrod"))) def run(): root=Tk() width=400 height=400 backgroundColor="red" canvas=Canvas(width=width, height=height, bg=backgroundColor) canvas.pack() canvas.data=type("", (), {'bg':0}) timerFired(canvas) root.mainloop() run()