Something went wrong. Try again.
CMU Coding Bootcamp
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546def 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 = 10652assert solvesCryptarithm("SEND + MORE = MONEY", "OMY--ENDRS") == True
# 201689 + 201689 = 403378assert solvesCryptarithm("NUMBER + NUMBER = PUZZLE", "UMNZP-BLER") == True
# 91542 + 3077542 = 3169084assert 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 solutionassert solvesCryptarithm("SEND + MORE = MONEY", "OMY--ENDR-") == Falseprint("Passed!")