From 4bf2eedd3c234eddd04fbea314005ca06d32e923 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 14:07:45 +0800 Subject: [PATCH] [mypy] fix mypy error in Project Euler Problem 092 solution 1 (#5357) * fix mypy error * updating DIRECTORY.md * simplify code * run black * fix doc consistency * Fix doc * fix doc Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 5 +++ project_euler/problem_092/sol1.py | 55 +++++++++++-------------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 219877a3a..c197dd880 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -211,6 +211,9 @@ * Histogram Equalization * [Histogram Stretch](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/histogram_equalization/histogram_stretch.py) * [Index Calculation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/index_calculation.py) + * Morphological Operations + * [Dilation Operation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/morphological_operations/dilation_operation.py) + * [Erosion Operation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/morphological_operations/erosion_operation.py) * Resize * [Resize](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/resize/resize.py) * Rotation @@ -778,6 +781,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_089/sol1.py) * Problem 091 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py) + * Problem 092 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_092/sol1.py) * Problem 097 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_097/sol1.py) * Problem 099 diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index a02629a7b..dcda3a486 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -17,7 +17,7 @@ def next_number(number: int) -> int: Returns the next number of the chain by adding the square of each digit to form a neww number. For example if number = 12, next_number() will return 1^2 + 2^2 = 5. - Therefore 5 is the next number of the chain. + Therefore, 5 is the next number of the chain. >>> next_number(44) 32 >>> next_number(10) @@ -25,22 +25,22 @@ def next_number(number: int) -> int: >>> next_number(32) 13 """ - num = 0 - for i in range(len(str(number))): - num += int(str(number)[i]) ** 2 + sum_of_digits_squared = 0 + while number: + sum_of_digits_squared += (number % 10) ** 2 + number //= 10 - return num + return sum_of_digits_squared def chain(number: int) -> bool: """ - Generates the chain of numbers until the nest number generated is 1 0r 89. - for example, if starting number is 44, then the function generates the - following chain of numbers. - chain: 44 → 32 → 13 → 10 → 1 → 1 - once the next number generated is 1 or 89, the function - Returns True if the next number generated by next_number() if 1. - Returns False if the next number generated by next_number() is 89. + The function generates the chain of numbers until the next number is 1 or 89. + For example, if starting number is 44, then the function generates the + following chain of numbers: + 44 → 32 → 13 → 10 → 1 → 1. + Once the next number generated is 1 or 89, the function returns whether + or not the the next number generated by next_number() is 1. >>> chain(10) True >>> chain(58) @@ -51,43 +51,26 @@ def chain(number: int) -> bool: while number != 1 and number != 89: number = next_number(number) - if number == 1: - return True - - elif number == 89: - return False + return number == 1 def solution(number: int = 10000000) -> int: """ - The function returns the total numbers that end up in 89 after the chain generation. + The function returns the number of integers that end up being 89 in each chain. The function accepts a range number and the function checks all the values under value number. - if the chain generation leads to the end number as 1 or 89. If the chain() - returns True, then total is incremented, implying that the number we - started with ended up with 1 else total2 is incremented, implying that - the number we started with ended up in 89 after chain generation. - But the function returns total2 as the requirement of question is - to find out how many ended up in 89. >>> solution(100) 80 >>> solution(10000000) 8581146 """ - total = 0 - total2 = 0 - for i in range(1, number): - val = chain(i) - - if val is True: - total += 1 - - elif val is False: - total2 += 1 - - return total2 + return sum(1 for i in range(1, number) if not chain(i)) if __name__ == "__main__": + import doctest + + doctest.testmod() + print(f"{solution() = }")