From a3bbcd5f88985248cf44f5797b18e81f9115f85e Mon Sep 17 00:00:00 2001 From: Suyash Gupta Date: Fri, 9 Oct 2020 08:33:23 +0530 Subject: [PATCH] [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 --- project_euler/problem_14/sol1.py | 19 +++++++++---------- project_euler/problem_14/sol2.py | 23 +++++++++++------------ 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/project_euler/problem_14/sol1.py b/project_euler/problem_14/sol1.py index fda45bc94..1745ec931 100644 --- a/project_euler/problem_14/sol1.py +++ b/project_euler/problem_14/sol1.py @@ -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()))) diff --git a/project_euler/problem_14/sol2.py b/project_euler/problem_14/sol2.py index 375a34c72..20ad96327 100644 --- a/project_euler/problem_14/sol2.py +++ b/project_euler/problem_14/sol2.py @@ -1,4 +1,6 @@ """ +Problem 14: https://projecteuler.net/problem=14 + Collatz conjecture: start with any positive integer n. Next term obtained from 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? """ +from typing import List -def collatz_sequence(n): +def collatz_sequence(n: int) -> List[int]: """Returns the Collatz sequence for n.""" sequence = [n] while n != 1: @@ -37,27 +40,23 @@ def collatz_sequence(n): return sequence -def solution(n): +def solution(n: int = 1000000) -> int: """Returns the number under n that generates the longest Collatz sequence. # 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 """ 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__": - result = solution(int(input().strip())) - print( - "Longest Collatz sequence under one million is %d with length %d" - % (result["largest_number"], result["counter"]) - ) + print(solution(int(input().strip())))