From aa887db2e0131360a52707da8c61cc856cde0732 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:37:48 -0400 Subject: [PATCH 1/5] Move string compress to a class, update test --- .../compress/compress_challenge.ipynb | 13 ++++--- .../compress/compress_solution.ipynb | 35 ++++++++++--------- arrays_strings/compress/test_compress.py | 3 +- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/arrays_strings/compress/compress_challenge.ipynb b/arrays_strings/compress/compress_challenge.ipynb index b3ecd66..2eb0f8f 100644 --- a/arrays_strings/compress/compress_challenge.ipynb +++ b/arrays_strings/compress/compress_challenge.ipynb @@ -81,9 +81,11 @@ }, "outputs": [], "source": [ - "def compress_string(string):\n", - " # TODO: Implement me\n", - " pass" + "class CompressString(object):\n", + "\n", + " def compress(self, string):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -120,13 +122,14 @@ " assert_equal(func(None), None)\n", " assert_equal(func(''), '')\n", " assert_equal(func('AABBCC'), 'AABBCC')\n", - " assert_equal(func('AAABCCDDDD'), 'A3BC2D4')\n", + " assert_equal(func('AAABCCDDDDE'), 'A3BC2D4E')\n", " print('Success: test_compress')\n", "\n", "\n", "def main():\n", " test = TestCompress()\n", - " test.test_compress(compress_string)\n", + " compress_string = CompressString()\n", + " test.test_compress(compress_string.compress)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/arrays_strings/compress/compress_solution.ipynb b/arrays_strings/compress/compress_solution.ipynb index a19e4ab..781d949 100644 --- a/arrays_strings/compress/compress_solution.ipynb +++ b/arrays_strings/compress/compress_solution.ipynb @@ -97,21 +97,23 @@ }, "outputs": [], "source": [ - "def compress_string(string):\n", - " if string is None or len(string) == 0:\n", - " return string\n", - " result = ''\n", - " prev_char = string[0]\n", - " count = 0\n", - " for char in string:\n", - " if char == prev_char:\n", - " count += 1\n", - " else:\n", - " result += prev_char + (str(count) if count > 1 else '')\n", - " prev_char = char\n", - " count = 1\n", - " result += prev_char + (str(count) if count > 1 else '')\n", - " return result if len(result) < len(string) else string" + "class CompressString(object):\n", + "\n", + " def compress(self, string):\n", + " if string is None or len(string) == 0:\n", + " return string\n", + " result = ''\n", + " prev_char = string[0]\n", + " count = 0\n", + " for char in string:\n", + " if char == prev_char:\n", + " count += 1\n", + " else:\n", + " result += prev_char + (str(count) if count > 1 else '')\n", + " prev_char = char\n", + " 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", "def main():\n", " test = TestCompress()\n", - " test.test_compress(compress_string)\n", + " compress_string = CompressString()\n", + " test.test_compress(compress_string.compress)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/arrays_strings/compress/test_compress.py b/arrays_strings/compress/test_compress.py index 0b1350e..e0828d6 100644 --- a/arrays_strings/compress/test_compress.py +++ b/arrays_strings/compress/test_compress.py @@ -13,7 +13,8 @@ class TestCompress(object): def main(): test = TestCompress() - test.test_compress(compress_string) + compress_string = CompressString() + test.test_compress(compress_string.compress) if __name__ == '__main__': From 22e3549d7d602e0eac84035e74a2c6c4b8519da6 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:39:19 -0400 Subject: [PATCH 2/5] Move string permutation to a class --- .../permutation/permutation_challenge.ipynb | 14 +++--- .../permutation/permutation_solution.ipynb | 45 +++++++++++-------- .../permutation/test_permutation_solution.py | 6 ++- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/arrays_strings/permutation/permutation_challenge.ipynb b/arrays_strings/permutation/permutation_challenge.ipynb index 7799abe..4125371 100644 --- a/arrays_strings/permutation/permutation_challenge.ipynb +++ b/arrays_strings/permutation/permutation_challenge.ipynb @@ -84,9 +84,11 @@ }, "outputs": [], "source": [ - "def permutations(str1, str2):\n", - " # TODO: Implement me\n", - " pass" + "class Permutations(object):\n", + "\n", + " def is_permutation(self, str1, str2):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -130,9 +132,11 @@ "\n", "def main():\n", " test = TestPermutation()\n", - " test.test_permutation(permutations)\n", + " permutations = Permutations()\n", + " test.test_permutation(permutations.is_permutation)\n", " try:\n", - " test.test_permutation(permutations_alt)\n", + " permutations_alt = PermutationsAlt()\n", + " test.test_permutation(permutations_alt.is_permutation)\n", " except NameError:\n", " # Alternate solutions are only defined\n", " # in the solutions file\n", diff --git a/arrays_strings/permutation/permutation_solution.ipynb b/arrays_strings/permutation/permutation_solution.ipynb index 92630bc..b36f333 100644 --- a/arrays_strings/permutation/permutation_solution.ipynb +++ b/arrays_strings/permutation/permutation_solution.ipynb @@ -95,10 +95,12 @@ }, "outputs": [], "source": [ - "def permutations(str1, str2):\n", - " if str1 is None or str2 is None:\n", - " return False\n", - " return sorted(str1) == sorted(str2)" + "class Permutations(object):\n", + "\n", + " def is_permutation(self, str1, str2):\n", + " 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", "\n", "\n", - "def permutations_alt(str1, str2):\n", - " if str1 is None or str2 is None:\n", - " return False\n", - " if len(str1) != len(str2):\n", - " return False\n", - " unique_counts1 = defaultdict(int)\n", - " unique_counts2 = defaultdict(int)\n", - " for char in str1:\n", - " unique_counts1[char] += 1\n", - " for char in str2:\n", - " unique_counts2[char] += 1\n", - " return unique_counts1 == unique_counts2" + "class PermutationsAlt(object):\n", + "\n", + " def is_permutation(self, str1, str2):\n", + " if str1 is None or str2 is None:\n", + " return False\n", + " if len(str1) != len(str2):\n", + " return False\n", + " unique_counts1 = defaultdict(int)\n", + " unique_counts2 = defaultdict(int)\n", + " for char in str1:\n", + " unique_counts1[char] += 1\n", + " for char in str2:\n", + " unique_counts2[char] += 1\n", + " return unique_counts1 == unique_counts2" ] }, { @@ -201,9 +205,11 @@ "\n", "def main():\n", " test = TestPermutation()\n", - " test.test_permutation(permutations)\n", + " permutations = Permutations()\n", + " test.test_permutation(permutations.is_permutation)\n", " try:\n", - " test.test_permutation(permutations_alt)\n", + " permutations_alt = PermutationsAlt()\n", + " test.test_permutation(permutations_alt.is_permutation)\n", " except NameError:\n", " # Alternate solutions are only defined\n", " # in the solutions file\n", @@ -225,6 +231,7 @@ "name": "stdout", "output_type": "stream", "text": [ + "Success: test_permutation\n", "Success: test_permutation\n" ] } @@ -250,7 +257,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/arrays_strings/permutation/test_permutation_solution.py b/arrays_strings/permutation/test_permutation_solution.py index 23929a5..8320cfd 100644 --- a/arrays_strings/permutation/test_permutation_solution.py +++ b/arrays_strings/permutation/test_permutation_solution.py @@ -14,9 +14,11 @@ class TestPermutation(object): def main(): test = TestPermutation() - test.test_permutation(permutations) + permutations = Permutations() + test.test_permutation(permutations.is_permutation) try: - test.test_permutation(permutations_alt) + permutations_alt = PermutationsAlt() + test.test_permutation(permutations_alt.is_permutation) except NameError: # Alternate solutions are only defined # in the solutions file From 1fde26fcf5d3e58715af13ebab3de2632c358adc Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:40:13 -0400 Subject: [PATCH 3/5] Move string reverse to a class --- .../reverse_string_challenge.ipynb | 25 +++++---- .../reverse_string_solution.ipynb | 52 ++++++++++--------- .../reverse_string/test_reverse_string.py | 17 +++--- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/arrays_strings/reverse_string/reverse_string_challenge.ipynb b/arrays_strings/reverse_string/reverse_string_challenge.ipynb index a77aa6b..ebad367 100644 --- a/arrays_strings/reverse_string/reverse_string_challenge.ipynb +++ b/arrays_strings/reverse_string/reverse_string_challenge.ipynb @@ -78,9 +78,11 @@ }, "outputs": [], "source": [ - "def reverse_string(list_chars):\n", - " # TODO: Implement me\n", - " pass" + "class ReverseString(object):\n", + "\n", + " def reverse(self, chars):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -113,25 +115,26 @@ "\n", "class TestReverse(object):\n", "\n", - " def test_reverse(self):\n", - " assert_equal(reverse_string(None), None)\n", - " assert_equal(reverse_string(['']), [''])\n", - " assert_equal(reverse_string(\n", + " def test_reverse(self, func):\n", + " assert_equal(func(None), None)\n", + " assert_equal(func(['']), [''])\n", + " assert_equal(func(\n", " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", " ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " print('Success: test_reverse')\n", "\n", - " def test_reverse_inplace(self):\n", + " def test_reverse_inplace(self, func):\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", " print('Success: test_reverse_inplace')\n", "\n", "\n", "def main():\n", " test = TestReverse()\n", - " test.test_reverse()\n", - " test.test_reverse_inplace()\n", + " reverse_string = ReverseString()\n", + " test.test_reverse(reverse_string.reverse)\n", + " test.test_reverse_inplace(reverse_string.reverse)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/arrays_strings/reverse_string/reverse_string_solution.ipynb b/arrays_strings/reverse_string/reverse_string_solution.ipynb index e3eb99b..112e6e2 100644 --- a/arrays_strings/reverse_string/reverse_string_solution.ipynb +++ b/arrays_strings/reverse_string/reverse_string_solution.ipynb @@ -94,14 +94,16 @@ "from __future__ import division\n", "\n", "\n", - "def reverse_string(chars):\n", - " if chars is None:\n", - " return None\n", - " size = len(chars)\n", - " for i in range(size//2):\n", - " chars[i], chars[size-1-i] = \\\n", - " chars[size-1-i], chars[i]\n", - " return chars" + "class ReverseString(object):\n", + "\n", + " def reverse(self, chars):\n", + " if chars is None:\n", + " return None\n", + " size = len(chars)\n", + " for i in range(size//2):\n", + " chars[i], chars[size-1-i] = \\\n", + " chars[size-1-i], chars[i]\n", + " return chars" ] }, { @@ -121,16 +123,17 @@ }, "outputs": [], "source": [ - "def reverse_string_alt(string):\n", - " if string is None:\n", - " return None\n", - " return string[::-1]\n", + "class ReverseStringAlt(object):\n", "\n", + " def reverse_string_alt(string):\n", + " if string is None:\n", + " return None\n", + " return string[::-1]\n", "\n", - "def reverse_string_alt2(string):\n", - " if string is None:\n", - " return None\n", - " return ''.join(reversed(string))" + " def reverse_string_alt2(string):\n", + " if string is None:\n", + " return None\n", + " return ''.join(reversed(string))" ] }, { @@ -162,25 +165,26 @@ "\n", "class TestReverse(object):\n", "\n", - " def test_reverse(self):\n", - " assert_equal(reverse_string(None), None)\n", - " assert_equal(reverse_string(['']), [''])\n", - " assert_equal(reverse_string(\n", + " def test_reverse(self, func):\n", + " assert_equal(func(None), None)\n", + " assert_equal(func(['']), [''])\n", + " assert_equal(func(\n", " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", " ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " print('Success: test_reverse')\n", "\n", - " def test_reverse_inplace(self):\n", + " def test_reverse_inplace(self, func):\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", " print('Success: test_reverse_inplace')\n", "\n", "\n", "def main():\n", " test = TestReverse()\n", - " test.test_reverse()\n", - " test.test_reverse_inplace()\n", + " reverse_string = ReverseString()\n", + " test.test_reverse(reverse_string.reverse)\n", + " test.test_reverse_inplace(reverse_string.reverse)\n", "\n", "\n", "if __name__ == '__main__':\n", diff --git a/arrays_strings/reverse_string/test_reverse_string.py b/arrays_strings/reverse_string/test_reverse_string.py index cbb20f1..3f2a1f7 100644 --- a/arrays_strings/reverse_string/test_reverse_string.py +++ b/arrays_strings/reverse_string/test_reverse_string.py @@ -3,25 +3,26 @@ from nose.tools import assert_equal class TestReverse(object): - def test_reverse(self): - assert_equal(reverse_string(None), None) - assert_equal(reverse_string(['']), ['']) - assert_equal(reverse_string( + def test_reverse(self, func): + assert_equal(func(None), None) + assert_equal(func(['']), ['']) + assert_equal(func( ['f', 'o', 'o', ' ', 'b', 'a', 'r']), ['r', 'a', 'b', ' ', 'o', 'o', 'f']) print('Success: test_reverse') - def test_reverse_inplace(self): + def test_reverse_inplace(self, func): 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']) print('Success: test_reverse_inplace') def main(): test = TestReverse() - test.test_reverse() - test.test_reverse_inplace() + reverse_string = ReverseString() + test.test_reverse(reverse_string.reverse) + test.test_reverse_inplace(reverse_string.reverse) if __name__ == '__main__': From 14a2751862a3d0c258b2480816d99947f782f016 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:40:52 -0400 Subject: [PATCH 4/5] Move string rotation to a class --- .../rotation/rotation_challenge.ipynb | 26 +++++++++-------- .../rotation/rotation_solution.ipynb | 28 ++++++++++--------- arrays_strings/rotation/test_rotation.py | 11 ++++---- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/arrays_strings/rotation/rotation_challenge.ipynb b/arrays_strings/rotation/rotation_challenge.ipynb index 24b4db3..6fd765d 100644 --- a/arrays_strings/rotation/rotation_challenge.ipynb +++ b/arrays_strings/rotation/rotation_challenge.ipynb @@ -82,15 +82,16 @@ }, "outputs": [], "source": [ - "def is_substring(s1, s2):\n", - " # TODO: Implement me\n", - " pass\n", + "class Rotation(object):\n", "\n", + " def is_substring(self, s1, s2):\n", + " # TODO: Implement me\n", + " pass\n", "\n", - "def is_rotation(s1, s2):\n", - " # TODO: Implement me\n", - " # Call is_substring only once\n", - " pass" + " def is_rotation(self, s1, s2):\n", + " # TODO: Implement me\n", + " # Call is_substring only once\n", + " pass" ] }, { @@ -124,11 +125,12 @@ "class TestRotation(object):\n", "\n", " def test_rotation(self):\n", - " assert_equal(is_rotation('o', 'oo'), False)\n", - " assert_equal(is_rotation(None, 'foo'), False)\n", - " assert_equal(is_rotation('', 'foo'), False)\n", - " assert_equal(is_rotation('', ''), True)\n", - " assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n", + " rotation = Rotation()\n", + " assert_equal(rotation.is_rotation('o', 'oo'), False)\n", + " assert_equal(rotation.is_rotation(None, 'foo'), False)\n", + " assert_equal(rotation.is_rotation('', 'foo'), False)\n", + " assert_equal(rotation.is_rotation('', ''), True)\n", + " assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)\n", " print('Success: test_rotation')\n", "\n", "\n", diff --git a/arrays_strings/rotation/rotation_solution.ipynb b/arrays_strings/rotation/rotation_solution.ipynb index 80d777c..a153a02 100644 --- a/arrays_strings/rotation/rotation_solution.ipynb +++ b/arrays_strings/rotation/rotation_solution.ipynb @@ -92,16 +92,17 @@ }, "outputs": [], "source": [ - "def is_substring(s1, s2):\n", - " return s1 in s2\n", + "class Rotation(object):\n", "\n", + " def is_substring(self, s1, s2):\n", + " return s1 in s2\n", "\n", - "def is_rotation(s1, s2):\n", - " if s1 is None or s2 is None:\n", - " return False\n", - " if len(s1) != len(s2):\n", - " return False\n", - " return is_substring(s1, s2 + s2)" + " def is_rotation(self, s1, s2):\n", + " if s1 is None or s2 is None:\n", + " return False\n", + " if len(s1) != len(s2):\n", + " return False\n", + " return self.is_substring(s1, s2 + s2)" ] }, { @@ -134,11 +135,12 @@ "class TestRotation(object):\n", "\n", " def test_rotation(self):\n", - " assert_equal(is_rotation('o', 'oo'), False)\n", - " assert_equal(is_rotation(None, 'foo'), False)\n", - " assert_equal(is_rotation('', 'foo'), False)\n", - " assert_equal(is_rotation('', ''), True)\n", - " assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n", + " rotation = Rotation()\n", + " assert_equal(rotation.is_rotation('o', 'oo'), False)\n", + " assert_equal(rotation.is_rotation(None, 'foo'), False)\n", + " assert_equal(rotation.is_rotation('', 'foo'), False)\n", + " assert_equal(rotation.is_rotation('', ''), True)\n", + " assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)\n", " print('Success: test_rotation')\n", "\n", "\n", diff --git a/arrays_strings/rotation/test_rotation.py b/arrays_strings/rotation/test_rotation.py index 4c14422..89b0a20 100644 --- a/arrays_strings/rotation/test_rotation.py +++ b/arrays_strings/rotation/test_rotation.py @@ -4,11 +4,12 @@ from nose.tools import assert_equal class TestRotation(object): def test_rotation(self): - assert_equal(is_rotation('o', 'oo'), False) - assert_equal(is_rotation(None, 'foo'), False) - assert_equal(is_rotation('', 'foo'), False) - assert_equal(is_rotation('', ''), True) - assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True) + rotation = Rotation() + assert_equal(rotation.is_rotation('o', 'oo'), False) + assert_equal(rotation.is_rotation(None, 'foo'), False) + assert_equal(rotation.is_rotation('', 'foo'), False) + assert_equal(rotation.is_rotation('', ''), True) + assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True) print('Success: test_rotation') From 8a7c14239cb4ae3c2535b85f540d7c965ae57053 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:42:05 -0400 Subject: [PATCH 5/5] Move unique chars to a class --- .../unique_chars/test_unique_chars.py | 9 ++-- .../unique_chars/unique_chars_challenge.ipynb | 17 +++--- .../unique_chars/unique_chars_solution.ipynb | 53 +++++++++++-------- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/arrays_strings/unique_chars/test_unique_chars.py b/arrays_strings/unique_chars/test_unique_chars.py index 5219cf5..fc7ceba 100644 --- a/arrays_strings/unique_chars/test_unique_chars.py +++ b/arrays_strings/unique_chars/test_unique_chars.py @@ -13,10 +13,13 @@ class TestUniqueChars(object): def main(): test = TestUniqueChars() - test.test_unique_chars(unique_chars) + unique_chars = UniqueChars() + test.test_unique_chars(unique_chars.has_unique_chars) try: - test.test_unique_chars(unique_chars_hash) - test.test_unique_chars(unique_chars_inplace) + unique_chars_set = UniqueCharsSet() + 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: # Alternate solutions are only defined # in the solutions file diff --git a/arrays_strings/unique_chars/unique_chars_challenge.ipynb b/arrays_strings/unique_chars/unique_chars_challenge.ipynb index 1f126ee..535a5eb 100644 --- a/arrays_strings/unique_chars/unique_chars_challenge.ipynb +++ b/arrays_strings/unique_chars/unique_chars_challenge.ipynb @@ -81,9 +81,11 @@ }, "outputs": [], "source": [ - "def unique_chars(string):\n", - " # TODO: Implement me\n", - " pass" + "class UniqueChars(object):\n", + "\n", + " def has_unique_chars(self, string):\n", + " # TODO: Implement me\n", + " pass" ] }, { @@ -124,10 +126,13 @@ "\n", "def main():\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", - " test.test_unique_chars(unique_chars_hash)\n", - " test.test_unique_chars(unique_chars_inplace)\n", + " unique_chars_set = UniqueCharsSet()\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", " # Alternate solutions are only defined\n", " # in the solutions file\n", diff --git a/arrays_strings/unique_chars/unique_chars_solution.ipynb b/arrays_strings/unique_chars/unique_chars_solution.ipynb index 2f84953..971274b 100644 --- a/arrays_strings/unique_chars/unique_chars_solution.ipynb +++ b/arrays_strings/unique_chars/unique_chars_solution.ipynb @@ -93,10 +93,12 @@ }, "outputs": [], "source": [ - "def unique_chars(string):\n", - " if string is None:\n", - " return False\n", - " return len(set(string)) == len(string)" + "class UniqueCharsSet(object):\n", + "\n", + " def has_unique_chars(self, string):\n", + " if string is None:\n", + " return False\n", + " return len(set(string)) == len(string)" ] }, { @@ -138,16 +140,18 @@ }, "outputs": [], "source": [ - "def unique_chars_hash(string):\n", - " if string is None:\n", - " return False\n", - " chars_set = set()\n", - " for char in string:\n", - " if char in chars_set:\n", + "class UniqueChars(object):\n", + "\n", + " def has_unique_chars(self, string):\n", + " if string is None:\n", " return False\n", - " else:\n", - " chars_set.add(char)\n", - " return True" + " chars_set = set()\n", + " for char in string:\n", + " if char in chars_set:\n", + " return False\n", + " else:\n", + " chars_set.add(char)\n", + " return True" ] }, { @@ -184,13 +188,15 @@ }, "outputs": [], "source": [ - "def unique_chars_inplace(string):\n", - " if string is None:\n", - " return False\n", - " for char in string:\n", - " if string.count(char) > 1:\n", + "class UniqueCharsInPlace(object):\n", + "\n", + " def has_unique_chars(self, string):\n", + " if string is None:\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", "def main():\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", - " test.test_unique_chars(unique_chars_hash)\n", - " test.test_unique_chars(unique_chars_inplace)\n", + " unique_chars_set = UniqueCharsSet()\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", " # Alternate solutions are only defined\n", " # in the solutions file\n",