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():
|
||||
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",
|
||||
|
|
Loading…
Reference in New Issue
Block a user