CMU Coding Bootcamp

feat: oct14 level1

thecoded.prof 84301b11 8edd123b

verified
+23
python/oct14/level1/maxDigit.py
··· 1 + def maxDigit(n: int) -> int: 2 + n = abs(n) 3 + d = n % 10 4 + r = n // 10 5 + next = maxDigit(r) if r > 0 else 0 6 + return d if d > next else next 7 + 8 + def testMaxDigit(): 9 + print('Testing maxDigit()...', end='') 10 + assert(maxDigit(0) == 0) 11 + assert(maxDigit(1) == 1) 12 + assert(maxDigit(-1) == 1) 13 + assert(maxDigit(12321) == 3) 14 + assert(maxDigit(-12321) == 3) 15 + assert(maxDigit(102030405060708090) == 9) 16 + assert(maxDigit(22222222222) == 2) 17 + assert(maxDigit(22222322222) == 3) 18 + print('Passed!') 19 + 20 + def main(): 21 + testMaxDigit() 22 + 23 + main()
+18
python/oct14/level1/oddCount.py
··· 1 + def oddCount(L: list[int]) -> int: 2 + return len([n for n in L if n % 2]) 3 + 4 + def testOddCount(): 5 + print('Testing oddCount()...', end='') 6 + assert(oddCount([]) == 0) 7 + assert(oddCount([1]) == 1) 8 + assert(oddCount([2]) == 0) 9 + assert(oddCount([1,2,3,4,5,4,3]) == 4) 10 + assert(oddCount([1,2,3,4,5,4,3,2]) == 4) 11 + assert(oddCount([2,4,6,8,10,12,14]) == 0) 12 + assert(oddCount([1,1,1,1,1]) == 5) 13 + print('Passed!') 14 + 15 + def main(): 16 + testOddCount() 17 + 18 + main()
+24
python/oct14/level1/oddSum.py
··· 1 + def oddSum(L: list[int], sum: int = 0) -> int: 2 + if len(L) == 0: 3 + return sum 4 + val = L[0] 5 + rest = L[1:] 6 + if val % 2 == 1: 7 + sum += val 8 + return oddSum(rest, sum) 9 + 10 + def testOddSum(): 11 + print('Testing oddSum()...', end='') 12 + assert(oddSum([]) == 0) 13 + assert(oddSum([1]) == 1) 14 + assert(oddSum([2]) == 0) 15 + assert(oddSum([1,2,3,4,5,4,3]) == 12) # 1+3+5+3 16 + assert(oddSum([1,2,3,4,5,4,3,2]) == 12) # 1+3+5+3 17 + assert(oddSum([2,4,6,8,10,12,14]) == 0) 18 + assert(oddSum([1,1,1,1,1]) == 5) # 1+1+1+1+1 19 + print('Passed!') 20 + 21 + def main(): 22 + testOddSum() 23 + 24 + main()
+17
python/oct14/level1/reversedEvens.py
··· 1 + def reversedEvens(L: list[int]) -> list[int]: 2 + return list(reversed([n for n in L if not (n % 2)])) 3 + 4 + def testReversedEvens(): 5 + print('Testing reversedEvens()...', end='') 6 + assert(reversedEvens([]) == []) 7 + assert(reversedEvens([1]) == []) 8 + assert(reversedEvens([1,2]) == [2]) 9 + assert(reversedEvens([1,2,3]) == [2]) 10 + assert(reversedEvens([1,2,3,4]) == [4,2]) 11 + assert(reversedEvens([1,2,3,4,5,6,5,4,5,6,7]) == [6,4,6,4,2]) 12 + print('Passed!') 13 + 14 + def main(): 15 + testReversedEvens() 16 + 17 + main()
+29
python/oct14/level1/twoHeadsInFiveFlips.py
··· 1 + from random import choice 2 + 3 + def flipCoin() -> str: 4 + return choice("HT") 5 + 6 + def estimateProbability(trials: int) -> float: 7 + heads = 0 8 + for _ in range(trials): 9 + tosses: list[str] = [] 10 + for _ in range(5): 11 + tosses.append(flipCoin()) 12 + if len([h for h in tosses if h == "H"]) == 2: 13 + heads += 1 14 + else: 15 + return heads / trials 16 + 17 + 18 + def testTwoHeadsInFiveFlipsProblem(): 19 + print('Testing the twoHeadsInFiveFlips Problem...', end='') 20 + trials = 10**5 21 + p = estimateProbability(trials) 22 + print(p) 23 + assert(0.30 <= p <= 0.32) # actual answer is about 0.313 24 + print('Passed!') 25 + 26 + def main(): 27 + testTwoHeadsInFiveFlipsProblem() 28 + 29 + main()