From 45fa576bc23b0f0665a27146968fc2df1194940f Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 6 Jul 2015 06:15:53 -0400 Subject: [PATCH] Changed xrange to range to be compatible with Python 3. --- arrays_strings/hash_map/hash_map_solution.ipynb | 2 +- .../reverse_string/reverse_string_solution.ipynb | 2 +- .../unique_chars/unique_chars_solution.ipynb | 11 +++++------ linked_lists/palindrome/palindrome_solution.ipynb | 2 +- .../maximizing_xor/maximizing_xor_solution.ipynb | 4 ++-- .../utopian_tree/utopian_tree_solution.ipynb | 2 +- recursion_dynamic/fibonacci/fibonacci_challenge.ipynb | 2 +- recursion_dynamic/fibonacci/fibonacci_solution.ipynb | 4 ++-- recursion_dynamic/fibonacci/test_fibonacci.py | 4 ++-- .../insertion_sort/insertion_sort_solution.ipynb | 4 ++-- .../quick_sort/quick_sort_solution.ipynb | 2 +- .../selection_sort/selection_sort_solution.ipynb | 4 ++-- stacks_queues/hanoi/hanoi_challenge.ipynb | 4 ++-- stacks_queues/hanoi/hanoi_solution.ipynb | 4 ++-- stacks_queues/hanoi/test_hanoi.py | 4 ++-- stacks_queues/n_stacks/n_stacks_challenge.ipynb | 2 +- stacks_queues/n_stacks/n_stacks_solution.ipynb | 2 +- stacks_queues/n_stacks/test_n_stacks.py | 8 ++++---- stacks_queues/sort_stack/sort_stack_challenge.ipynb | 4 ++-- stacks_queues/sort_stack/sort_stack_solution.ipynb | 4 ++-- stacks_queues/sort_stack/test_sort_stack.py | 8 ++++---- 21 files changed, 41 insertions(+), 42 deletions(-) diff --git a/arrays_strings/hash_map/hash_map_solution.ipynb b/arrays_strings/hash_map/hash_map_solution.ipynb index 763f6b0..32710e9 100644 --- a/arrays_strings/hash_map/hash_map_solution.ipynb +++ b/arrays_strings/hash_map/hash_map_solution.ipynb @@ -125,7 +125,7 @@ " \n", " def __init__(self, size):\n", " self.size = size\n", - " self.table = [[] for _ in xrange(self.size)]\n", + " self.table = [[] for _ in range(self.size)]\n", "\n", " def hash_function(self, key):\n", " return key % self.size\n", diff --git a/arrays_strings/reverse_string/reverse_string_solution.ipynb b/arrays_strings/reverse_string/reverse_string_solution.ipynb index c23822d..18a2aae 100644 --- a/arrays_strings/reverse_string/reverse_string_solution.ipynb +++ b/arrays_strings/reverse_string/reverse_string_solution.ipynb @@ -96,7 +96,7 @@ " if chars is None:\n", " return None\n", " size = len(chars)\n", - " for i in xrange(size/2):\n", + " for i in range(size/2):\n", " chars[i], chars[size-1-i] = \\\n", " chars[size-1-i], chars[i]\n", " return chars" diff --git a/arrays_strings/unique_chars/unique_chars_solution.ipynb b/arrays_strings/unique_chars/unique_chars_solution.ipynb index 752f68d..d07e0a2 100644 --- a/arrays_strings/unique_chars/unique_chars_solution.ipynb +++ b/arrays_strings/unique_chars/unique_chars_solution.ipynb @@ -37,7 +37,6 @@ "source": [ "## Constraints\n", "\n", - "* Can you assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -259,21 +258,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.4.3" } }, "nbformat": 4, diff --git a/linked_lists/palindrome/palindrome_solution.ipynb b/linked_lists/palindrome/palindrome_solution.ipynb index 0dda885..93b53a3 100644 --- a/linked_lists/palindrome/palindrome_solution.ipynb +++ b/linked_lists/palindrome/palindrome_solution.ipynb @@ -120,7 +120,7 @@ " iterations_to_compare_half = length / 2\n", " curr = self.head\n", " curr_reversed = reversed_list.head\n", - " for _ in xrange(0, iterations_to_compare_half):\n", + " for _ in range(0, iterations_to_compare_half):\n", " if curr.data != curr_reversed.data:\n", " return False\n", " curr = curr.next\n", diff --git a/online_judges/maximizing_xor/maximizing_xor_solution.ipynb b/online_judges/maximizing_xor/maximizing_xor_solution.ipynb index a1d5a05..ebb422a 100644 --- a/online_judges/maximizing_xor/maximizing_xor_solution.ipynb +++ b/online_judges/maximizing_xor/maximizing_xor_solution.ipynb @@ -82,8 +82,8 @@ "source": [ "def max_xor(lower, upper):\n", " result = 0\n", - " for l in xrange(lower, upper + 1):\n", - " for u in xrange(lower, upper + 1):\n", + " for l in range(lower, upper + 1):\n", + " for u in range(lower, upper + 1):\n", " curr = l ^ u\n", " if result < curr:\n", " result = curr\n", diff --git a/online_judges/utopian_tree/utopian_tree_solution.ipynb b/online_judges/utopian_tree/utopian_tree_solution.ipynb index 5762a04..fca76ab 100644 --- a/online_judges/utopian_tree/utopian_tree_solution.ipynb +++ b/online_judges/utopian_tree/utopian_tree_solution.ipynb @@ -87,7 +87,7 @@ " height = 1\n", " if cycles == 0:\n", " return height\n", - " for i in xrange(1, cycles+1):\n", + " for i in range(1, cycles+1):\n", " if i % 2 == 1:\n", " height *= 2\n", " else:\n", diff --git a/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb b/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb index ab9a143..157f7eb 100644 --- a/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb +++ b/recursion_dynamic/fibonacci/fibonacci_challenge.ipynb @@ -134,7 +134,7 @@ " \n", " def test_fib(self, func):\n", " result = []\n", - " for i in xrange(num_items):\n", + " for i in range(num_items):\n", " result.append(func(i))\n", " fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", " assert_equal(result, fib_seq)\n", diff --git a/recursion_dynamic/fibonacci/fibonacci_solution.ipynb b/recursion_dynamic/fibonacci/fibonacci_solution.ipynb index 1b8cab4..f8161f2 100644 --- a/recursion_dynamic/fibonacci/fibonacci_solution.ipynb +++ b/recursion_dynamic/fibonacci/fibonacci_solution.ipynb @@ -117,7 +117,7 @@ "def fib_iterative(n):\n", " a = 0 \n", " b = 1\n", - " for _ in xrange(n):\n", + " for _ in range(n):\n", " a, b = b, a + b\n", " return a" ] @@ -154,7 +154,7 @@ " \n", " def test_fib(self, func):\n", " result = []\n", - " for i in xrange(num_items):\n", + " for i in range(num_items):\n", " result.append(func(i))\n", " fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", " assert_equal(result, fib_seq)\n", diff --git a/recursion_dynamic/fibonacci/test_fibonacci.py b/recursion_dynamic/fibonacci/test_fibonacci.py index 07812b5..ced1b0a 100644 --- a/recursion_dynamic/fibonacci/test_fibonacci.py +++ b/recursion_dynamic/fibonacci/test_fibonacci.py @@ -2,10 +2,10 @@ from nose.tools import assert_equal class TestFib(object): - + def test_fib(self, func): result = [] - for i in xrange(num_items): + for i in range(num_items): result.append(func(i)) fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] assert_equal(result, fib_seq) diff --git a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb index 58cd4f6..83314b7 100644 --- a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb +++ b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb @@ -88,8 +88,8 @@ "def insertion_sort(data):\n", " if len(data) < 2:\n", " return\n", - " for r in xrange(1, len(data)):\n", - " for l in xrange(0, r):\n", + " for r in range(1, len(data)):\n", + " for l in range(0, r):\n", " if data[l] > data[r]:\n", " temp = data[r]\n", " data[l+1:r+1] = data[l:r]\n", diff --git a/sorting_searching/quick_sort/quick_sort_solution.ipynb b/sorting_searching/quick_sort/quick_sort_solution.ipynb index 73abdc9..06d3ebf 100644 --- a/sorting_searching/quick_sort/quick_sort_solution.ipynb +++ b/sorting_searching/quick_sort/quick_sort_solution.ipynb @@ -97,7 +97,7 @@ " pivot_value = data[pivot_index]\n", " \n", " # Build the left and right partitions\n", - " for i in xrange(0, len(data)):\n", + " for i in range(0, len(data)):\n", " if i == pivot_index:\n", " continue\n", " if data[i] < pivot_value:\n", diff --git a/sorting_searching/selection_sort/selection_sort_solution.ipynb b/sorting_searching/selection_sort/selection_sort_solution.ipynb index 7752580..2ae7778 100644 --- a/sorting_searching/selection_sort/selection_sort_solution.ipynb +++ b/sorting_searching/selection_sort/selection_sort_solution.ipynb @@ -86,7 +86,7 @@ "source": [ "def find_min_index(data, start):\n", " min_index = start\n", - " for i in xrange(start + 1, len(data)):\n", + " for i in range(start + 1, len(data)):\n", " if data[i] < data[min_index]:\n", " min_index = i\n", " return min_index\n", @@ -121,7 +121,7 @@ "def selection_sort_iterative(data):\n", " if len(data) == 0 or len(data) == 1:\n", " return\n", - " for i in xrange(0, len(data) - 1):\n", + " for i in range(0, len(data) - 1):\n", " swap(data, i, find_min_index(data, i))" ] }, diff --git a/stacks_queues/hanoi/hanoi_challenge.ipynb b/stacks_queues/hanoi/hanoi_challenge.ipynb index 09a13c2..51da878 100644 --- a/stacks_queues/hanoi/hanoi_challenge.ipynb +++ b/stacks_queues/hanoi/hanoi_challenge.ipynb @@ -136,10 +136,10 @@ " assert_equal(dest.pop(), 5)\n", "\n", " print('Test: 2 or more disks')\n", - " for i in xrange(num_disks, -1, -1):\n", + " for i in range(num_disks, -1, -1):\n", " src.push(i)\n", " hanoi(num_disks, src, dest, buff)\n", - " for i in xrange(0, num_disks):\n", + " for i in range(0, num_disks):\n", " assert_equal(dest.pop(), i)\n", "\n", " print('Success: test_hanoi')\n", diff --git a/stacks_queues/hanoi/hanoi_solution.ipynb b/stacks_queues/hanoi/hanoi_solution.ipynb index c98c976..884cb22 100644 --- a/stacks_queues/hanoi/hanoi_solution.ipynb +++ b/stacks_queues/hanoi/hanoi_solution.ipynb @@ -153,10 +153,10 @@ " assert_equal(dest.pop(), 5)\n", "\n", " print('Test: 2 or more disks')\n", - " for i in xrange(num_disks, -1, -1):\n", + " for i in range(num_disks, -1, -1):\n", " src.push(i)\n", " hanoi(num_disks, src, dest, buff)\n", - " for i in xrange(0, num_disks):\n", + " for i in range(0, num_disks):\n", " assert_equal(dest.pop(), i)\n", "\n", " print('Success: test_hanoi')\n", diff --git a/stacks_queues/hanoi/test_hanoi.py b/stacks_queues/hanoi/test_hanoi.py index b5a74ed..5a2606d 100644 --- a/stacks_queues/hanoi/test_hanoi.py +++ b/stacks_queues/hanoi/test_hanoi.py @@ -22,10 +22,10 @@ class TestHanoi(object): assert_equal(dest.pop(), 5) print('Test: 2 or more disks') - for i in xrange(num_disks, -1, -1): + for i in range(num_disks, -1, -1): src.push(i) hanoi(num_disks, src, dest, buff) - for i in xrange(0, num_disks): + for i in range(0, num_disks): assert_equal(dest.pop(), i) print('Success: test_hanoi') diff --git a/stacks_queues/n_stacks/n_stacks_challenge.ipynb b/stacks_queues/n_stacks/n_stacks_challenge.ipynb index 9148124..070a88c 100644 --- a/stacks_queues/n_stacks/n_stacks_challenge.ipynb +++ b/stacks_queues/n_stacks/n_stacks_challenge.ipynb @@ -131,7 +131,7 @@ " def test_push_on_full(self, num_stacks, stack_size):\n", " print('Test: Push to full stack')\n", " stacks = Stacks(num_stacks, stack_size)\n", - " for i in xrange(0, stack_size):\n", + " for i in range(0, stack_size):\n", " stacks.push(2, i)\n", " stacks.push(2, stack_size)\n", "\n", diff --git a/stacks_queues/n_stacks/n_stacks_solution.ipynb b/stacks_queues/n_stacks/n_stacks_solution.ipynb index d741bf1..aa1efea 100644 --- a/stacks_queues/n_stacks/n_stacks_solution.ipynb +++ b/stacks_queues/n_stacks/n_stacks_solution.ipynb @@ -177,7 +177,7 @@ " def test_push_on_full(self, num_stacks, stack_size):\n", " print('Test: Push to full stack')\n", " stacks = Stacks(num_stacks, stack_size)\n", - " for i in xrange(0, stack_size):\n", + " for i in range(0, stack_size):\n", " stacks.push(2, i)\n", " stacks.push(2, stack_size)\n", "\n", diff --git a/stacks_queues/n_stacks/test_n_stacks.py b/stacks_queues/n_stacks/test_n_stacks.py index bee6f48..5878d28 100644 --- a/stacks_queues/n_stacks/test_n_stacks.py +++ b/stacks_queues/n_stacks/test_n_stacks.py @@ -3,7 +3,7 @@ from nose.tools import raises class TestStacks(object): - + @raises(Exception) def test_pop_on_empty(self, num_stacks, stack_size): print('Test: Pop on empty stack') @@ -14,7 +14,7 @@ class TestStacks(object): def test_push_on_full(self, num_stacks, stack_size): print('Test: Push to full stack') stacks = Stacks(num_stacks, stack_size) - for i in xrange(0, stack_size): + for i in range(0, stack_size): stacks.push(2, i) stacks.push(2, stack_size) @@ -36,11 +36,11 @@ class TestStacks(object): def main(): num_stacks = 3 - stack_size = 100 + stack_size = 100 test = TestStacks() test.test_pop_on_empty(num_stacks, stack_size) test.test_push_on_full(num_stacks, stack_size) test.test_stacks(num_stacks, stack_size) - + if __name__ == '__main__': main() \ No newline at end of file diff --git a/stacks_queues/sort_stack/sort_stack_challenge.ipynb b/stacks_queues/sort_stack/sort_stack_challenge.ipynb index 41b610d..7b29aa5 100644 --- a/stacks_queues/sort_stack/sort_stack_challenge.ipynb +++ b/stacks_queues/sort_stack/sort_stack_challenge.ipynb @@ -142,10 +142,10 @@ "\n", " print('Test: Two or more element stack (general case)')\n", " num_items = 10\n", - " numbers = [randint(0, 10) for x in xrange(num_items)]\n", + " numbers = [randint(0, 10) for x in range(num_items)]\n", " sorted_stack = self.get_sorted_stack(numbers)\n", " sorted_numbers = []\n", - " for _ in xrange(num_items):\n", + " for _ in range(num_items):\n", " sorted_numbers.append(sorted_stack.pop())\n", " assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n", " \n", diff --git a/stacks_queues/sort_stack/sort_stack_solution.ipynb b/stacks_queues/sort_stack/sort_stack_solution.ipynb index 98dd44f..7ace48a 100644 --- a/stacks_queues/sort_stack/sort_stack_solution.ipynb +++ b/stacks_queues/sort_stack/sort_stack_solution.ipynb @@ -161,10 +161,10 @@ "\n", " print('Test: Two or more element stack (general case)')\n", " num_items = 10\n", - " numbers = [randint(0, 10) for x in xrange(num_items)]\n", + " numbers = [randint(0, 10) for x in range(num_items)]\n", " sorted_stack = self.get_sorted_stack(numbers)\n", " sorted_numbers = []\n", - " for _ in xrange(num_items):\n", + " for _ in range(num_items):\n", " sorted_numbers.append(sorted_stack.pop())\n", " assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n", " \n", diff --git a/stacks_queues/sort_stack/test_sort_stack.py b/stacks_queues/sort_stack/test_sort_stack.py index 3522cc1..4985077 100644 --- a/stacks_queues/sort_stack/test_sort_stack.py +++ b/stacks_queues/sort_stack/test_sort_stack.py @@ -3,7 +3,7 @@ from nose.tools import assert_equal class TestSortStack(object): - + def get_sorted_stack(self, numbers): stack = MyStack() for x in numbers: @@ -22,13 +22,13 @@ class TestSortStack(object): print('Test: Two or more element stack (general case)') num_items = 10 - numbers = [randint(0, 10) for x in xrange(num_items)] + numbers = [randint(0, 10) for x in range(num_items)] sorted_stack = self.get_sorted_stack(numbers) sorted_numbers = [] - for _ in xrange(num_items): + for _ in range(num_items): sorted_numbers.append(sorted_stack.pop()) assert_equal(sorted_numbers, sorted(numbers, reverse=True)) - + print('Success: test_sort_stack') def main():