From e70c27d434d22413a5a43d4830f30592d7564dd2 Mon Sep 17 00:00:00 2001 From: Archaengel Date: Mon, 5 Oct 2020 18:51:26 -0700 Subject: [PATCH 1/2] Fix typehints in project_euler/problem01 Squashed commit of the following: commit 6801d073b31bf702814861cd3b07b634ca295bfa Author: Archaengel Date: Mon Oct 5 16:40:10 2020 -0700 Fix typehints in project_euler/problem01 commit 29afc3af114abd1b99dc3f7c8fc99128229db131 Author: Archaengel Date: Mon Oct 5 15:06:34 2020 -0700 Add typehints and default argument for project_euler/problem_01 --- project_euler/problem_01/sol1.py | 2 +- project_euler/problem_01/sol2.py | 2 +- project_euler/problem_01/sol3.py | 2 +- project_euler/problem_01/sol4.py | 2 +- project_euler/problem_01/sol5.py | 2 +- project_euler/problem_01/sol6.py | 2 +- project_euler/problem_01/sol7.py | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/project_euler/problem_01/sol1.py b/project_euler/problem_01/sol1.py index e81156edaee4..2dbb60cdb16f 100644 --- a/project_euler/problem_01/sol1.py +++ b/project_euler/problem_01/sol1.py @@ -6,7 +6,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) diff --git a/project_euler/problem_01/sol2.py b/project_euler/problem_01/sol2.py index 8041c7ffa589..212fd40562d1 100644 --- a/project_euler/problem_01/sol2.py +++ b/project_euler/problem_01/sol2.py @@ -6,7 +6,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) diff --git a/project_euler/problem_01/sol3.py b/project_euler/problem_01/sol3.py index c0bcbc06ec83..faa505615924 100644 --- a/project_euler/problem_01/sol3.py +++ b/project_euler/problem_01/sol3.py @@ -6,7 +6,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """ This solution is based on the pattern that the successive numbers in the series follow: 0+3,+2,+1,+3,+1,+2,+3. diff --git a/project_euler/problem_01/sol4.py b/project_euler/problem_01/sol4.py index e01dc977d8cf..d5e86320da5e 100644 --- a/project_euler/problem_01/sol4.py +++ b/project_euler/problem_01/sol4.py @@ -6,7 +6,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) diff --git a/project_euler/problem_01/sol5.py b/project_euler/problem_01/sol5.py index bd96d965f92d..eae62ef5c75c 100644 --- a/project_euler/problem_01/sol5.py +++ b/project_euler/problem_01/sol5.py @@ -8,7 +8,7 @@ """A straightforward pythonic solution using list comprehension""" -def solution(n): +def solution(n: int = 1000) -> int: """Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) diff --git a/project_euler/problem_01/sol6.py b/project_euler/problem_01/sol6.py index c9f94b9f77c8..ca08a4a639ab 100644 --- a/project_euler/problem_01/sol6.py +++ b/project_euler/problem_01/sol6.py @@ -6,7 +6,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) diff --git a/project_euler/problem_01/sol7.py b/project_euler/problem_01/sol7.py index a0510b54c409..8e53d2a81fb9 100644 --- a/project_euler/problem_01/sol7.py +++ b/project_euler/problem_01/sol7.py @@ -6,7 +6,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the sum of all the multiples of 3 or 5 below n. >>> solution(3) From db03f25bbd63636826379928f9d5f95ba7ea4c51 Mon Sep 17 00:00:00 2001 From: Archaengel Date: Mon, 5 Oct 2020 20:38:39 -0700 Subject: [PATCH 2/2] Add default args, typehints, and expand variable names for PE prob 02 --- project_euler/problem_02/sol1.py | 8 ++++---- project_euler/problem_02/sol2.py | 18 +++++++++--------- project_euler/problem_02/sol3.py | 2 +- project_euler/problem_02/sol4.py | 6 +++--- project_euler/problem_02/sol5.py | 20 ++++++++++---------- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/project_euler/problem_02/sol1.py b/project_euler/problem_02/sol1.py index ec89ddaeb2b5..2acc93b0affc 100644 --- a/project_euler/problem_02/sol1.py +++ b/project_euler/problem_02/sol1.py @@ -11,7 +11,7 @@ """ -def solution(n): +def solution(n: int = 4000000) -> int: """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. @@ -28,13 +28,13 @@ def solution(n): """ i = 1 j = 2 - sum = 0 + total = 0 while j <= n: if j % 2 == 0: - sum += j + total += j i, j = j, i + j - return sum + return total if __name__ == "__main__": diff --git a/project_euler/problem_02/sol2.py b/project_euler/problem_02/sol2.py index bc5040cc6b3b..01fc552b9b21 100644 --- a/project_euler/problem_02/sol2.py +++ b/project_euler/problem_02/sol2.py @@ -11,28 +11,28 @@ """ -def solution(n): +def solution(n: int = 4000000) -> int: """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. >>> solution(10) - [2, 8] + 10 >>> solution(15) - [2, 8] + 10 >>> solution(2) - [2] + 2 >>> solution(1) - [] + 0 >>> solution(34) - [2, 8, 34] + 44 """ - ls = [] + even_fibs = [] a, b = 0, 1 while b <= n: if b % 2 == 0: - ls.append(b) + even_fibs.append(b) a, b = b, a + b - return ls + return sum(even_fibs) if __name__ == "__main__": diff --git a/project_euler/problem_02/sol3.py b/project_euler/problem_02/sol3.py index f29f21c287e5..53d8ca6f1b68 100644 --- a/project_euler/problem_02/sol3.py +++ b/project_euler/problem_02/sol3.py @@ -11,7 +11,7 @@ """ -def solution(n): +def solution(n: int = 4000000) -> int: """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. diff --git a/project_euler/problem_02/sol4.py b/project_euler/problem_02/sol4.py index be4328941aa3..a87410b7006d 100644 --- a/project_euler/problem_02/sol4.py +++ b/project_euler/problem_02/sol4.py @@ -13,7 +13,7 @@ from decimal import Decimal, getcontext -def solution(n): +def solution(n: int = 4000000) -> int: """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. @@ -57,8 +57,8 @@ def solution(n): index = (math.floor(math.log(n * (phi + 2), phi) - 1) // 3) * 3 + 2 num = Decimal(round(phi ** Decimal(index + 1))) / (phi + 2) - sum = num // 2 - return int(sum) + total = num // 2 + return int(total) if __name__ == "__main__": diff --git a/project_euler/problem_02/sol5.py b/project_euler/problem_02/sol5.py index 180906cf8717..dcf6eae85891 100644 --- a/project_euler/problem_02/sol5.py +++ b/project_euler/problem_02/sol5.py @@ -11,7 +11,7 @@ """ -def solution(n): +def solution(n: int = 4000000) -> int: """Returns the sum of all fibonacci sequence even elements that are lower or equals to n. @@ -27,19 +27,19 @@ def solution(n): 44 """ - a = [0, 1] + fib = [0, 1] i = 0 - while a[i] <= n: - a.append(a[i] + a[i + 1]) - if a[i + 2] > n: + while fib[i] <= n: + fib.append(fib[i] + fib[i + 1]) + if fib[i + 2] > n: break i += 1 - sum = 0 - for j in range(len(a) - 1): - if a[j] % 2 == 0: - sum += a[j] + total = 0 + for j in range(len(fib) - 1): + if fib[j] % 2 == 0: + total += fib[j] - return sum + return total if __name__ == "__main__":