CMU Coding Bootcamp

feat: oct 3 level 3

thecoded.prof fd72f669 b98b09c3

verified
Changed files
+141
python
+44
python/oct3/level3/bowlingScore.py
··· 1 + from typing import List 2 + 3 + 4 + def bowlingScore(frames: List[int]) -> int: 5 + """Calculate the total score of a bowling game.""" 6 + scores: List[int] = [] 7 + next_frame = 0 8 + for i in range(10): 9 + frame_score = 0 10 + if frames[next_frame] == 10: 11 + frame_score += 10 12 + if i < 10: 13 + frame_score += frames[next_frame + 1] 14 + if frames[i + 1] == 10: 15 + frame_score += frames[next_frame + 2] 16 + else: 17 + frame_score += frames[next_frame + 2] 18 + next_frame += 1 19 + elif frames[next_frame] + frames[next_frame + 1] == 10: 20 + frame_score += 10 21 + frame_score += frames[next_frame + 2] 22 + next_frame += 2 23 + else: 24 + frame_score += frames[next_frame] + frames[next_frame + 1] 25 + next_frame += 2 26 + scores.append(frame_score) 27 + return sum(scores) 28 + 29 + 30 + print("Testing bowlingScore()...", end="") 31 + assert bowlingScore([10] * 12) == 300 32 + assert bowlingScore([7, 2, 8, 2, 10, 7, 1, 8, 2, 7, 3, 10, 10, 5, 4, 8, 2, 7]) == 162 33 + assert bowlingScore([2, 6, 2, 6, 9, 1, 10, 10, 10, 5, 1, 4, 5, 9, 0, 8, 1]) == 140 34 + assert ( 35 + bowlingScore([6, 4, 2, 7, 8, 1, 2, 4, 6, 3, 10, 6, 2, 1, 9, 6, 4, 10, 10, 10]) 36 + == 137 37 + ) 38 + assert bowlingScore([8, 1, 5, 3, 4, 3, 0, 8, 9, 0, 8, 1, 3, 6, 1, 8, 5, 4, 7, 1]) == 85 39 + 40 + # Finally, verify that the function is non-mutating 41 + L = [7, 2, 8, 2, 10, 7, 1, 8, 2, 7, 3, 10, 10, 5, 4, 8, 2, 7] 42 + bowlingScore(L) 43 + assert L == [7, 2, 8, 2, 10, 7, 1, 8, 2, 7, 3, 10, 10, 5, 4, 8, 2, 7] 44 + print("Passed!")
+45
python/oct3/level3/solvesCryptarithm.py
··· 1 + def solvesCryptarithm(puzzle: str, solution: str) -> bool: 2 + """Check if a cryptarithm puzzle is solved correctly. 3 + 4 + Args: 5 + puzzle (str): The cryptarithm puzzle. 6 + solution (str): The solution to the puzzle. 7 + 8 + Returns: 9 + bool: True if the puzzle is solved correctly, False otherwise. 10 + """ 11 + left = puzzle.split(" + ")[0].strip() 12 + right = puzzle.split(" + ")[1].split(" = ")[0].strip() 13 + result = puzzle.split(" + ")[1].split(" = ")[1].strip() 14 + scores = list(solution) 15 + for score, letter in enumerate(scores): 16 + print(score, letter) 17 + left = left.replace(letter, str(score)) 18 + right = right.replace(letter, str(score)) 19 + result = result.replace(letter, str(score)) 20 + print(left, right, result) 21 + try: 22 + return int(left) + int(right) == int(result) 23 + except ValueError: 24 + return False 25 + 26 + 27 + print("Testing solvesCryptarithm()...", end="") 28 + # 9567 + 1085 = 10652 29 + assert solvesCryptarithm("SEND + MORE = MONEY", "OMY--ENDRS") == True 30 + 31 + # 201689 + 201689 = 403378 32 + assert solvesCryptarithm("NUMBER + NUMBER = PUZZLE", "UMNZP-BLER") == True 33 + 34 + # 91542 + 3077542 = 3169084 35 + assert solvesCryptarithm("TILES + PUZZLES = PICTURE", "UISPELCZRT") == True 36 + 37 + # 8456 + 1074 = 10542 (False) 38 + assert solvesCryptarithm("SEND + MORE = MONEY", "OMY-ENDRS") == False 39 + 40 + # 9567 + 1085 = 1062 (False) 41 + assert solvesCryptarithm("SEND + MORE = MONY", "OMY--ENDRS") == False 42 + 43 + # No S in solution 44 + assert solvesCryptarithm("SEND + MORE = MONEY", "OMY--ENDR-") == False 45 + print("Passed!")
+52
python/oct3/level3/topScorer.py
··· 1 + from typing import List, Tuple 2 + 3 + 4 + def topScorer(data: str) -> str | None: 5 + """Return the name(s) of the student(s) with the highest total score.""" 6 + lines = [line for line in map(lambda line: line.strip(), data.split("\n")) if line] 7 + max_score: List[Tuple[str, int]] = [] 8 + for line in lines: 9 + name, *scores = line.split(",") 10 + scores = list(map(int, scores)) 11 + if not scores: 12 + max_score.append((name, 0)) 13 + else: 14 + max_score.append((name, sum(scores))) 15 + max_score.sort(key=lambda x: x[1], reverse=True) 16 + if not max_score: 17 + return None 18 + names = [name for name, score in max_score if score == max_score[0][1]] 19 + return ",".join(names) 20 + 21 + 22 + print("Testing topScorer()...", end="") 23 + data = """\ 24 + Joe,10,20,30,40 25 + Lauren,10,20,30 26 + Ben,10,20,30,5 27 + """ 28 + assert topScorer(data) == "Joe" 29 + 30 + data = """\ 31 + David,11,20,30 32 + Austin,10,20,30,1 33 + Lauren,50 34 + """ 35 + assert topScorer(data) == "David,Austin" 36 + 37 + data = """\ 38 + Ping-Ya,100,80,90 39 + """ 40 + assert topScorer(data) == "Ping-Ya" 41 + 42 + data = """\ 43 + Reyna,20,40,40 44 + Ema,5,5,5,5,10 45 + Ketandu,50,20,10,20 46 + Tanya,80,20 47 + Kate,70 48 + """ 49 + assert topScorer(data) == "Reyna,Ketandu,Tanya" 50 + 51 + assert topScorer("") == None 52 + print("Passed!")