chore(Python): add fibonacci algorithms (#327)
parent
4e1b8a502a
commit
d55f566a00
|
@ -37,3 +37,9 @@
|
||||||
4. [Remove Duplicates from a String](strings/remove_duplicates_from_a_string.py)
|
4. [Remove Duplicates from a String](strings/remove_duplicates_from_a_string.py)
|
||||||
5. [First Non Repeating Character](strings/first_non_repeating_character.py)
|
5. [First Non Repeating Character](strings/first_non_repeating_character.py)
|
||||||
6. [Longest Common Subsequence](strings/longest_common_subsequence.py)
|
6. [Longest Common Subsequence](strings/longest_common_subsequence.py)
|
||||||
|
|
||||||
|
## Dynamic Programming
|
||||||
|
1. [Print Fibonacci Series Up To N-th Term](dynamic_programming/fibonacci_series.py)
|
||||||
|
2. [Sum Up To N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_sum.py )
|
||||||
|
3. [N-th Term Of Fibonacci Series](dynamic_programming/fibonacci_series_nth_term.py)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
# Print the fibonacci series up to term n using dynamic approach
|
||||||
|
# Fibonacci series starts from 0th term
|
||||||
|
|
||||||
|
"""
|
||||||
|
Output:
|
||||||
|
|
||||||
|
The fibonacci series up to term 20: 0 1 1 2 3 5 8 13 21 34 55
|
||||||
|
89 144 233 377 610 987 1597 2584 4181 6765
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
key = 20
|
||||||
|
|
||||||
|
if key < 0:
|
||||||
|
print("Please enter a valid term.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
d = {0: 0, 1: 1}
|
||||||
|
|
||||||
|
print(f"The fibonacci series up to term {key}: ", end=" ")
|
||||||
|
|
||||||
|
if key == 0:
|
||||||
|
print(0)
|
||||||
|
exit()
|
||||||
|
if key == 1:
|
||||||
|
print(0, "\t", 1)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
print(0, "\t", 1, sep="", end="")
|
||||||
|
|
||||||
|
|
||||||
|
def fibo(n):
|
||||||
|
if n in d.keys():
|
||||||
|
return d[n]
|
||||||
|
else:
|
||||||
|
d[n] = fibo(n - 1) + fibo(n - 2)
|
||||||
|
print("\t", d[n], end=" ")
|
||||||
|
return d[n]
|
||||||
|
|
||||||
|
|
||||||
|
fibo(key)
|
||||||
|
|
||||||
|
print()
|
|
@ -0,0 +1,34 @@
|
||||||
|
# Find the nth term of fibonacci series using dynamic approach
|
||||||
|
# Fibonacci series starts from 0th term
|
||||||
|
|
||||||
|
"""
|
||||||
|
Output:
|
||||||
|
|
||||||
|
The term 10 of the fibonacci series is: 55
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
key = 10
|
||||||
|
|
||||||
|
if key < 0:
|
||||||
|
print("Please enter a valid term.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
d = {0: 0, 1: 1}
|
||||||
|
|
||||||
|
if key < 2:
|
||||||
|
print(f"The term {key} of the fibonacci series is: ", d[key])
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
def fibo(n):
|
||||||
|
if n in d.keys():
|
||||||
|
return d[n]
|
||||||
|
else:
|
||||||
|
d[n] = fibo(n - 1) + fibo(n - 2)
|
||||||
|
return d[n]
|
||||||
|
|
||||||
|
|
||||||
|
fibo(key)
|
||||||
|
|
||||||
|
print(f"The term {key} of the fibonacci series is: ", d[key])
|
|
@ -0,0 +1,42 @@
|
||||||
|
# Find the sum up to nth term of fibonacci series using dynamic approach
|
||||||
|
# Fibonacci series starts from 0th term
|
||||||
|
|
||||||
|
"""
|
||||||
|
Output:
|
||||||
|
|
||||||
|
Sum up to term 10 of fibonacci series is: 143
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
key = 10
|
||||||
|
|
||||||
|
if key < 0:
|
||||||
|
print("Please enter a valid term.")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
d = {0: 0, 1: 1}
|
||||||
|
|
||||||
|
if key == 0:
|
||||||
|
print(f"Sum up to term {key} of fibonacci series: ", 0)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
if key == 1:
|
||||||
|
print(f"Sum up to term {key} of fibonacci series: ", 1)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
sum = 1
|
||||||
|
|
||||||
|
|
||||||
|
def fibo(n):
|
||||||
|
if n in d.keys():
|
||||||
|
return d[n]
|
||||||
|
else:
|
||||||
|
d[n] = fibo(n - 1) + fibo(n - 2)
|
||||||
|
global sum
|
||||||
|
sum += d[n]
|
||||||
|
return d[n]
|
||||||
|
|
||||||
|
|
||||||
|
fibo(key)
|
||||||
|
|
||||||
|
print(f"Sum up to term {key} of fibonacci series is: ", sum)
|
Loading…
Reference in New Issue