def f0(x): return ~(~x - x) def f1(x, y): return (x ^ y) < 0 def f2(x, y): return y ^ ((x ^ y) & -(x < y)) def f3(x): return x and not (x & (x-1)) def f4(x): (result, y) = (0,x) while (y>0): (y,result)=(y>>1,result+(y&1)) return (x>0) and (((result-2) ^ result) < 0) def f5(x): return (x>0) and (bin(x).count("1") == 1) def f6(x,y): x ^= y y ^= x x ^= y return (x,y)
def h1(x, y): # interesting note: this is basically the ancient Egyptian multiplication algorithm! # And it only uses addition to multiply! Cool! assert((x>0) and (y>0)) (hi, lo) = (max(x,y), min(x,y)) (product, addend) = (0, hi) while (lo > 0): if (lo&1): product += addend addend += addend lo = lo >> 1 return product def h2(x, y): assert((x>0) and (y>0)) result = 0 for m in xrange(x): for n in xrange(y): result += 1 return result def h3(x, y): assert((x>0) and (y>0)) (hi, lo) = (max(x,y), min(x,y)) # hint: (x-m)(y-m) = xy - m(x+y) + m**2 result = 0 while (lo > 10): result += 10*(lo+hi) - 100 (lo, hi) = (lo-10, hi-10) while (lo > 0): (lo, result) = (lo-1, result+hi) return result def h4(x, y): assert((x>0) and (y>0)) (hi, lo) = (max(x,y), min(x,y)) product = 0 for i in xrange(lo.bit_length()): if (lo & (1<<i)): product += hi<<i return product
def testDrawCircularPattern(): print "Testing drawCircularPattern. See drawing to confirm this passed." root = Tk() canvas = Canvas(root, width=550, height=400) canvas.pack() # some simple examples drawCircularPattern(canvas, 50, 25, 250, 125, 3, "red") drawCircularPattern(canvas, 50, 150, 250, 250, 4, "black") drawCircularPattern(canvas, 50, 275, 250, 375, 5, "green") # and now a more interesting one drawCircularPattern(canvas, 300, 50, 500, 350, 20, "blue") root.mainloop()
And when you run that function, you should see this
window:
text = """\ We hold these truths to be self-evident: that all men are created equal; that they are endowed by their Creator with certain unalienable rights; that among these are life, liberty, and the pursuit of happiness."""
WIth this text, a call to justifyText(text, 30) would return this text left- and right-justified with 30 characters per line, as such:
"""\ We hold these truths to be self-evident: that all men are created equal; that they are endowed by their Creator with certain unalienable rights; that among these are life, liberty, and the pursuit of happiness."""Here are some specifics to keep in mind:
carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem - carpe diem