From 968988d4bf44c052110901a37f3ed33f4ca9c176 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 03:09:03 +0800 Subject: [PATCH 1/7] fix mypy error --- project_euler/problem_092/sol1.py | 42 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index a02629a7bc7d..f28bb76461a9 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -25,11 +25,12 @@ 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: @@ -51,11 +52,7 @@ 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 True if number == 1 else False def solution(number: int = 10000000) -> int: @@ -63,11 +60,11 @@ def solution(number: int = 10000000) -> int: The function returns the total numbers that end up in 89 after the chain generation. 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 + If the chain generation leads to the end number as 1 or 89. If the chain() + returns True, then total_1 is incremented, implying that the number we + started with ended up with 1. Otherwise, total_89 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 + But the function returns total_89 as the requirement of question is to find out how many ended up in 89. >>> solution(100) @@ -75,19 +72,20 @@ def solution(number: int = 10000000) -> int: >>> solution(10000000) 8581146 """ - total = 0 - total2 = 0 + total_1 = 0 + total_89 = 0 for i in range(1, number): - val = chain(i) + if chain(i): + total_1 += 1 + else: + total_89 += 1 - if val is True: - total += 1 + return total_89 - elif val is False: - total2 += 1 - return total2 +if __name__ == "__main__": + import doctest + doctest.testmod() -if __name__ == "__main__": print(f"{solution() = }") From aa01d602529f66e1534f7c59bc0b2ce863a1419a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 16 Oct 2021 19:09:54 +0000 Subject: [PATCH 2/7] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 219877a3aee8..c197dd88032e 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 From c558325190787133ef8fdf419a4863a3aa26972d Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 03:19:48 +0800 Subject: [PATCH 3/7] simplify code --- project_euler/problem_092/sol1.py | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index f28bb76461a9..84ce29e9d602 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -35,13 +35,12 @@ def next_number(number: int) -> int: def chain(number: int) -> bool: """ - Generates the chain of numbers until the nest number generated is 1 0r 89. + Generates the chain of numbers until the nest number generated is 1 or 89. for example, if starting number is 44, then the function generates the - following chain of numbers. + 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. + 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) @@ -52,35 +51,21 @@ def chain(number: int) -> bool: while number != 1 and number != 89: number = next_number(number) - return True if number == 1 else 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 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_1 is incremented, implying that the number we - started with ended up with 1. Otherwise, total_89 is incremented, implying that - the number we started with ended up in 89 after chain generation. - But the function returns total_89 as the requirement of question is - to find out how many ended up in 89. + The function returns the number of integers that end up being 89 in each chain. >>> solution(100) 80 >>> solution(10000000) 8581146 """ - total_1 = 0 - total_89 = 0 - for i in range(1, number): - if chain(i): - total_1 += 1 - else: - total_89 += 1 - - return total_89 + return sum(1 for i in range(1, number) if not chain(i)) if __name__ == "__main__": From add8688b22130d929b8dacaf83a324a22281c34f Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 03:23:01 +0800 Subject: [PATCH 4/7] run black --- project_euler/problem_092/sol1.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index 84ce29e9d602..d9b029bceb5a 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -53,6 +53,7 @@ def chain(number: int) -> bool: return number == 1 + def solution(number: int = 10000000) -> int: """ The function returns the total numbers that end up in 89 after the chain generation. From 28e6b4a0f8e30ea6409adebd7f9e75bfb6f12661 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 03:26:24 +0800 Subject: [PATCH 5/7] fix doc consistency --- project_euler/problem_092/sol1.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index d9b029bceb5a..1bb749b255d7 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -35,8 +35,9 @@ def next_number(number: int) -> int: def chain(number: int) -> bool: """ - Generates the chain of numbers until the nest number generated is 1 or 89. - for example, if starting number is 44, then the function generates the + The function generates the chain of numbers until the nest number + generated is 1 or 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 whether From 008e3f49890f6ee426d06912201dd6298090ea05 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 03:32:17 +0800 Subject: [PATCH 6/7] Fix doc --- project_euler/problem_092/sol1.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index 1bb749b255d7..3e0bbc76475d 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -39,7 +39,7 @@ def chain(number: int) -> bool: generated is 1 or 89. For example, if starting number is 44, then the function generates the following chain of numbers. - chain: 44 → 32 → 13 → 10 → 1 → 1 + Chain: 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) @@ -57,10 +57,9 @@ def chain(number: int) -> bool: 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. - The function returns the number of integers that end up being 89 in each chain. >>> solution(100) 80 From ca2ef28e95ae5b8ae66bdd471067e1e9eaf3b481 Mon Sep 17 00:00:00 2001 From: John Law Date: Sun, 17 Oct 2021 03:35:09 +0800 Subject: [PATCH 7/7] fix doc --- project_euler/problem_092/sol1.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index 3e0bbc76475d..dcda3a48679e 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) @@ -35,11 +35,10 @@ def next_number(number: int) -> int: def chain(number: int) -> bool: """ - The function generates the chain of numbers until the nest number - generated is 1 or 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. - Chain: 44 → 32 → 13 → 10 → 1 → 1. + 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)