Skip to content

Commit 7084994

Browse files
authored
Add typehints and default input for project_euler/problem_25 (TheAlgorithms#2901)
* add typehints and docstrings * add typehint and default value * add typehint and default value. Removed unused variable. * do not modifiy the given solution * add doctests * update sol1 after running black * add typehint, docstring, and doctest * update sol2 after running black
1 parent b405a66 commit 7084994

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

Diff for: project_euler/problem_25/sol1.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,24 @@
2525
"""
2626

2727

28-
def fibonacci(n):
28+
def fibonacci(n: int) -> int:
29+
"""
30+
Computes the Fibonacci number for input n by iterating through n numbers
31+
and creating an array of ints using the Fibonacci formula.
32+
Returns the nth element of the array.
33+
34+
>>> fibonacci(2)
35+
1
36+
>>> fibonacci(3)
37+
2
38+
>>> fibonacci(5)
39+
5
40+
>>> fibonacci(10)
41+
55
42+
>>> fibonacci(12)
43+
144
44+
45+
"""
2946
if n == 1 or type(n) is not int:
3047
return 0
3148
elif n == 2:
@@ -38,7 +55,21 @@ def fibonacci(n):
3855
return sequence[n]
3956

4057

41-
def fibonacci_digits_index(n):
58+
def fibonacci_digits_index(n: int) -> int:
59+
"""
60+
Computes incrementing Fibonacci numbers starting from 3 until the length
61+
of the resulting Fibonacci result is the input value n. Returns the term
62+
of the Fibonacci sequence where this occurs.
63+
64+
>>> fibonacci_digits_index(1000)
65+
4782
66+
>>> fibonacci_digits_index(100)
67+
476
68+
>>> fibonacci_digits_index(50)
69+
237
70+
>>> fibonacci_digits_index(3)
71+
12
72+
"""
4273
digits = 0
4374
index = 2
4475

@@ -49,8 +80,9 @@ def fibonacci_digits_index(n):
4980
return index
5081

5182

52-
def solution(n):
53-
"""Returns the index of the first term in the Fibonacci sequence to contain
83+
def solution(n: int = 1000) -> int:
84+
"""
85+
Returns the index of the first term in the Fibonacci sequence to contain
5486
n digits.
5587
5688
>>> solution(1000)

Diff for: project_euler/problem_25/sol2.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,29 @@
2525
"""
2626

2727

28-
def fibonacci_generator():
28+
def fibonacci_generator() -> int:
29+
"""
30+
A generator that produces numbers in the Fibonacci sequence
31+
32+
>>> generator = fibonacci_generator()
33+
>>> next(generator)
34+
1
35+
>>> next(generator)
36+
2
37+
>>> next(generator)
38+
3
39+
>>> next(generator)
40+
5
41+
>>> next(generator)
42+
8
43+
"""
2944
a, b = 0, 1
3045
while True:
3146
a, b = b, a + b
3247
yield b
3348

3449

35-
def solution(n):
50+
def solution(n: int = 1000) -> int:
3651
"""Returns the index of the first term in the Fibonacci sequence to contain
3752
n digits.
3853

Diff for: project_euler/problem_25/sol3.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"""
2626

2727

28-
def solution(n):
28+
def solution(n: int = 1000) -> int:
2929
"""Returns the index of the first term in the Fibonacci sequence to contain
3030
n digits.
3131

0 commit comments

Comments
 (0)