From bd87a173264c4c510ad5d68593dcd0b053d7e82a Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 30 May 2015 11:06:39 -0400 Subject: [PATCH] Cleaned up code. --- recursion-dynamic/fibonacci.ipynb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/recursion-dynamic/fibonacci.ipynb b/recursion-dynamic/fibonacci.ipynb index 0fda31c..d10c4a4 100644 --- a/recursion-dynamic/fibonacci.ipynb +++ b/recursion-dynamic/fibonacci.ipynb @@ -80,7 +80,7 @@ " else:\n", " return fib_recursive(n-1) + fib_recursive(n-2)\n", "\n", - "for i in xrange(0, num_items):\n", + "for i in xrange(num_items):\n", " print(fib_recursive(i))" ] }, @@ -102,7 +102,7 @@ " cache[n] = fib_dynamic(n-1) + fib_dynamic(n-2)\n", " return cache[n]\n", "\n", - "for i in xrange(0, num_items):\n", + "for i in xrange(num_items):\n", " print(fib_dynamic(i))" ] }, @@ -115,18 +115,13 @@ "outputs": [], "source": [ "def fib_iterative(n):\n", - " if n == 0 or n == 1:\n", - " return n\n", - " else:\n", - " n_minus_2 = 0 \n", - " n_minus_1 = 1\n", - " total = 0\n", - " for _ in xrange(2, n+1):\n", - " total = n_minus_2 + n_minus_1\n", - " n_minus_2, n_minus_1 = n_minus_1, total\n", - " return total\n", + " a = 0 \n", + " b = 1\n", + " for _ in xrange(n):\n", + " a, b = b, a + b\n", + " return a\n", "\n", - "for i in xrange(0, num_items):\n", + "for i in xrange(num_items):\n", " print(fib_iterative(i))" ] }