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,9 +81,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def compress_string(string):\n", "class CompressString(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def compress(self, string):\n",
" # TODO: Implement me\n",
" 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,21 +97,23 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def compress_string(string):\n", "class CompressString(object):\n",
" if string is None or len(string) == 0:\n", "\n",
" return string\n", " def compress(self, string):\n",
" result = ''\n", " if string is None or len(string) == 0:\n",
" prev_char = string[0]\n", " return string\n",
" count = 0\n", " result = ''\n",
" for char in string:\n", " prev_char = string[0]\n",
" if char == prev_char:\n", " count = 0\n",
" count += 1\n", " for char in string:\n",
" else:\n", " if char == prev_char:\n",
" result += prev_char + (str(count) if count > 1 else '')\n", " count += 1\n",
" prev_char = char\n", " else:\n",
" count = 1\n", " result += prev_char + (str(count) if count > 1 else '')\n",
" result += prev_char + (str(count) if count > 1 else '')\n", " prev_char = char\n",
" return result if len(result) < len(string) else string" " count = 1\n",
" result += prev_char + (str(count) if count > 1 else '')\n",
" return result if len(result) < len(string) else string"
] ]
}, },
{ {
@@ -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,9 +84,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "class Permutations(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def is_permutation(self, str1, str2):\n",
" # TODO: Implement me\n",
" 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,10 +95,12 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def permutations(str1, str2):\n", "class Permutations(object):\n",
" if str1 is None or str2 is None:\n", "\n",
" return False\n", " def is_permutation(self, str1, str2):\n",
" return sorted(str1) == sorted(str2)" " if str1 is None or str2 is None:\n",
" return False\n",
" return sorted(str1) == sorted(str2)"
] ]
}, },
{ {
@@ -147,18 +149,20 @@
"from collections import defaultdict\n", "from collections import defaultdict\n",
"\n", "\n",
"\n", "\n",
"def permutations_alt(str1, str2):\n", "class PermutationsAlt(object):\n",
" if str1 is None or str2 is None:\n", "\n",
" return False\n", " def is_permutation(self, str1, str2):\n",
" if len(str1) != len(str2):\n", " if str1 is None or str2 is None:\n",
" return False\n", " return False\n",
" unique_counts1 = defaultdict(int)\n", " if len(str1) != len(str2):\n",
" unique_counts2 = defaultdict(int)\n", " return False\n",
" for char in str1:\n", " unique_counts1 = defaultdict(int)\n",
" unique_counts1[char] += 1\n", " unique_counts2 = defaultdict(int)\n",
" for char in str2:\n", " for char in str1:\n",
" unique_counts2[char] += 1\n", " unique_counts1[char] += 1\n",
" return unique_counts1 == unique_counts2" " for char in str2:\n",
" unique_counts2[char] += 1\n",
" return unique_counts1 == unique_counts2"
] ]
}, },
{ {
@@ -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,9 +78,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def reverse_string(list_chars):\n", "class ReverseString(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def reverse(self, chars):\n",
" # TODO: Implement me\n",
" 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,14 +94,16 @@
"from __future__ import division\n", "from __future__ import division\n",
"\n", "\n",
"\n", "\n",
"def reverse_string(chars):\n", "class ReverseString(object):\n",
" if chars is None:\n", "\n",
" return None\n", " def reverse(self, chars):\n",
" size = len(chars)\n", " if chars is None:\n",
" for i in range(size//2):\n", " return None\n",
" chars[i], chars[size-1-i] = \\\n", " size = len(chars)\n",
" chars[size-1-i], chars[i]\n", " for i in range(size//2):\n",
" return chars" " chars[i], chars[size-1-i] = \\\n",
" chars[size-1-i], chars[i]\n",
" return chars"
] ]
}, },
{ {
@@ -121,16 +123,17 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def reverse_string_alt(string):\n", "class ReverseStringAlt(object):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\n",
"\n", "\n",
" def reverse_string_alt(string):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\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",
" return ''.join(reversed(string))" " return ''.join(reversed(string))"
] ]
}, },
{ {
@@ -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,15 +82,16 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def is_substring(s1, s2):\n", "class Rotation(object):\n",
" # TODO: Implement me\n",
" pass\n",
"\n", "\n",
" def is_substring(self, s1, s2):\n",
" # TODO: Implement me\n",
" pass\n",
"\n", "\n",
"def is_rotation(s1, s2):\n", " def is_rotation(self, 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",
" return s1 in s2\n",
"\n", "\n",
" def is_substring(self, s1, s2):\n",
" return s1 in s2\n",
"\n", "\n",
"def is_rotation(s1, s2):\n", " def is_rotation(self, 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,9 +81,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "class UniqueChars(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def has_unique_chars(self, string):\n",
" # TODO: Implement me\n",
" 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,10 +93,12 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars(string):\n", "class UniqueCharsSet(object):\n",
" if string is None:\n", "\n",
" return False\n", " def has_unique_chars(self, string):\n",
" return len(set(string)) == len(string)" " if string is None:\n",
" return False\n",
" return len(set(string)) == len(string)"
] ]
}, },
{ {
@@ -138,16 +140,18 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars_hash(string):\n", "class UniqueChars(object):\n",
" if string is None:\n", "\n",
" return False\n", " def has_unique_chars(self, string):\n",
" chars_set = set()\n", " if string is None:\n",
" for char in string:\n",
" if char in chars_set:\n",
" return False\n", " return False\n",
" else:\n", " chars_set = set()\n",
" chars_set.add(char)\n", " for char in string:\n",
" return True" " if char in chars_set:\n",
" return False\n",
" else:\n",
" chars_set.add(char)\n",
" return True"
] ]
}, },
{ {
@@ -184,13 +188,15 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def unique_chars_inplace(string):\n", "class UniqueCharsInPlace(object):\n",
" if string is None:\n", "\n",
" return False\n", " def has_unique_chars(self, string):\n",
" for char in string:\n", " if string is None:\n",
" if string.count(char) > 1:\n",
" return False\n", " return False\n",
" return True" " for char in string:\n",
" if string.count(char) > 1:\n",
" return False\n",
" return True"
] ]
}, },
{ {
@@ -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",