Skip to content

Commit 4bf2eed

Browse files
poyeagithub-actions
and
github-actions
authored
[mypy] fix mypy error in Project Euler Problem 092 solution 1 (TheAlgorithms#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>
1 parent 7ef2e4d commit 4bf2eed

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

DIRECTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@
211211
* Histogram Equalization
212212
* [Histogram Stretch](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/histogram_equalization/histogram_stretch.py)
213213
* [Index Calculation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/index_calculation.py)
214+
* Morphological Operations
215+
* [Dilation Operation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/morphological_operations/dilation_operation.py)
216+
* [Erosion Operation](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/morphological_operations/erosion_operation.py)
214217
* Resize
215218
* [Resize](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/resize/resize.py)
216219
* Rotation
@@ -778,6 +781,8 @@
778781
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_089/sol1.py)
779782
* Problem 091
780783
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py)
784+
* Problem 092
785+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_092/sol1.py)
781786
* Problem 097
782787
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_097/sol1.py)
783788
* Problem 099

project_euler/problem_092/sol1.py

+18-35
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@ def next_number(number: int) -> int:
1717
Returns the next number of the chain by adding the square of each digit
1818
to form a neww number.
1919
For example if number = 12, next_number() will return 1^2 + 2^2 = 5.
20-
Therefore 5 is the next number of the chain.
20+
Therefore, 5 is the next number of the chain.
2121
>>> next_number(44)
2222
32
2323
>>> next_number(10)
2424
1
2525
>>> next_number(32)
2626
13
2727
"""
28-
num = 0
29-
for i in range(len(str(number))):
30-
num += int(str(number)[i]) ** 2
28+
sum_of_digits_squared = 0
29+
while number:
30+
sum_of_digits_squared += (number % 10) ** 2
31+
number //= 10
3132

32-
return num
33+
return sum_of_digits_squared
3334

3435

3536
def chain(number: int) -> bool:
3637
"""
37-
Generates the chain of numbers until the nest number generated is 1 0r 89.
38-
for example, if starting number is 44, then the function generates the
39-
following chain of numbers.
40-
chain: 44 → 32 → 13 → 10 → 1 → 1
41-
once the next number generated is 1 or 89, the function
42-
Returns True if the next number generated by next_number() if 1.
43-
Returns False if the next number generated by next_number() is 89.
38+
The function generates the chain of numbers until the next number is 1 or 89.
39+
For example, if starting number is 44, then the function generates the
40+
following chain of numbers:
41+
44 → 32 → 13 → 10 → 1 → 1.
42+
Once the next number generated is 1 or 89, the function returns whether
43+
or not the the next number generated by next_number() is 1.
4444
>>> chain(10)
4545
True
4646
>>> chain(58)
@@ -51,43 +51,26 @@ def chain(number: int) -> bool:
5151
while number != 1 and number != 89:
5252
number = next_number(number)
5353

54-
if number == 1:
55-
return True
56-
57-
elif number == 89:
58-
return False
54+
return number == 1
5955

6056

6157
def solution(number: int = 10000000) -> int:
6258
"""
63-
The function returns the total numbers that end up in 89 after the chain generation.
59+
The function returns the number of integers that end up being 89 in each chain.
6460
The function accepts a range number and the function checks all the values
6561
under value number.
66-
if the chain generation leads to the end number as 1 or 89. If the chain()
67-
returns True, then total is incremented, implying that the number we
68-
started with ended up with 1 else total2 is incremented, implying that
69-
the number we started with ended up in 89 after chain generation.
70-
But the function returns total2 as the requirement of question is
71-
to find out how many ended up in 89.
7262
7363
>>> solution(100)
7464
80
7565
>>> solution(10000000)
7666
8581146
7767
"""
78-
total = 0
79-
total2 = 0
80-
for i in range(1, number):
81-
val = chain(i)
82-
83-
if val is True:
84-
total += 1
68+
return sum(1 for i in range(1, number) if not chain(i))
8569

86-
elif val is False:
87-
total2 += 1
8870

89-
return total2
71+
if __name__ == "__main__":
72+
import doctest
9073

74+
doctest.testmod()
9175

92-
if __name__ == "__main__":
9376
print(f"{solution() = }")

0 commit comments

Comments
 (0)