mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move unique chars to a class
This commit is contained in:
parent
14a2751862
commit
8a7c14239c
|
@ -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
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -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",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user