Changed xrange to range to be compatible with Python 3.

This commit is contained in:
Donne Martin 2015-07-06 06:15:53 -04:00
parent fac7bbb1be
commit 45fa576bc2
21 changed files with 41 additions and 42 deletions

View File

@ -125,7 +125,7 @@
" \n", " \n",
" def __init__(self, size):\n", " def __init__(self, size):\n",
" self.size = size\n", " self.size = size\n",
" self.table = [[] for _ in xrange(self.size)]\n", " self.table = [[] for _ in range(self.size)]\n",
"\n", "\n",
" def hash_function(self, key):\n", " def hash_function(self, key):\n",
" return key % self.size\n", " return key % self.size\n",

View File

@ -96,7 +96,7 @@
" if chars is None:\n", " if chars is None:\n",
" return None\n", " return None\n",
" size = len(chars)\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[i], chars[size-1-i] = \\\n",
" chars[size-1-i], chars[i]\n", " chars[size-1-i], chars[i]\n",
" return chars" " return chars"

View File

@ -37,7 +37,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can you assume the string is ASCII?\n", "* Can you assume the string is ASCII?\n",
" * Yes\n", " * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n", " * Note: Unicode strings could require special handling depending on your language\n",
@ -259,21 +258,21 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 2", "display_name": "Python 3",
"language": "python", "language": "python",
"name": "python2" "name": "python3"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 2 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython2", "pygments_lexer": "ipython3",
"version": "2.7.10" "version": "3.4.3"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@ -120,7 +120,7 @@
" iterations_to_compare_half = length / 2\n", " iterations_to_compare_half = length / 2\n",
" curr = self.head\n", " curr = self.head\n",
" curr_reversed = reversed_list.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", " if curr.data != curr_reversed.data:\n",
" return False\n", " return False\n",
" curr = curr.next\n", " curr = curr.next\n",

View File

@ -82,8 +82,8 @@
"source": [ "source": [
"def max_xor(lower, upper):\n", "def max_xor(lower, upper):\n",
" result = 0\n", " result = 0\n",
" for l in xrange(lower, upper + 1):\n", " for l in range(lower, upper + 1):\n",
" for u in xrange(lower, upper + 1):\n", " for u in range(lower, upper + 1):\n",
" curr = l ^ u\n", " curr = l ^ u\n",
" if result < curr:\n", " if result < curr:\n",
" result = curr\n", " result = curr\n",

View File

@ -87,7 +87,7 @@
" height = 1\n", " height = 1\n",
" if cycles == 0:\n", " if cycles == 0:\n",
" return height\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", " if i % 2 == 1:\n",
" height *= 2\n", " height *= 2\n",
" else:\n", " else:\n",

View File

@ -134,7 +134,7 @@
" \n", " \n",
" def test_fib(self, func):\n", " def test_fib(self, func):\n",
" result = []\n", " result = []\n",
" for i in xrange(num_items):\n", " for i in range(num_items):\n",
" result.append(func(i))\n", " result.append(func(i))\n",
" fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", " fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
" assert_equal(result, fib_seq)\n", " assert_equal(result, fib_seq)\n",

View File

@ -117,7 +117,7 @@
"def fib_iterative(n):\n", "def fib_iterative(n):\n",
" a = 0 \n", " a = 0 \n",
" b = 1\n", " b = 1\n",
" for _ in xrange(n):\n", " for _ in range(n):\n",
" a, b = b, a + b\n", " a, b = b, a + b\n",
" return a" " return a"
] ]
@ -154,7 +154,7 @@
" \n", " \n",
" def test_fib(self, func):\n", " def test_fib(self, func):\n",
" result = []\n", " result = []\n",
" for i in xrange(num_items):\n", " for i in range(num_items):\n",
" result.append(func(i))\n", " result.append(func(i))\n",
" fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n", " fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
" assert_equal(result, fib_seq)\n", " assert_equal(result, fib_seq)\n",

View File

@ -5,7 +5,7 @@ class TestFib(object):
def test_fib(self, func): def test_fib(self, func):
result = [] result = []
for i in xrange(num_items): for i in range(num_items):
result.append(func(i)) result.append(func(i))
fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] fib_seq = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
assert_equal(result, fib_seq) assert_equal(result, fib_seq)

View File

@ -88,8 +88,8 @@
"def insertion_sort(data):\n", "def insertion_sort(data):\n",
" if len(data) < 2:\n", " if len(data) < 2:\n",
" return\n", " return\n",
" for r in xrange(1, len(data)):\n", " for r in range(1, len(data)):\n",
" for l in xrange(0, r):\n", " for l in range(0, r):\n",
" if data[l] > data[r]:\n", " if data[l] > data[r]:\n",
" temp = data[r]\n", " temp = data[r]\n",
" data[l+1:r+1] = data[l:r]\n", " data[l+1:r+1] = data[l:r]\n",

View File

@ -97,7 +97,7 @@
" pivot_value = data[pivot_index]\n", " pivot_value = data[pivot_index]\n",
" \n", " \n",
" # Build the left and right partitions\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", " if i == pivot_index:\n",
" continue\n", " continue\n",
" if data[i] < pivot_value:\n", " if data[i] < pivot_value:\n",

View File

@ -86,7 +86,7 @@
"source": [ "source": [
"def find_min_index(data, start):\n", "def find_min_index(data, start):\n",
" min_index = 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", " if data[i] < data[min_index]:\n",
" min_index = i\n", " min_index = i\n",
" return min_index\n", " return min_index\n",
@ -121,7 +121,7 @@
"def selection_sort_iterative(data):\n", "def selection_sort_iterative(data):\n",
" if len(data) == 0 or len(data) == 1:\n", " if len(data) == 0 or len(data) == 1:\n",
" return\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))" " swap(data, i, find_min_index(data, i))"
] ]
}, },

View File

@ -136,10 +136,10 @@
" assert_equal(dest.pop(), 5)\n", " assert_equal(dest.pop(), 5)\n",
"\n", "\n",
" print('Test: 2 or more disks')\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", " src.push(i)\n",
" hanoi(num_disks, src, dest, buff)\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", " assert_equal(dest.pop(), i)\n",
"\n", "\n",
" print('Success: test_hanoi')\n", " print('Success: test_hanoi')\n",

View File

@ -153,10 +153,10 @@
" assert_equal(dest.pop(), 5)\n", " assert_equal(dest.pop(), 5)\n",
"\n", "\n",
" print('Test: 2 or more disks')\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", " src.push(i)\n",
" hanoi(num_disks, src, dest, buff)\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", " assert_equal(dest.pop(), i)\n",
"\n", "\n",
" print('Success: test_hanoi')\n", " print('Success: test_hanoi')\n",

View File

@ -22,10 +22,10 @@ class TestHanoi(object):
assert_equal(dest.pop(), 5) assert_equal(dest.pop(), 5)
print('Test: 2 or more disks') 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) src.push(i)
hanoi(num_disks, src, dest, buff) 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) assert_equal(dest.pop(), i)
print('Success: test_hanoi') print('Success: test_hanoi')

View File

@ -131,7 +131,7 @@
" def test_push_on_full(self, num_stacks, stack_size):\n", " def test_push_on_full(self, num_stacks, stack_size):\n",
" print('Test: Push to full stack')\n", " print('Test: Push to full stack')\n",
" stacks = Stacks(num_stacks, stack_size)\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, i)\n",
" stacks.push(2, stack_size)\n", " stacks.push(2, stack_size)\n",
"\n", "\n",

View File

@ -177,7 +177,7 @@
" def test_push_on_full(self, num_stacks, stack_size):\n", " def test_push_on_full(self, num_stacks, stack_size):\n",
" print('Test: Push to full stack')\n", " print('Test: Push to full stack')\n",
" stacks = Stacks(num_stacks, stack_size)\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, i)\n",
" stacks.push(2, stack_size)\n", " stacks.push(2, stack_size)\n",
"\n", "\n",

View File

@ -14,7 +14,7 @@ class TestStacks(object):
def test_push_on_full(self, num_stacks, stack_size): def test_push_on_full(self, num_stacks, stack_size):
print('Test: Push to full stack') print('Test: Push to full stack')
stacks = Stacks(num_stacks, stack_size) 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, i)
stacks.push(2, stack_size) stacks.push(2, stack_size)

View File

@ -142,10 +142,10 @@
"\n", "\n",
" print('Test: Two or more element stack (general case)')\n", " print('Test: Two or more element stack (general case)')\n",
" num_items = 10\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_stack = self.get_sorted_stack(numbers)\n",
" sorted_numbers = []\n", " sorted_numbers = []\n",
" for _ in xrange(num_items):\n", " for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n", " sorted_numbers.append(sorted_stack.pop())\n",
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n", " assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
" \n", " \n",

View File

@ -161,10 +161,10 @@
"\n", "\n",
" print('Test: Two or more element stack (general case)')\n", " print('Test: Two or more element stack (general case)')\n",
" num_items = 10\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_stack = self.get_sorted_stack(numbers)\n",
" sorted_numbers = []\n", " sorted_numbers = []\n",
" for _ in xrange(num_items):\n", " for _ in range(num_items):\n",
" sorted_numbers.append(sorted_stack.pop())\n", " sorted_numbers.append(sorted_stack.pop())\n",
" assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n", " assert_equal(sorted_numbers, sorted(numbers, reverse=True))\n",
" \n", " \n",

View File

@ -22,10 +22,10 @@ class TestSortStack(object):
print('Test: Two or more element stack (general case)') print('Test: Two or more element stack (general case)')
num_items = 10 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_stack = self.get_sorted_stack(numbers)
sorted_numbers = [] sorted_numbers = []
for _ in xrange(num_items): for _ in range(num_items):
sorted_numbers.append(sorted_stack.pop()) sorted_numbers.append(sorted_stack.pop())
assert_equal(sorted_numbers, sorted(numbers, reverse=True)) assert_equal(sorted_numbers, sorted(numbers, reverse=True))