mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 16:08:02 +00:00
Merge pull request #96 from donnemartin/develop
Move string challenges and solutions to classes
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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__':
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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__':
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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')
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user