mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Changed xrange to range to be compatible with Python 3.
This commit is contained in:
parent
fac7bbb1be
commit
45fa576bc2
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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))"
|
||||
]
|
||||
},
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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()
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Reference in New Issue
Block a user