From a3c5167fad2db8d1b5f2acee8cb1c5cf2606bd14 Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Mon, 26 Sep 2016 16:08:40 +0530 Subject: [PATCH 1/2] pep style and fixed exception on input other and integer type --- dynamic_programming/fibonacci.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index bb6d297eb..c790d41d8 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -1,16 +1,18 @@ """ This is a pure Python implementation of Dynamic Programming solution to the fibonacci sequence problem. """ -class Fibonacci: + +class Fibonacci: def __init__(self, N=None): if N: + N = int(N) self.fib_array = [0] * (N + 1) self.fib_array[0] = 0 self.fib_array[1] = 1 for i in range(2, N + 1): self.fib_array[i] = self.fib_array[ - i - 1] + self.fib_array[i - 2] + i - 1] + self.fib_array[i - 2] else: self.fib_array = [None] * (N + 1) @@ -43,12 +45,13 @@ if __name__ == '__main__': "\n********* Enter different values to get the corresponding fibonacci sequence, enter any negative number to exit. ************\n") while True: print("Enter value: ", end=" ") - i = eval(input()) - if i < 0: - print("\n********* Good Bye!! ************\n") - break - fib.get(i) - except NameError: - print("\nInvalid input, please try again.") + try: + i = eval(input()) + if i < 0: + print("\n********* Good Bye!! ************\n") + break + fib.get(i) + except NameError: + print("\nInvalid input, please try again.") except NameError: print("\n********* Invalid input, good bye!! ************\n") From 2dcb85d581e5ed9ae75e817bf2e90c7c8ae97e7c Mon Sep 17 00:00:00 2001 From: Akshay Sharma Date: Mon, 26 Sep 2016 16:09:59 +0530 Subject: [PATCH 2/2] PEP style --- dynamic_programming/fibonacci.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index c790d41d8..5733f693a 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -4,6 +4,7 @@ This is a pure Python implementation of Dynamic Programming solution to the fibo class Fibonacci: + def __init__(self, N=None): if N: N = int(N) @@ -12,7 +13,7 @@ class Fibonacci: self.fib_array[1] = 1 for i in range(2, N + 1): self.fib_array[i] = self.fib_array[ - i - 1] + self.fib_array[i - 2] + i - 1] + self.fib_array[i - 2] else: self.fib_array = [None] * (N + 1)