Fix #13, PEP8-ify notebooks.

This commit is contained in:
Donne Martin 2015-07-11 15:34:14 -04:00
parent 27c4a4f97c
commit 374d67ff30
18 changed files with 117 additions and 80 deletions

View File

@ -113,7 +113,7 @@
"\n",
"\n",
"class TestCompress(object):\n",
" \n",
"\n",
" def test_compress(self, func):\n",
" assert_equal(func(None), None)\n",
" assert_equal(func(''), '')\n",
@ -121,10 +121,12 @@
" assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n",
" print('Success: test_compress')\n",
"\n",
"\n",
"def main():\n",
" test = TestCompress()\n",
" test.test_compress(compress_string)\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -100,7 +100,7 @@
"def compress_string(string):\n",
" if string is None or len(string) == 0:\n",
" return string\n",
" \n",
"\n",
" # Calculate the size of the compressed string\n",
" size = 0\n",
" last_char = string[0]\n",
@ -109,8 +109,8 @@
" size += 2\n",
" last_char = char\n",
" size += 2\n",
" \n",
" # If the compressed string size is greater than \n",
"\n",
" # If the compressed string size is greater than\n",
" # or equal to string size, return original string\n",
" if size >= len(string):\n",
" return string\n",
@ -129,7 +129,7 @@
" last_char = char\n",
" compressed_string.append(last_char)\n",
" compressed_string.append(str(count))\n",
" \n",
"\n",
" # Convert the characters in the list to a string\n",
" return \"\".join(compressed_string)"
]
@ -162,7 +162,7 @@
"\n",
"\n",
"class TestCompress(object):\n",
" \n",
"\n",
" def test_compress(self, func):\n",
" assert_equal(func(None), None)\n",
" assert_equal(func(''), '')\n",
@ -170,10 +170,12 @@
" assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')\n",
" print('Success: test_compress')\n",
"\n",
"\n",
"def main():\n",
" test = TestCompress()\n",
" test.test_compress(compress_string)\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -10,9 +10,11 @@ class TestCompress(object):
assert_equal(func('AAABCCDDDD'), 'A3B1C2D4')
print('Success: test_compress')
def main():
test = TestCompress()
test.test_compress(compress_string)
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* For simplicity, are the keys integers only?\n",
" * Yes\n",
"* For collision resolution, can we use linked lists?\n",
@ -82,13 +81,14 @@
"outputs": [],
"source": [
"class Item(object):\n",
" \n",
"\n",
" def __init__(self, key, value):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
"\n",
"class HashTable(object):\n",
" \n",
"\n",
" def __init__(self, size):\n",
" # TODO: Implement me\n",
" pass\n",
@ -139,45 +139,47 @@
"\n",
"\n",
"class TestHashMap(object):\n",
" \n",
"\n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
" hash_table = HashTable(10)\n",
" \n",
"\n",
" print(\"Test: get on an empty hash table index\")\n",
" assert_equal(hash_table.get(0), None)\n",
" \n",
"\n",
" print(\"Test: set on an empty hash table index\")\n",
" hash_table.set(0, 'foo')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" hash_table.set(1, 'bar')\n",
" assert_equal(hash_table.get(1), 'bar')\n",
" \n",
"\n",
" print(\"Test: set on a non empty hash table index\")\n",
" hash_table.set(10, 'foo2')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), 'foo2')\n",
" \n",
"\n",
" print(\"Test: set on a key that already exists\")\n",
" hash_table.set(10, 'foo3')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), 'foo3')\n",
" \n",
"\n",
" print(\"Test: remove on a key that already exists\")\n",
" hash_table.remove(10)\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), None)\n",
" \n",
"\n",
" print(\"Test: remove on a key that doesn't exist\")\n",
" hash_table.remove(-1)\n",
" \n",
"\n",
" print('Success: test_end_to_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestHashMap()\n",
" test.test_end_to_end()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -33,7 +33,6 @@
"source": [
"## Constraints\n",
"\n",
"* For simplicity, are the keys integers only?\n",
" * Yes\n",
"* For collision resolution, can we use linked lists?\n",
@ -116,13 +115,14 @@
"outputs": [],
"source": [
"class Item(object):\n",
" \n",
"\n",
" def __init__(self, key, value):\n",
" self.key = key\n",
" self.value = value\n",
"\n",
"\n",
"class HashTable(object):\n",
" \n",
"\n",
" def __init__(self, size):\n",
" self.size = size\n",
" self.table = [[] for _ in range(self.size)]\n",
@ -180,45 +180,47 @@
"\n",
"\n",
"class TestHashMap(object):\n",
" \n",
"\n",
" # TODO: It would be better if we had unit tests for each\n",
" # method in addition to the following end-to-end test\n",
" def test_end_to_end(self):\n",
" hash_table = HashTable(10)\n",
" \n",
"\n",
" print(\"Test: get on an empty hash table index\")\n",
" assert_equal(hash_table.get(0), None)\n",
" \n",
"\n",
" print(\"Test: set on an empty hash table index\")\n",
" hash_table.set(0, 'foo')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" hash_table.set(1, 'bar')\n",
" assert_equal(hash_table.get(1), 'bar')\n",
" \n",
"\n",
" print(\"Test: set on a non empty hash table index\")\n",
" hash_table.set(10, 'foo2')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), 'foo2')\n",
" \n",
"\n",
" print(\"Test: set on a key that already exists\")\n",
" hash_table.set(10, 'foo3')\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), 'foo3')\n",
" \n",
"\n",
" print(\"Test: remove on a key that already exists\")\n",
" hash_table.remove(10)\n",
" assert_equal(hash_table.get(0), 'foo')\n",
" assert_equal(hash_table.get(10), None)\n",
" \n",
"\n",
" print(\"Test: remove on a key that doesn't exist\")\n",
" hash_table.remove(-1)\n",
" \n",
"\n",
" print('Success: test_end_to_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestHashMap()\n",
" test.test_end_to_end()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -37,9 +37,11 @@ class TestHashMap(object):
print('Success: test_end_to_end')
def main():
test = TestHashMap()
test.test_end_to_end()
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can we assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
@ -114,7 +113,7 @@
"\n",
"\n",
"class TestPermutation(object):\n",
" \n",
"\n",
" def test_permutation(self, func):\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
@ -122,6 +121,7 @@
" assert_equal(func('a ct', 'ca t'), True)\n",
" print('Success: test_permutation')\n",
"\n",
"\n",
"def main():\n",
" test = TestPermutation()\n",
" test.test_permutation(permutations)\n",
@ -131,7 +131,8 @@
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
" pass\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -35,7 +35,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can we assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
@ -147,6 +146,7 @@
" dict_chars[char] += 1\n",
" return dict_chars\n",
"\n",
"\n",
"def permutations_alt(str1, str2):\n",
" if len(str1) != len(str2):\n",
" return False\n",
@ -183,7 +183,7 @@
"\n",
"\n",
"class TestPermutation(object):\n",
" \n",
"\n",
" def test_permutation(self, func):\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('Nib', 'bin'), False)\n",
@ -191,6 +191,7 @@
" assert_equal(func('a ct', 'ca t'), True)\n",
" print('Success: test_permutation')\n",
"\n",
"\n",
"def main():\n",
" test = TestPermutation()\n",
" test.test_permutation(permutations)\n",
@ -200,7 +201,8 @@
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
" pass\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -10,6 +10,7 @@ class TestPermutation(object):
assert_equal(func('a ct', 'ca t'), True)
print('Success: test_permutation')
def main():
test = TestPermutation()
test.test_permutation(permutations)
@ -20,5 +21,6 @@ def main():
# in the solutions file
pass
if __name__ == '__main__':
main()

View File

@ -34,7 +34,6 @@
"source": [
"## Constraints\n",
"\n",
"* Can I assume the string is ASCII?\n",
" * Yes\n",
" * Note: Unicode strings could require special handling depending on your language\n",
@ -113,18 +112,21 @@
"\n",
"\n",
"class TestReverse(object):\n",
" \n",
"\n",
" def test_reverse(self):\n",
" assert_equal(list_of_chars(None), None)\n",
" assert_equal(list_of_chars(['']), [''])\n",
" assert_equal(list_of_chars(['f', 'o', 'o', ' ', 'b', 'a', 'r']), \n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" assert_equal(list_of_chars(\n",
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse')\n",
"\n",
"\n",
"def main():\n",
" test = TestReverse()\n",
" test.test_reverse()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -126,9 +126,10 @@
" return None\n",
" return string[::-1]\n",
"\n",
"\n",
"def reverse_string_alt2(string):\n",
" if string is None:\n",
" return None \n",
" return None\n",
" return ''.join(reversed(string))"
]
},
@ -160,18 +161,21 @@
"\n",
"\n",
"class TestReverse(object):\n",
" \n",
"\n",
" def test_reverse(self):\n",
" assert_equal(list_of_chars(None), None)\n",
" assert_equal(list_of_chars(['']), [''])\n",
" assert_equal(list_of_chars(['f', 'o', 'o', ' ', 'b', 'a', 'r']), \n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" assert_equal(list_of_chars(\n",
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse')\n",
"\n",
"\n",
"def main():\n",
" test = TestReverse()\n",
" test.test_reverse()\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -6,13 +6,16 @@ class TestReverse(object):
def test_reverse(self):
assert_equal(list_of_chars(None), None)
assert_equal(list_of_chars(['']), [''])
assert_equal(list_of_chars(['f', 'o', 'o', ' ', 'b', 'a', 'r']),
['r', 'a', 'b', ' ', 'o', 'o', 'f'])
assert_equal(list_of_chars(
['f', 'o', 'o', ' ', 'b', 'a', 'r']),
['r', 'a', 'b', ' ', 'o', 'o', 'f'])
print('Success: test_reverse')
def main():
test = TestReverse()
test.test_reverse()
if __name__ == '__main__':
main()

View File

@ -34,7 +34,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",
@ -85,6 +84,7 @@
" # TODO: Implement me\n",
" pass\n",
"\n",
"\n",
"def is_rotation(s1, s2):\n",
" # TODO: Implement me\n",
" # Call is_substring only once\n",
@ -120,7 +120,7 @@
"\n",
"\n",
"class TestRotation(object):\n",
" \n",
"\n",
" def test_rotation(self):\n",
" assert_equal(is_rotation('o', 'oo'), False)\n",
" assert_equal(is_rotation(None, 'foo'), False)\n",
@ -129,10 +129,12 @@
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_rotation')\n",
"\n",
"\n",
"def main():\n",
" test = TestRotation()\n",
" test.test_rotation()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -34,7 +34,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",
@ -91,9 +90,10 @@
},
"outputs": [],
"source": [
"def is_substring(s1, s2): \n",
"def is_substring(s1, s2):\n",
" return s1 in s2\n",
"\n",
"\n",
"def is_rotation(s1, s2):\n",
" if s1 is None or s2 is None:\n",
" return False\n",
@ -131,7 +131,7 @@
"\n",
"\n",
"class TestRotation(object):\n",
" \n",
"\n",
" def test_rotation(self):\n",
" assert_equal(is_rotation('o', 'oo'), False)\n",
" assert_equal(is_rotation(None, 'foo'), False)\n",
@ -140,10 +140,12 @@
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_rotation')\n",
"\n",
"\n",
"def main():\n",
" test = TestRotation()\n",
" test.test_rotation()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -11,9 +11,11 @@ class TestRotation(object):
assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)
print('Success: test_rotation')
def main():
test = TestRotation()
test.test_rotation()
if __name__ == '__main__':
main()

View File

@ -3,12 +3,13 @@ from nose.tools import assert_equal
class TestUniqueChars(object):
def test_unique_chars(self, func):
def test_unique_chars(self, func):
assert_equal(func(''), True)
assert_equal(func('foo'), False)
assert_equal(func('bar'), True)
print('Success: test_unique_chars')
def main():
test = TestUniqueChars()
test.test_unique_chars(unique_chars)
@ -20,5 +21,6 @@ def main():
# in the solutions file
pass
if __name__ == '__main__':
main()

View File

@ -34,7 +34,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",
@ -112,12 +111,13 @@
"\n",
"class TestUniqueChars(object):\n",
"\n",
" def test_unique_chars(self, func):\n",
" def test_unique_chars(self, func):\n",
" assert_equal(func(''), True)\n",
" assert_equal(func('foo'), False)\n",
" assert_equal(func('bar'), True)\n",
" print('Success: test_unique_chars')\n",
"\n",
"\n",
"def main():\n",
" test = TestUniqueChars()\n",
" test.test_unique_chars(unique_chars)\n",
@ -128,7 +128,8 @@
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
" pass\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]

View File

@ -213,12 +213,13 @@
"\n",
"class TestUniqueChars(object):\n",
"\n",
" def test_unique_chars(self, func):\n",
" def test_unique_chars(self, func):\n",
" assert_equal(func(''), True)\n",
" assert_equal(func('foo'), False)\n",
" assert_equal(func('bar'), True)\n",
" print('Success: test_unique_chars')\n",
"\n",
"\n",
"def main():\n",
" test = TestUniqueChars()\n",
" test.test_unique_chars(unique_chars)\n",
@ -229,7 +230,8 @@
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
" pass\n",
" \n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
@ -258,21 +260,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 2",
"language": "python",
"name": "python3"
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,