diff --git a/python/oct3/level3/bowlingScore.py b/python/oct3/level3/bowlingScore.py new file mode 100644 --- /dev/null +++ b/python/oct3/level3/bowlingScore.py @@ -0,0 +1,44 @@ +from typing import List + + +def bowlingScore(frames: List[int]) -> int: + """Calculate the total score of a bowling game.""" + scores: List[int] = [] + next_frame = 0 + for i in range(10): + frame_score = 0 + if frames[next_frame] == 10: + frame_score += 10 + if i < 10: + frame_score += frames[next_frame + 1] + if frames[i + 1] == 10: + frame_score += frames[next_frame + 2] + else: + frame_score += frames[next_frame + 2] + next_frame += 1 + elif frames[next_frame] + frames[next_frame + 1] == 10: + frame_score += 10 + frame_score += frames[next_frame + 2] + next_frame += 2 + else: + frame_score += frames[next_frame] + frames[next_frame + 1] + next_frame += 2 + scores.append(frame_score) + return sum(scores) + + +print("Testing bowlingScore()...", end="") +assert bowlingScore([10] * 12) == 300 +assert bowlingScore([7, 2, 8, 2, 10, 7, 1, 8, 2, 7, 3, 10, 10, 5, 4, 8, 2, 7]) == 162 +assert bowlingScore([2, 6, 2, 6, 9, 1, 10, 10, 10, 5, 1, 4, 5, 9, 0, 8, 1]) == 140 +assert ( + bowlingScore([6, 4, 2, 7, 8, 1, 2, 4, 6, 3, 10, 6, 2, 1, 9, 6, 4, 10, 10, 10]) + == 137 +) +assert bowlingScore([8, 1, 5, 3, 4, 3, 0, 8, 9, 0, 8, 1, 3, 6, 1, 8, 5, 4, 7, 1]) == 85 + +# Finally, verify that the function is non-mutating +L = [7, 2, 8, 2, 10, 7, 1, 8, 2, 7, 3, 10, 10, 5, 4, 8, 2, 7] +bowlingScore(L) +assert L == [7, 2, 8, 2, 10, 7, 1, 8, 2, 7, 3, 10, 10, 5, 4, 8, 2, 7] +print("Passed!") diff --git a/python/oct3/level3/solvesCryptarithm.py b/python/oct3/level3/solvesCryptarithm.py new file mode 100644 --- /dev/null +++ b/python/oct3/level3/solvesCryptarithm.py @@ -0,0 +1,45 @@ +def solvesCryptarithm(puzzle: str, solution: str) -> bool: + """Check if a cryptarithm puzzle is solved correctly. + + Args: + puzzle (str): The cryptarithm puzzle. + solution (str): The solution to the puzzle. + + Returns: + bool: True if the puzzle is solved correctly, False otherwise. + """ + left = puzzle.split(" + ")[0].strip() + right = puzzle.split(" + ")[1].split(" = ")[0].strip() + result = puzzle.split(" + ")[1].split(" = ")[1].strip() + scores = list(solution) + for score, letter in enumerate(scores): + print(score, letter) + left = left.replace(letter, str(score)) + right = right.replace(letter, str(score)) + result = result.replace(letter, str(score)) + print(left, right, result) + try: + return int(left) + int(right) == int(result) + except ValueError: + return False + + +print("Testing solvesCryptarithm()...", end="") +# 9567 + 1085 = 10652 +assert solvesCryptarithm("SEND + MORE = MONEY", "OMY--ENDRS") == True + +# 201689 + 201689 = 403378 +assert solvesCryptarithm("NUMBER + NUMBER = PUZZLE", "UMNZP-BLER") == True + +# 91542 + 3077542 = 3169084 +assert solvesCryptarithm("TILES + PUZZLES = PICTURE", "UISPELCZRT") == True + +# 8456 + 1074 = 10542 (False) +assert solvesCryptarithm("SEND + MORE = MONEY", "OMY-ENDRS") == False + +# 9567 + 1085 = 1062 (False) +assert solvesCryptarithm("SEND + MORE = MONY", "OMY--ENDRS") == False + +# No S in solution +assert solvesCryptarithm("SEND + MORE = MONEY", "OMY--ENDR-") == False +print("Passed!") diff --git a/python/oct3/level3/topScorer.py b/python/oct3/level3/topScorer.py new file mode 100644 --- /dev/null +++ b/python/oct3/level3/topScorer.py @@ -0,0 +1,52 @@ +from typing import List, Tuple + + +def topScorer(data: str) -> str | None: + """Return the name(s) of the student(s) with the highest total score.""" + lines = [line for line in map(lambda line: line.strip(), data.split("\n")) if line] + max_score: List[Tuple[str, int]] = [] + for line in lines: + name, *scores = line.split(",") + scores = list(map(int, scores)) + if not scores: + max_score.append((name, 0)) + else: + max_score.append((name, sum(scores))) + max_score.sort(key=lambda x: x[1], reverse=True) + if not max_score: + return None + names = [name for name, score in max_score if score == max_score[0][1]] + return ",".join(names) + + +print("Testing topScorer()...", end="") +data = """\ +Joe,10,20,30,40 +Lauren,10,20,30 +Ben,10,20,30,5 +""" +assert topScorer(data) == "Joe" + +data = """\ +David,11,20,30 +Austin,10,20,30,1 +Lauren,50 +""" +assert topScorer(data) == "David,Austin" + +data = """\ +Ping-Ya,100,80,90 +""" +assert topScorer(data) == "Ping-Ya" + +data = """\ +Reyna,20,40,40 +Ema,5,5,5,5,10 +Ketandu,50,20,10,20 +Tanya,80,20 +Kate,70 +""" +assert topScorer(data) == "Reyna,Ketandu,Tanya" + +assert topScorer("") == None +print("Passed!")