[Project Euler] Added type hints and refactored the code for Problem 14 (#3047)

* added type hints and refactored the code a bit

* made output statement more explicit

* used f-strings and updated type hints

* modified solution function to return an integer solution

* updated docstring

* Update sol1.py

* Update sol2.py

Co-authored-by: Dhruv <dhruvmanila@gmail.com>
This commit is contained in:
Suyash Gupta
2020-10-09 08:33:23 +05:30
committed by GitHub
parent 261be28120
commit a3bbcd5f88
2 changed files with 20 additions and 22 deletions

View File

@ -1,4 +1,6 @@
"""
Problem 14: https://projecteuler.net/problem=14
Problem Statement:
The following iterative sequence is defined for the set of positive integers:
@ -17,7 +19,7 @@ Which starting number, under one million, produces the longest chain?
"""
def solution(n):
def solution(n: int = 1000000) -> int:
"""Returns the number under n that generates the longest sequence using the
formula:
n → n/2 (n is even)
@ -25,13 +27,13 @@ def solution(n):
# The code below has been commented due to slow execution affecting Travis.
# >>> solution(1000000)
# {'counter': 525, 'largest_number': 837799}
# 837799
>>> solution(200)
{'counter': 125, 'largest_number': 171}
171
>>> solution(5000)
{'counter': 238, 'largest_number': 3711}
3711
>>> solution(15000)
{'counter': 276, 'largest_number': 13255}
13255
"""
largest_number = 0
pre_counter = 0
@ -51,11 +53,8 @@ def solution(n):
if counter > pre_counter:
largest_number = input1
pre_counter = counter
return {"counter": pre_counter, "largest_number": largest_number}
return largest_number
if __name__ == "__main__":
result = solution(int(input().strip()))
print(
("Largest Number:", result["largest_number"], "->", result["counter"], "digits")
)
print(solution(int(input().strip())))