From 8a7c14239cb4ae3c2535b85f540d7c965ae57053 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 13 Aug 2016 06:42:05 -0400 Subject: [PATCH] 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",