Move unique chars to a class

This commit is contained in:
Donne Martin 2016-08-13 06:42:05 -04:00
parent 14a2751862
commit 8a7c14239c
3 changed files with 48 additions and 31 deletions

View File

@ -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

View File

@ -81,7 +81,9 @@
},
"outputs": [],
"source": [
"def unique_chars(string):\n",
"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",

View File

@ -93,7 +93,9 @@
},
"outputs": [],
"source": [
"def unique_chars(string):\n",
"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,7 +140,9 @@
},
"outputs": [],
"source": [
"def unique_chars_hash(string):\n",
"class UniqueChars(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" if string is None:\n",
" return False\n",
" chars_set = set()\n",
@ -184,7 +188,9 @@
},
"outputs": [],
"source": [
"def unique_chars_inplace(string):\n",
"class UniqueCharsInPlace(object):\n",
"\n",
" def has_unique_chars(self, string):\n",
" if string is None:\n",
" return False\n",
" for char in string:\n",
@ -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",