Move string reverse to a class

This commit is contained in:
Donne Martin 2016-08-13 06:40:13 -04:00
parent 22e3549d7d
commit 1fde26fcf5
3 changed files with 51 additions and 43 deletions

View File

@ -78,9 +78,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def reverse_string(list_chars):\n", "class ReverseString(object):\n",
" # TODO: Implement me\n", "\n",
" pass" " def reverse(self, chars):\n",
" # TODO: Implement me\n",
" pass"
] ]
}, },
{ {
@ -113,25 +115,26 @@
"\n", "\n",
"class TestReverse(object):\n", "class TestReverse(object):\n",
"\n", "\n",
" def test_reverse(self):\n", " def test_reverse(self, func):\n",
" assert_equal(reverse_string(None), None)\n", " assert_equal(func(None), None)\n",
" assert_equal(reverse_string(['']), [''])\n", " assert_equal(func(['']), [''])\n",
" assert_equal(reverse_string(\n", " assert_equal(func(\n",
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse')\n", " print('Success: test_reverse')\n",
"\n", "\n",
" def test_reverse_inplace(self):\n", " def test_reverse_inplace(self, func):\n",
" target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']\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", " assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse_inplace')\n", " print('Success: test_reverse_inplace')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestReverse()\n", " test = TestReverse()\n",
" test.test_reverse()\n", " reverse_string = ReverseString()\n",
" test.test_reverse_inplace()\n", " test.test_reverse(reverse_string.reverse)\n",
" test.test_reverse_inplace(reverse_string.reverse)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@ -94,14 +94,16 @@
"from __future__ import division\n", "from __future__ import division\n",
"\n", "\n",
"\n", "\n",
"def reverse_string(chars):\n", "class ReverseString(object):\n",
" if chars is None:\n", "\n",
" return None\n", " def reverse(self, chars):\n",
" size = len(chars)\n", " if chars is None:\n",
" for i in range(size//2):\n", " return None\n",
" chars[i], chars[size-1-i] = \\\n", " size = len(chars)\n",
" chars[size-1-i], chars[i]\n", " for i in range(size//2):\n",
" return chars" " chars[i], chars[size-1-i] = \\\n",
" chars[size-1-i], chars[i]\n",
" return chars"
] ]
}, },
{ {
@ -121,16 +123,17 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def reverse_string_alt(string):\n", "class ReverseStringAlt(object):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\n",
"\n", "\n",
" def reverse_string_alt(string):\n",
" if string is None:\n",
" return None\n",
" return string[::-1]\n",
"\n", "\n",
"def reverse_string_alt2(string):\n", " def reverse_string_alt2(string):\n",
" if string is None:\n", " if string is None:\n",
" return None\n", " return None\n",
" return ''.join(reversed(string))" " return ''.join(reversed(string))"
] ]
}, },
{ {
@ -162,25 +165,26 @@
"\n", "\n",
"class TestReverse(object):\n", "class TestReverse(object):\n",
"\n", "\n",
" def test_reverse(self):\n", " def test_reverse(self, func):\n",
" assert_equal(reverse_string(None), None)\n", " assert_equal(func(None), None)\n",
" assert_equal(reverse_string(['']), [''])\n", " assert_equal(func(['']), [''])\n",
" assert_equal(reverse_string(\n", " assert_equal(func(\n",
" ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n", " ['f', 'o', 'o', ' ', 'b', 'a', 'r']),\n",
" ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n", " ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse')\n", " print('Success: test_reverse')\n",
"\n", "\n",
" def test_reverse_inplace(self):\n", " def test_reverse_inplace(self, func):\n",
" target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r']\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", " assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])\n",
" print('Success: test_reverse_inplace')\n", " print('Success: test_reverse_inplace')\n",
"\n", "\n",
"\n", "\n",
"def main():\n", "def main():\n",
" test = TestReverse()\n", " test = TestReverse()\n",
" test.test_reverse()\n", " reverse_string = ReverseString()\n",
" test.test_reverse_inplace()\n", " test.test_reverse(reverse_string.reverse)\n",
" test.test_reverse_inplace(reverse_string.reverse)\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",

View File

@ -3,25 +3,26 @@ from nose.tools import assert_equal
class TestReverse(object): class TestReverse(object):
def test_reverse(self): def test_reverse(self, func):
assert_equal(reverse_string(None), None) assert_equal(func(None), None)
assert_equal(reverse_string(['']), ['']) assert_equal(func(['']), [''])
assert_equal(reverse_string( assert_equal(func(
['f', 'o', 'o', ' ', 'b', 'a', 'r']), ['f', 'o', 'o', ' ', 'b', 'a', 'r']),
['r', 'a', 'b', ' ', 'o', 'o', 'f']) ['r', 'a', 'b', ' ', 'o', 'o', 'f'])
print('Success: test_reverse') print('Success: test_reverse')
def test_reverse_inplace(self): def test_reverse_inplace(self, func):
target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r'] 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']) assert_equal(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f'])
print('Success: test_reverse_inplace') print('Success: test_reverse_inplace')
def main(): def main():
test = TestReverse() test = TestReverse()
test.test_reverse() reverse_string = ReverseString()
test.test_reverse_inplace() test.test_reverse(reverse_string.reverse)
test.test_reverse_inplace(reverse_string.reverse)
if __name__ == '__main__': if __name__ == '__main__':