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,7 +78,9 @@
},
"outputs": [],
"source": [
"def reverse_string(list_chars):\n",
"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",

View File

@ -94,7 +94,9 @@
"from __future__ import division\n",
"\n",
"\n",
"def reverse_string(chars):\n",
"class ReverseString(object):\n",
"\n",
" def reverse(self, chars):\n",
" if chars is None:\n",
" return None\n",
" size = len(chars)\n",
@ -121,13 +123,14 @@
},
"outputs": [],
"source": [
"def reverse_string_alt(string):\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",
"\n",
"def reverse_string_alt2(string):\n",
" 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",

View File

@ -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__':