mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move string reverse to a class
This commit is contained in:
parent
22e3549d7d
commit
1fde26fcf5
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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__':
|
||||
|
|
Loading…
Reference in New Issue
Block a user