From 31e7d92547a6806075b6c11a7c449529abd1dcef Mon Sep 17 00:00:00 2001 From: nstoik Date: Mon, 5 Oct 2020 22:52:01 -0600 Subject: [PATCH 1/8] add typehints and docstrings --- project_euler/problem_25/sol1.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py index f0228915dc15..88e94d5d6013 100644 --- a/project_euler/problem_25/sol1.py +++ b/project_euler/problem_25/sol1.py @@ -25,8 +25,13 @@ """ -def fibonacci(n): - if n == 1 or type(n) is not int: +def fibonacci(n: int) -> int: + """ + Computes the Fibonacci number for input n by iterating through n numbers + and creating an array of ints using the Fibonacci formula. + Returns the nth element of the array. + """ + if n == 1: return 0 elif n == 2: return 1 @@ -38,7 +43,11 @@ def fibonacci(n): return sequence[n] -def fibonacci_digits_index(n): +def fibonacci_digits_index(n: int) -> int: + """ + Computes incrementing Fibonacci numbers starting from 3 until the length + of the resulting Fibonacci result is the input value n. + """ digits = 0 index = 2 @@ -49,8 +58,9 @@ def fibonacci_digits_index(n): return index -def solution(n): - """Returns the index of the first term in the Fibonacci sequence to contain +def solution(n: int = 1000) -> int: + """ + Returns the index of the first term in the Fibonacci sequence to contain n digits. >>> solution(1000) From 0cdb707f92f777366466b66f4e7d1f7a1f59ede5 Mon Sep 17 00:00:00 2001 From: nstoik Date: Mon, 5 Oct 2020 22:52:16 -0600 Subject: [PATCH 2/8] add typehint and default value --- project_euler/problem_25/sol2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_25/sol2.py b/project_euler/problem_25/sol2.py index c98f09b1d316..6ed790c7aae6 100644 --- a/project_euler/problem_25/sol2.py +++ b/project_euler/problem_25/sol2.py @@ -32,7 +32,7 @@ def fibonacci_generator(): yield b -def solution(n): +def solution(n: int = 1000) -> int: """Returns the index of the first term in the Fibonacci sequence to contain n digits. From 1ec2a293c758aede95797be7c918e14ab0f3e2d5 Mon Sep 17 00:00:00 2001 From: nstoik Date: Mon, 5 Oct 2020 22:53:14 -0600 Subject: [PATCH 3/8] add typehint and default value. Removed unused variable. --- project_euler/problem_25/sol3.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_25/sol3.py b/project_euler/problem_25/sol3.py index 4a1d9da76bf7..0b9f3a0c84ef 100644 --- a/project_euler/problem_25/sol3.py +++ b/project_euler/problem_25/sol3.py @@ -25,7 +25,7 @@ """ -def solution(n): +def solution(n: int = 1000) -> int: """Returns the index of the first term in the Fibonacci sequence to contain n digits. @@ -45,7 +45,7 @@ def solution(n): f = f1 + f2 f1, f2 = f2, f index += 1 - for j in str(f): + for _ in str(f): i += 1 if i == n: break From 87f8102a6fac5a5b8bd55848e530561faaf5fe93 Mon Sep 17 00:00:00 2001 From: nstoik Date: Tue, 6 Oct 2020 08:24:58 -0600 Subject: [PATCH 4/8] do not modifiy the given solution --- project_euler/problem_25/sol1.py | 2 +- project_euler/problem_25/sol3.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py index 88e94d5d6013..255aa099c750 100644 --- a/project_euler/problem_25/sol1.py +++ b/project_euler/problem_25/sol1.py @@ -31,7 +31,7 @@ def fibonacci(n: int) -> int: and creating an array of ints using the Fibonacci formula. Returns the nth element of the array. """ - if n == 1: + if n == 1 or type(n) is not int: return 0 elif n == 2: return 1 diff --git a/project_euler/problem_25/sol3.py b/project_euler/problem_25/sol3.py index 0b9f3a0c84ef..c66411dc55fc 100644 --- a/project_euler/problem_25/sol3.py +++ b/project_euler/problem_25/sol3.py @@ -45,7 +45,7 @@ def solution(n: int = 1000) -> int: f = f1 + f2 f1, f2 = f2, f index += 1 - for _ in str(f): + for j in str(f): i += 1 if i == n: break From 364112198c23ae77c7d785ad312a995e10015987 Mon Sep 17 00:00:00 2001 From: nstoik Date: Tue, 6 Oct 2020 08:32:15 -0600 Subject: [PATCH 5/8] add doctests --- project_euler/problem_25/sol1.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py index 255aa099c750..fe343bfe2475 100644 --- a/project_euler/problem_25/sol1.py +++ b/project_euler/problem_25/sol1.py @@ -30,6 +30,18 @@ def fibonacci(n: int) -> int: Computes the Fibonacci number for input n by iterating through n numbers and creating an array of ints using the Fibonacci formula. Returns the nth element of the array. + + >>> fibonacci(2) + 1 + >>> fibonacci(3) + 2 + >>> fibonacci(5) + 5 + >>> fibonacci(10) + 55 + >>> fibonacci(12) + 144 + """ if n == 1 or type(n) is not int: return 0 @@ -46,7 +58,17 @@ def fibonacci(n: int) -> int: def fibonacci_digits_index(n: int) -> int: """ Computes incrementing Fibonacci numbers starting from 3 until the length - of the resulting Fibonacci result is the input value n. + of the resulting Fibonacci result is the input value n. Returns the term + of the Fibonacci sequence where this occurs. + + >>> fibonacci_digits_index(1000) + 4782 + >>> fibonacci_digits_index(100) + 476 + >>> fibonacci_digits_index(50) + 237 + >>> fibonacci_digits_index(3) + 12 """ digits = 0 index = 2 From f8feca3836b7347e9f7a1b734aa8c7a0ff4eb3be Mon Sep 17 00:00:00 2001 From: nstoik Date: Tue, 6 Oct 2020 08:37:03 -0600 Subject: [PATCH 6/8] update sol1 after running black --- project_euler/problem_25/sol1.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/project_euler/problem_25/sol1.py b/project_euler/problem_25/sol1.py index fe343bfe2475..c30a74a43cb0 100644 --- a/project_euler/problem_25/sol1.py +++ b/project_euler/problem_25/sol1.py @@ -31,15 +31,15 @@ def fibonacci(n: int) -> int: and creating an array of ints using the Fibonacci formula. Returns the nth element of the array. - >>> fibonacci(2) + >>> fibonacci(2) 1 - >>> fibonacci(3) + >>> fibonacci(3) 2 - >>> fibonacci(5) + >>> fibonacci(5) 5 - >>> fibonacci(10) + >>> fibonacci(10) 55 - >>> fibonacci(12) + >>> fibonacci(12) 144 """ @@ -60,14 +60,14 @@ def fibonacci_digits_index(n: int) -> int: Computes incrementing Fibonacci numbers starting from 3 until the length of the resulting Fibonacci result is the input value n. Returns the term of the Fibonacci sequence where this occurs. - + >>> fibonacci_digits_index(1000) 4782 - >>> fibonacci_digits_index(100) + >>> fibonacci_digits_index(100) 476 - >>> fibonacci_digits_index(50) + >>> fibonacci_digits_index(50) 237 - >>> fibonacci_digits_index(3) + >>> fibonacci_digits_index(3) 12 """ digits = 0 From f0dc6d99d0c10ada36fe56ae2f92966b3003510d Mon Sep 17 00:00:00 2001 From: nstoik Date: Tue, 6 Oct 2020 08:47:49 -0600 Subject: [PATCH 7/8] add typehint, docstring, and doctest --- project_euler/problem_25/sol2.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_25/sol2.py b/project_euler/problem_25/sol2.py index 6ed790c7aae6..9205518b7ae3 100644 --- a/project_euler/problem_25/sol2.py +++ b/project_euler/problem_25/sol2.py @@ -25,7 +25,22 @@ """ -def fibonacci_generator(): +def fibonacci_generator() -> int: + """ + A generator that produces numbers in the Fibonacci sequence + + >>> generator = fibonacci_generator() + >>> next(generator) + 1 + >>> next(generator) + 2 + >>> next(generator) + 3 + >>> next(generator) + 5 + >>> next(generator) + 8 + """ a, b = 0, 1 while True: a, b = b, a + b From ca26d67056b72631c56d45fe69126615bd5bf804 Mon Sep 17 00:00:00 2001 From: nstoik Date: Tue, 6 Oct 2020 08:51:33 -0600 Subject: [PATCH 8/8] update sol2 after running black --- project_euler/problem_25/sol2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_25/sol2.py b/project_euler/problem_25/sol2.py index 9205518b7ae3..ed3b54bb351f 100644 --- a/project_euler/problem_25/sol2.py +++ b/project_euler/problem_25/sol2.py @@ -29,7 +29,7 @@ def fibonacci_generator() -> int: """ A generator that produces numbers in the Fibonacci sequence - >>> generator = fibonacci_generator() + >>> generator = fibonacci_generator() >>> next(generator) 1 >>> next(generator)