diff --git a/arrays_strings/reverse_string/reverse_string_challenge.ipynb b/arrays_strings/reverse_string/reverse_string_challenge.ipynb index a77aa6b..ebad367 100644 --- a/arrays_strings/reverse_string/reverse_string_challenge.ipynb +++ b/arrays_strings/reverse_string/reverse_string_challenge.ipynb @@ -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", diff --git a/arrays_strings/reverse_string/reverse_string_solution.ipynb b/arrays_strings/reverse_string/reverse_string_solution.ipynb index e3eb99b..112e6e2 100644 --- a/arrays_strings/reverse_string/reverse_string_solution.ipynb +++ b/arrays_strings/reverse_string/reverse_string_solution.ipynb @@ -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", diff --git a/arrays_strings/reverse_string/test_reverse_string.py b/arrays_strings/reverse_string/test_reverse_string.py index cbb20f1..3f2a1f7 100644 --- a/arrays_strings/reverse_string/test_reverse_string.py +++ b/arrays_strings/reverse_string/test_reverse_string.py @@ -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__':