From 5ed4a6d9be37ef53a44aa67ef0d1727166610b02 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sun, 29 Sep 2024 15:04:13 +0200 Subject: [PATCH] Add type hints in problems 11-20 --- Python/p011.py | 14 +++++--------- Python/p012.py | 2 +- Python/p013.py | 2 +- Python/p014.py | 6 +++--- Python/p015.py | 2 +- Python/p016.py | 2 +- Python/p017.py | 2 +- Python/p018.py | 2 +- Python/p019.py | 2 +- Python/p020.py | 2 +- 10 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Python/p011.py b/Python/p011.py index 8f4c0d9..6b039ad 100644 --- a/Python/p011.py +++ b/Python/p011.py @@ -31,7 +31,7 @@ from projecteuler import timing @timing -def p011(): +def p011() -> None: grid = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8], [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0], [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65], @@ -67,8 +67,7 @@ def p011(): prod = prod * grid[i][k] k = k + 1 - if prod > max_: - max_ = prod + max_ = max(max_, prod) # Vertical direction. prod = 1 @@ -77,8 +76,7 @@ def p011(): prod = prod * grid[k][j] k = k + 1 - if prod > max_: - max_ = prod + max_ = max(max_, prod) # Diagonal direction, from top left to bottom right. prod = 1 @@ -90,8 +88,7 @@ def p011(): k = k + 1 w = w + 1 - if prod > max_: - max_ = prod + max_ = max(max_, prod) # The last diagonal is handled separately for i in range(17): @@ -106,8 +103,7 @@ def p011(): k = k + 1 w = w - 1 - if prod > max_: - max_ = prod + max_ = max(max_, prod) print('Project Euler, Problem 11') print(f'Answer: {max_}') diff --git a/Python/p012.py b/Python/p012.py index 61ebc01..ba7e0fa 100644 --- a/Python/p012.py +++ b/Python/p012.py @@ -23,7 +23,7 @@ from projecteuler import count_divisors, timing @timing -def p012(): +def p012() -> None: i = 0 triang = 0 finished = 0 diff --git a/Python/p013.py b/Python/p013.py index 4412a34..be96dcb 100644 --- a/Python/p013.py +++ b/Python/p013.py @@ -109,7 +109,7 @@ from projecteuler import timing @timing -def p013(): +def p013() -> None: numbers = [37107287533902102798797998220837590246510135740250, 46376937677490009712648124896970078050417018260538, 74324986199524741059474233309513058123726617309629, diff --git a/Python/p014.py b/Python/p014.py index 382d563..777ad8d 100644 --- a/Python/p014.py +++ b/Python/p014.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3 # The following iterative sequence is defined for the set of positive integers: # @@ -28,7 +28,7 @@ collatz_found = zeros(N, dtype=int) # Recursive function to calculate the Collatz sequence for n. # If n is even, Collatz(n)=1+Collatz(n/2), if n is odd # Collatz(n)=1+Collatz(3*n+1). -def collatz_length(n): +def collatz_length(n: int) -> int: if n == 1: return 1 @@ -45,7 +45,7 @@ def collatz_length(n): @timing -def p014(): +def p014() -> None: max_l = 0 max_ = 0 diff --git a/Python/p015.py b/Python/p015.py index 11104f6..0abc19e 100644 --- a/Python/p015.py +++ b/Python/p015.py @@ -9,7 +9,7 @@ from projecteuler import timing @timing -def p015(): +def p015() -> None: # Using a combinatorial solution: in a 20x20 grid there will always be # 20 movements to the right and 20 movements down, that can be represented # as a string of Rs and Ds. The number of routes is the number of combinations. diff --git a/Python/p016.py b/Python/p016.py index 844dfc0..1621899 100644 --- a/Python/p016.py +++ b/Python/p016.py @@ -8,7 +8,7 @@ from projecteuler import timing @timing -def p016(): +def p016() -> None: # Simply calculate 2^1000, convert the result to string and calculate # the sum of the digits res = str(2 ** 1000) diff --git a/Python/p017.py b/Python/p017.py index ecc068c..bdb161c 100644 --- a/Python/p017.py +++ b/Python/p017.py @@ -11,7 +11,7 @@ from projecteuler import timing @timing -def p017(): +def p017() -> None: # First list contains number of letters for numbers from 1 to 19, # the second letters for "twenty", "thirty", ..., "ninety", # the third letters for "one hundred and", "two hundred and", ..., "nine hundre and", diff --git a/Python/p018.py b/Python/p018.py index 5448f14..c46de53 100644 --- a/Python/p018.py +++ b/Python/p018.py @@ -36,7 +36,7 @@ from projecteuler import find_max_path, timing @timing -def p018(): +def p018() -> None: try: with open('p018_triangle.txt', 'r', encoding='utf-8') as fp: triang = [] diff --git a/Python/p019.py b/Python/p019.py index ae79465..44d0224 100644 --- a/Python/p019.py +++ b/Python/p019.py @@ -19,7 +19,7 @@ from projecteuler import timing @timing -def p019(): +def p019() -> None: count = 0 # Use the datetime library to find out which first day of the month is a Sunday diff --git a/Python/p020.py b/Python/p020.py index 61073fd..cace681 100644 --- a/Python/p020.py +++ b/Python/p020.py @@ -13,7 +13,7 @@ from projecteuler import timing @timing -def p020(): +def p020() -> None: # Calculate the factorial, convert the result to string and sum the digits. n = str(factorial(100))