[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 22 deletions

View File

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

View File

@ -1,4 +1,6 @@
""" """
Problem 14: https://projecteuler.net/problem=14
Collatz conjecture: start with any positive integer n. Next term obtained from Collatz conjecture: start with any positive integer n. Next term obtained from
the previous term as follows: the previous term as follows:
@ -23,9 +25,10 @@ that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain? Which starting number, under one million, produces the longest chain?
""" """
from typing import List
def collatz_sequence(n): def collatz_sequence(n: int) -> List[int]:
"""Returns the Collatz sequence for n.""" """Returns the Collatz sequence for n."""
sequence = [n] sequence = [n]
while n != 1: while n != 1:
@ -37,27 +40,23 @@ def collatz_sequence(n):
return sequence return sequence
def solution(n): def solution(n: int = 1000000) -> int:
"""Returns the number under n that generates the longest Collatz sequence. """Returns the number under n that generates the longest Collatz sequence.
# The code below has been commented due to slow execution affecting Travis. # The code below has been commented due to slow execution affecting Travis.
# >>> solution(1000000) # >>> solution(1000000)
# {'counter': 525, 'largest_number': 837799} # 837799
>>> solution(200) >>> solution(200)
{'counter': 125, 'largest_number': 171} 171
>>> solution(5000) >>> solution(5000)
{'counter': 238, 'largest_number': 3711} 3711
>>> solution(15000) >>> solution(15000)
{'counter': 276, 'largest_number': 13255} 13255
""" """
result = max([(len(collatz_sequence(i)), i) for i in range(1, n)]) result = max([(len(collatz_sequence(i)), i) for i in range(1, n)])
return {"counter": result[0], "largest_number": result[1]} return result[1]
if __name__ == "__main__": if __name__ == "__main__":
result = solution(int(input().strip())) print(solution(int(input().strip())))
print(
"Longest Collatz sequence under one million is %d with length %d"
% (result["largest_number"], result["counter"])
)