Merge pull request #96 from donnemartin/develop

Move string challenges and solutions to classes
This commit is contained in:
Donne Martin
2016-08-13 06:46:33 -04:00
committed by GitHub
15 changed files with 202 additions and 152 deletions

View File

@@ -81,7 +81,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def compress_string(string):\n", "class CompressString(object):\n",
"\n",
" def compress(self, string):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
] ]
@@ -120,13 +122,14 @@
" assert_equal(func(None), None)\n", " assert_equal(func(None), None)\n",
" assert_equal(func(''), '')\n", " assert_equal(func(''), '')\n",
" assert_equal(func('AABBCC'), 'AABBCC')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n",
" assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n", " assert_equal(func('AAABCCDDDDE'), 'A3BC2D4E')\n",
" print('Success: test_compress')\n", " print('Success: test_compress')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestCompress()\n", " test = TestCompress()\n",
" test.test_compress(compress_string)\n", " compress_string = CompressString()\n",
" test.test_compress(compress_string.compress)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@@ -97,7 +97,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def compress_string(string):\n", "class CompressString(object):\n",
"\n",
" def compress(self, string):\n",
" if string is None or len(string) == 0:\n", " if string is None or len(string) == 0:\n",
" return string\n", " return string\n",
" result = ''\n", " result = ''\n",
@@ -153,7 +155,8 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestCompress()\n", " test = TestCompress()\n",
" test.test_compress(compress_string)\n", " compress_string = CompressString()\n",
" test.test_compress(compress_string.compress)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@@ -13,7 +13,8 @@ class TestCompress(object):
def main(): def main():
test = TestCompress() test = TestCompress()
test.test_compress(compress_string) compress_string = CompressString()
test.test_compress(compress_string.compress)
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -84,7 +84,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "class Permutations(object):\n",
"\n",
" def is_permutation(self, str1, str2):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
] ]
@@ -130,9 +132,11 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestPermutation()\n", " test = TestPermutation()\n",
" test.test_permutation(permutations)\n", " permutations = Permutations()\n",
" test.test_permutation(permutations.is_permutation)\n",
" try:\n", " try:\n",
" test.test_permutation(permutations_alt)\n", " permutations_alt = PermutationsAlt()\n",
" test.test_permutation(permutations_alt.is_permutation)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",

View File

@@ -95,7 +95,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "class Permutations(object):\n",
"\n",
" def is_permutation(self, str1, str2):\n",
" if str1 is None or str2 is None:\n", " if str1 is None or str2 is None:\n",
" return False\n", " return False\n",
" return sorted(str1) == sorted(str2)" " return sorted(str1) == sorted(str2)"
@@ -147,7 +149,9 @@
"from collections import defaultdict\n", "from collections import defaultdict\n",
"\n", "\n",
"\n", "\n",
"def permutations_alt(str1, str2):\n", "class PermutationsAlt(object):\n",
"\n",
" def is_permutation(self, str1, str2):\n",
" if str1 is None or str2 is None:\n", " if str1 is None or str2 is None:\n",
" return False\n", " return False\n",
" if len(str1) != len(str2):\n", " if len(str1) != len(str2):\n",
@@ -201,9 +205,11 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestPermutation()\n", " test = TestPermutation()\n",
" test.test_permutation(permutations)\n", " permutations = Permutations()\n",
" test.test_permutation(permutations.is_permutation)\n",
" try:\n", " try:\n",
" test.test_permutation(permutations_alt)\n", " permutations_alt = PermutationsAlt()\n",
" test.test_permutation(permutations_alt.is_permutation)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",
@@ -225,6 +231,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: test_permutation\n",
"Success: test_permutation\n" "Success: test_permutation\n"
] ]
} }
@@ -250,7 +257,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.4.3" "version": "3.5.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

View File

@@ -14,9 +14,11 @@ class TestPermutation(object):
def main(): def main():
test = TestPermutation() test = TestPermutation()
test.test_permutation(permutations) permutations = Permutations()
test.test_permutation(permutations.is_permutation)
try: try:
test.test_permutation(permutations_alt) permutations_alt = PermutationsAlt()
test.test_permutation(permutations_alt.is_permutation)
except NameError: except NameError:
# Alternate solutions are only defined # Alternate solutions are only defined
# in the solutions file # in the solutions file

View File

@@ -78,7 +78,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def reverse_string(list_chars):\n", "class ReverseString(object):\n",
"\n",
" def reverse(self, chars):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
] ]
@@ -113,25 +115,26 @@
"\n", "\n",
"class TestReverse(object):\n", "class TestReverse(object):\n",
"\n", "\n",
" def test_reverse(self):\n", " def test_reverse(self, func):\n",
" assert_equal(reverse_string(None), None)\n", " assert_equal(func(None), None)\n",
" assert_equal(reverse_string(['']), [''])\n", " assert_equal(func(['']), [''])\n",
" assert_equal(reverse_string(\n", " assert_equal(func(\n",
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse')\n", " print('Success: test_reverse')\n",
"\n", "\n",
" def test_reverse_inplace(self):\n", " def test_reverse_inplace(self, func):\n",
" target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']\n", " target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']\n",
" reverse_string(target_list)\n", " func(target_list)\n",
" assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse_inplace')\n", " print('Success: test_reverse_inplace')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestReverse()\n", " test = TestReverse()\n",
" test.test_reverse()\n", " reverse_string = ReverseString()\n",
" test.test_reverse_inplace()\n", " test.test_reverse(reverse_string.reverse)\n",
" test.test_reverse_inplace(reverse_string.reverse)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@@ -94,7 +94,9 @@
"from __future__ import division\n", "from __future__ import division\n",
"\n", "\n",
"\n", "\n",
"def reverse_string(chars):\n", "class ReverseString(object):\n",
"\n",
" def reverse(self, chars):\n",
" if chars is None:\n", " if chars is None:\n",
" return None\n", " return None\n",
" size = len(chars)\n", " size = len(chars)\n",
@@ -121,12 +123,13 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"class ReverseStringAlt(object):\n",
"\n",
" def reverse_string_alt(string):\n", " def reverse_string_alt(string):\n",
" if string is None:\n", " if string is None:\n",
" return None\n", " return None\n",
" return string[::-1]\n", " return string[::-1]\n",
"\n", "\n",
"\n",
" def reverse_string_alt2(string):\n", " def reverse_string_alt2(string):\n",
" if string is None:\n", " if string is None:\n",
" return None\n", " return None\n",
@@ -162,25 +165,26 @@
"\n", "\n",
"class TestReverse(object):\n", "class TestReverse(object):\n",
"\n", "\n",
" def test_reverse(self):\n", " def test_reverse(self, func):\n",
" assert_equal(reverse_string(None), None)\n", " assert_equal(func(None), None)\n",
" assert_equal(reverse_string(['']), [''])\n", " assert_equal(func(['']), [''])\n",
" assert_equal(reverse_string(\n", " assert_equal(func(\n",
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse')\n", " print('Success: test_reverse')\n",
"\n", "\n",
" def test_reverse_inplace(self):\n", " def test_reverse_inplace(self, func):\n",
" target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']\n", " target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']\n",
" reverse_string(target_list)\n", " func(target_list)\n",
" assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse_inplace')\n", " print('Success: test_reverse_inplace')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestReverse()\n", " test = TestReverse()\n",
" test.test_reverse()\n", " reverse_string = ReverseString()\n",
" test.test_reverse_inplace()\n", " test.test_reverse(reverse_string.reverse)\n",
" test.test_reverse_inplace(reverse_string.reverse)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@@ -3,25 +3,26 @@ from nose.tools import assert_equal
class TestReverse(object): class TestReverse(object):
def test_reverse(self): def test_reverse(self, func):
assert_equal(reverse_string(None), None) assert_equal(func(None), None)
assert_equal(reverse_string(['']), ['']) assert_equal(func(['']), [''])
assert_equal(reverse_string( assert_equal(func(
['f', 'o', 'o', ' ', 'b', 'a', 'r']), ['f', 'o', 'o', ' ', 'b', 'a', 'r']),
['r', 'a', 'b', ' ', 'o', 'o', 'f']) ['r', 'a', 'b', ' ', 'o', 'o', 'f'])
print('Success: test_reverse') print('Success: test_reverse')
def test_reverse_inplace(self): def test_reverse_inplace(self, func):
target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r'] target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']
reverse_string(target_list) func(target_list)
assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f']) assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])
print('Success: test_reverse_inplace') print('Success: test_reverse_inplace')
def main(): def main():
test = TestReverse() test = TestReverse()
test.test_reverse() reverse_string = ReverseString()
test.test_reverse_inplace() test.test_reverse(reverse_string.reverse)
test.test_reverse_inplace(reverse_string.reverse)
if __name__ == '__main__': if __name__ == '__main__':

View File

@@ -82,12 +82,13 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def is_substring(s1, s2):\n", "class Rotation(object):\n",
"\n",
" def is_substring(self, s1, s2):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass\n", " pass\n",
"\n", "\n",
"\n", " def is_rotation(self, s1, s2):\n",
"def is_rotation(s1, s2):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" # Call is_substring only once\n", " # Call is_substring only once\n",
" pass" " pass"
@@ -124,11 +125,12 @@
"class TestRotation(object):\n", "class TestRotation(object):\n",
"\n", "\n",
" def test_rotation(self):\n", " def test_rotation(self):\n",
" assert_equal(is_rotation('o', 'oo'), False)\n", " rotation = Rotation()\n",
" assert_equal(is_rotation(None, 'foo'), False)\n", " assert_equal(rotation.is_rotation('o', 'oo'), False)\n",
" assert_equal(is_rotation('', 'foo'), False)\n", " assert_equal(rotation.is_rotation(None, 'foo'), False)\n",
" assert_equal(is_rotation('', ''), True)\n", " assert_equal(rotation.is_rotation('', 'foo'), False)\n",
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n", " assert_equal(rotation.is_rotation('', ''), True)\n",
" assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_rotation')\n", " print('Success: test_rotation')\n",
"\n", "\n",
"\n", "\n",

View File

@@ -92,16 +92,17 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def is_substring(s1, s2):\n", "class Rotation(object):\n",
"\n",
" def is_substring(self, s1, s2):\n",
" return s1 in s2\n", " return s1 in s2\n",
"\n", "\n",
"\n", " def is_rotation(self, s1, s2):\n",
"def is_rotation(s1, s2):\n",
" if s1 is None or s2 is None:\n", " if s1 is None or s2 is None:\n",
" return False\n", " return False\n",
" if len(s1) != len(s2):\n", " if len(s1) != len(s2):\n",
" return False\n", " return False\n",
" return is_substring(s1, s2 + s2)" " return self.is_substring(s1, s2 + s2)"
] ]
}, },
{ {
@@ -134,11 +135,12 @@
"class TestRotation(object):\n", "class TestRotation(object):\n",
"\n", "\n",
" def test_rotation(self):\n", " def test_rotation(self):\n",
" assert_equal(is_rotation('o', 'oo'), False)\n", " rotation = Rotation()\n",
" assert_equal(is_rotation(None, 'foo'), False)\n", " assert_equal(rotation.is_rotation('o', 'oo'), False)\n",
" assert_equal(is_rotation('', 'foo'), False)\n", " assert_equal(rotation.is_rotation(None, 'foo'), False)\n",
" assert_equal(is_rotation('', ''), True)\n", " assert_equal(rotation.is_rotation('', 'foo'), False)\n",
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n", " assert_equal(rotation.is_rotation('', ''), True)\n",
" assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_rotation')\n", " print('Success: test_rotation')\n",
"\n", "\n",
"\n", "\n",

View File

@@ -4,11 +4,12 @@ from nose.tools import assert_equal
class TestRotation(object): class TestRotation(object):
def test_rotation(self): def test_rotation(self):
assert_equal(is_rotation('o', 'oo'), False) rotation = Rotation()
assert_equal(is_rotation(None, 'foo'), False) assert_equal(rotation.is_rotation('o', 'oo'), False)
assert_equal(is_rotation('', 'foo'), False) assert_equal(rotation.is_rotation(None, 'foo'), False)
assert_equal(is_rotation('', ''), True) assert_equal(rotation.is_rotation('', 'foo'), False)
assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True) assert_equal(rotation.is_rotation('', ''), True)
assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)
print('Success: test_rotation') print('Success: test_rotation')

View File

@@ -13,10 +13,13 @@ class TestUniqueChars(object):
def main(): def main():
test = TestUniqueChars() test = TestUniqueChars()
test.test_unique_chars(unique_chars) unique_chars = UniqueChars()
test.test_unique_chars(unique_chars.has_unique_chars)
try: try:
test.test_unique_chars(unique_chars_hash) unique_chars_set = UniqueCharsSet()
test.test_unique_chars(unique_chars_inplace) test.test_unique_chars(unique_chars_set.has_unique_chars)
unique_chars_in_place = UniqueCharsInPlace()
test.test_unique_chars(unique_chars_in_place.has_unique_chars)
except NameError: except NameError:
# Alternate solutions are only defined # Alternate solutions are only defined
# in the solutions file # in the solutions file

View File

@@ -81,7 +81,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "class UniqueChars(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
] ]
@@ -124,10 +126,13 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestUniqueChars()\n", " test = TestUniqueChars()\n",
" test.test_unique_chars(unique_chars)\n", " unique_chars = UniqueChars()\n",
" test.test_unique_chars(unique_chars.has_unique_chars)\n",
" try:\n", " try:\n",
" test.test_unique_chars(unique_chars_hash)\n", " unique_chars_set = UniqueCharsSet()\n",
" test.test_unique_chars(unique_chars_inplace)\n", " test.test_unique_chars(unique_chars_set.has_unique_chars)\n",
" unique_chars_in_place = UniqueCharsInPlace()\n",
" test.test_unique_chars(unique_chars_in_place.has_unique_chars)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",

View File

@@ -93,7 +93,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "class UniqueCharsSet(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" if string is None:\n", " if string is None:\n",
" return False\n", " return False\n",
" return len(set(string)) == len(string)" " return len(set(string)) == len(string)"
@@ -138,7 +140,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars_hash(string):\n", "class UniqueChars(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" if string is None:\n", " if string is None:\n",
" return False\n", " return False\n",
" chars_set = set()\n", " chars_set = set()\n",
@@ -184,7 +188,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars_inplace(string):\n", "class UniqueCharsInPlace(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" if string is None:\n", " if string is None:\n",
" return False\n", " return False\n",
" for char in string:\n", " for char in string:\n",
@@ -232,10 +238,13 @@
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestUniqueChars()\n", " test = TestUniqueChars()\n",
" test.test_unique_chars(unique_chars)\n", " unique_chars = UniqueChars()\n",
" test.test_unique_chars(unique_chars.has_unique_chars)\n",
" try:\n", " try:\n",
" test.test_unique_chars(unique_chars_hash)\n", " unique_chars_set = UniqueCharsSet()\n",
" test.test_unique_chars(unique_chars_inplace)\n", " test.test_unique_chars(unique_chars_set.has_unique_chars)\n",
" unique_chars_in_place = UniqueCharsInPlace()\n",
" test.test_unique_chars(unique_chars_in_place.has_unique_chars)\n",
" except NameError:\n", " except NameError:\n",
" # Alternate solutions are only defined\n", " # Alternate solutions are only defined\n",
" # in the solutions file\n", " # in the solutions file\n",