mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[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>
This commit is contained in:
parent
7ef2e4d1d0
commit
4bf2eedd3c
@ -211,6 +211,9 @@
|
|||||||
* Histogram Equalization
|
* Histogram Equalization
|
||||||
* [Histogram Stretch](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/histogram_equalization/histogram_stretch.py)
|
* [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)
|
* [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
|
||||||
* [Resize](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/resize/resize.py)
|
* [Resize](https://github.com/TheAlgorithms/Python/blob/master/digital_image_processing/resize/resize.py)
|
||||||
* Rotation
|
* Rotation
|
||||||
@ -778,6 +781,8 @@
|
|||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_089/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_089/sol1.py)
|
||||||
* Problem 091
|
* Problem 091
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py)
|
* [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
|
* Problem 097
|
||||||
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_097/sol1.py)
|
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_097/sol1.py)
|
||||||
* Problem 099
|
* Problem 099
|
||||||
|
@ -17,7 +17,7 @@ def next_number(number: int) -> int:
|
|||||||
Returns the next number of the chain by adding the square of each digit
|
Returns the next number of the chain by adding the square of each digit
|
||||||
to form a neww number.
|
to form a neww number.
|
||||||
For example if number = 12, next_number() will return 1^2 + 2^2 = 5.
|
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)
|
>>> next_number(44)
|
||||||
32
|
32
|
||||||
>>> next_number(10)
|
>>> next_number(10)
|
||||||
@ -25,22 +25,22 @@ def next_number(number: int) -> int:
|
|||||||
>>> next_number(32)
|
>>> next_number(32)
|
||||||
13
|
13
|
||||||
"""
|
"""
|
||||||
num = 0
|
sum_of_digits_squared = 0
|
||||||
for i in range(len(str(number))):
|
while number:
|
||||||
num += int(str(number)[i]) ** 2
|
sum_of_digits_squared += (number % 10) ** 2
|
||||||
|
number //= 10
|
||||||
|
|
||||||
return num
|
return sum_of_digits_squared
|
||||||
|
|
||||||
|
|
||||||
def chain(number: int) -> bool:
|
def chain(number: int) -> bool:
|
||||||
"""
|
"""
|
||||||
Generates the chain of numbers until the nest number generated is 1 0r 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
|
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
|
44 → 32 → 13 → 10 → 1 → 1.
|
||||||
once the next number generated is 1 or 89, the function
|
Once the next number generated is 1 or 89, the function returns whether
|
||||||
Returns True if the next number generated by next_number() if 1.
|
or not the the next number generated by next_number() is 1.
|
||||||
Returns False if the next number generated by next_number() is 89.
|
|
||||||
>>> chain(10)
|
>>> chain(10)
|
||||||
True
|
True
|
||||||
>>> chain(58)
|
>>> chain(58)
|
||||||
@ -51,43 +51,26 @@ def chain(number: int) -> bool:
|
|||||||
while number != 1 and number != 89:
|
while number != 1 and number != 89:
|
||||||
number = next_number(number)
|
number = next_number(number)
|
||||||
|
|
||||||
if number == 1:
|
return number == 1
|
||||||
return True
|
|
||||||
|
|
||||||
elif number == 89:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def solution(number: int = 10000000) -> int:
|
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
|
The function accepts a range number and the function checks all the values
|
||||||
under value number.
|
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)
|
>>> solution(100)
|
||||||
80
|
80
|
||||||
>>> solution(10000000)
|
>>> solution(10000000)
|
||||||
8581146
|
8581146
|
||||||
"""
|
"""
|
||||||
total = 0
|
return sum(1 for i in range(1, number) if not chain(i))
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
import doctest
|
||||||
|
|
||||||
|
doctest.testmod()
|
||||||
|
|
||||||
print(f"{solution() = }")
|
print(f"{solution() = }")
|
||||||
|
Loading…
Reference in New Issue
Block a user