mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-22 05:37:36 +00:00
Add typehints and default input for project_euler/problem_25 (#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
This commit is contained in:
parent
91ad30c2b0
commit
4d4ce400ec
@ -25,7 +25,24 @@ digits?
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def fibonacci(n):
|
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:
|
if n == 1 or type(n) is not int:
|
||||||
return 0
|
return 0
|
||||||
elif n == 2:
|
elif n == 2:
|
||||||
@ -38,7 +55,21 @@ def fibonacci(n):
|
|||||||
return sequence[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. 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
|
digits = 0
|
||||||
index = 2
|
index = 2
|
||||||
|
|
||||||
@ -49,8 +80,9 @@ def fibonacci_digits_index(n):
|
|||||||
return index
|
return index
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n: int = 1000) -> int:
|
||||||
"""Returns the index of the first term in the Fibonacci sequence to contain
|
"""
|
||||||
|
Returns the index of the first term in the Fibonacci sequence to contain
|
||||||
n digits.
|
n digits.
|
||||||
|
|
||||||
>>> solution(1000)
|
>>> solution(1000)
|
||||||
|
@ -25,14 +25,29 @@ digits?
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
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
|
a, b = 0, 1
|
||||||
while True:
|
while True:
|
||||||
a, b = b, a + b
|
a, b = b, a + b
|
||||||
yield b
|
yield b
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n: int = 1000) -> int:
|
||||||
"""Returns the index of the first term in the Fibonacci sequence to contain
|
"""Returns the index of the first term in the Fibonacci sequence to contain
|
||||||
n digits.
|
n digits.
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ digits?
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def solution(n):
|
def solution(n: int = 1000) -> int:
|
||||||
"""Returns the index of the first term in the Fibonacci sequence to contain
|
"""Returns the index of the first term in the Fibonacci sequence to contain
|
||||||
n digits.
|
n digits.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user