Computer Chocolate Code-off Rules: Get into groups of 4 No computers or phones (or any other technology) Complete as many problems as possible within the allotted time You may ask me 4 yes/no questions (and they cannot be “Is this the answer?”) _________________________________________________________________ Problem 1: prints y y times (Answer: ‘333’) def f1(x,y): if (x == y): return(str(y)) else: return (str(y) + f1(x+1,y)) print(f1(1,3)) _________________________________________________________________ Problem 2: Product of odd numbers (Answer: 15) def f5 (a): if (a <= 0): return(1) elif ((a%2) ==0): return (f5(a - 1)) else: return (a*f5(a-1)) print(f5(6)) _________________________________________________________________ Problem 3: Counts numbers evenly divisible by 3 (Answer: 4) def f7(a,b): if (b <= 0): return(a) elif((b%3)== 0): return(f7(a+1,b-1)) else: return(f7(a,b-1)) print(f7(0,13)) _________________________________________________________________ Problem 4: (Answer: 42 (had to be there somewhere)) def mk(x,y): if (y == 0): return("") else: return(str(x**y) + mk(x,y-1)) print(mk(2,2)) _________________________________________________________________ 1 Problem 5: prints numbers in order (a string) (Answer: ‘12345’) def f6(x): if (x <= 1): return str(x) else: return(f6(x-1) + str(x) ) print(f6(5)) _________________________________________________________________ Problem 6: Sums the numbers in a string (Answer: 13) def f(x,y,z): if y == 1: return z + x else: return(f(x%y, y//10, z+x//y)) print(f(1354,1000,0)) _________________________________________________________________ Problem 7: (Answer: “byte”) def k(x): if (x > 8): return ('t') elif (x > 6): return('i') elif (x > 5): return('e') elif (x > 3): return('b') elif (x > 2): return('s') elif (x > 0): return('y') else: return('c') def k2(x): if (x == 0): return "" else: return(k2(x//10) + k(x%10)) print(k2(4196)) _________________________________________________________________ Problem 8: (Makes a number palindrome (Answer: 45654)) def fg(x,y,q,r): if (x == 0): return "" else: if (x == y): return(str(r+x)+fg(x+q,y,-1,r)) else: return(str(r+x)+fg(x+q,y,q,r)) print(fg(1,2,1,3)) 2 _________________________________________________________________ Problem 9: Determines whether a number is a perfect number or not (Answer: True) def m(x,y,z): if (x ==y): return(z==y) else: if y%x == 0: return(m(x+1,y,z+x)) else: return(m(x+1,y,z)) print(m(1,28,0)) _________________________________________________________________ Problem 10: (Replaces everything that’s not a multiple of 3 with dots (Answer: ..3..6.) def h(x): if (x == 0): return("") else: if (x % 3) == 0: return(h(x - 1) + str(x)) else: return(h(x-1) + ".") print(h(7)) _________________________________________________________________ Problem 11: (Finds the GCD Answer: 3 ) def f4 (a, b): if (b == 0): return (a) if (a < b): return f4 (b, a) else: return f4 (b, a%b) print(f4(27,12)) _________________________________________________________________ Problem 12: Count the number of prime numbers between 2 and x (Answer: 5) def f12(x,y): if (y>(x//2)): return(True) elif (x%y==0): return(False) else: return (f12(x,y+1)) def g12(x,y): if (x >y): return(0) elif f12(x,2): return (1 + g12(x+1,y)) else: return(g12(x+1,y)) print(g12(2,11)) 3 _________________________________________________________________ Problem 13: Converts binary to decimal (Answer: 13) def p2(y): if y == 0: return 1 else: return (p2(y-1) * 2) def p3(x,y): if (x == 0): return (0) else: if (x%2 == 1): return(p2(y) + p3(x//10,y+1)) else: return(p3(x//10,y+1)) print(p3(1101,0)) _________________________________________________________________ Problem 14: (that takes an integer and returns the reverse of that integer (as an integer) Answer: 7423 def f14(x,y): if (x//y)==0: return(y//10) else: return(f14(x,y*10)) def g14(x,y): if (f14(x,1) == 1): return(int(str(x) + y)) else: return g14(x % f14(x,1), str(x // f14(x,1) )+y) print(g14(3247,"")) _____________________________________________________________________________________ Problem 15: Answer: 3332244441 def fn1(x,y): if (x == y): return("") else: return (str(y) + fn1(x+1,y)) def fn2(x): if (x<9): return(fn1(0,x)) else: return(fn2(x//10) + fn1(0,x%10)) print(fn2(3241)) 4