mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 17:38:02 +00:00
Move string rotation to a class
This commit is contained in:
@@ -82,15 +82,16 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def is_substring(s1, s2):\n",
|
"class Rotation(object):\n",
|
||||||
" # TODO: Implement me\n",
|
|
||||||
" pass\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
|
" def is_substring(self, s1, s2):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def is_rotation(s1, s2):\n",
|
" def is_rotation(self, s1, s2):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" # Call is_substring only once\n",
|
" # Call is_substring only once\n",
|
||||||
" pass"
|
" pass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -124,11 +125,12 @@
|
|||||||
"class TestRotation(object):\n",
|
"class TestRotation(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_rotation(self):\n",
|
" def test_rotation(self):\n",
|
||||||
" assert_equal(is_rotation('o', 'oo'), False)\n",
|
" rotation = Rotation()\n",
|
||||||
" assert_equal(is_rotation(None, 'foo'), False)\n",
|
" assert_equal(rotation.is_rotation('o', 'oo'), False)\n",
|
||||||
" assert_equal(is_rotation('', 'foo'), False)\n",
|
" assert_equal(rotation.is_rotation(None, 'foo'), False)\n",
|
||||||
" assert_equal(is_rotation('', ''), True)\n",
|
" assert_equal(rotation.is_rotation('', 'foo'), False)\n",
|
||||||
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n",
|
" assert_equal(rotation.is_rotation('', ''), True)\n",
|
||||||
|
" assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)\n",
|
||||||
" print('Success: test_rotation')\n",
|
" print('Success: test_rotation')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -92,16 +92,17 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def is_substring(s1, s2):\n",
|
"class Rotation(object):\n",
|
||||||
" return s1 in s2\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
|
" def is_substring(self, s1, s2):\n",
|
||||||
|
" return s1 in s2\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def is_rotation(s1, s2):\n",
|
" def is_rotation(self, s1, s2):\n",
|
||||||
" if s1 is None or s2 is None:\n",
|
" if s1 is None or s2 is None:\n",
|
||||||
" return False\n",
|
" return False\n",
|
||||||
" if len(s1) != len(s2):\n",
|
" if len(s1) != len(s2):\n",
|
||||||
" return False\n",
|
" return False\n",
|
||||||
" return is_substring(s1, s2 + s2)"
|
" return self.is_substring(s1, s2 + s2)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -134,11 +135,12 @@
|
|||||||
"class TestRotation(object):\n",
|
"class TestRotation(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_rotation(self):\n",
|
" def test_rotation(self):\n",
|
||||||
" assert_equal(is_rotation('o', 'oo'), False)\n",
|
" rotation = Rotation()\n",
|
||||||
" assert_equal(is_rotation(None, 'foo'), False)\n",
|
" assert_equal(rotation.is_rotation('o', 'oo'), False)\n",
|
||||||
" assert_equal(is_rotation('', 'foo'), False)\n",
|
" assert_equal(rotation.is_rotation(None, 'foo'), False)\n",
|
||||||
" assert_equal(is_rotation('', ''), True)\n",
|
" assert_equal(rotation.is_rotation('', 'foo'), False)\n",
|
||||||
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n",
|
" assert_equal(rotation.is_rotation('', ''), True)\n",
|
||||||
|
" assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)\n",
|
||||||
" print('Success: test_rotation')\n",
|
" print('Success: test_rotation')\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|||||||
@@ -4,11 +4,12 @@ from nose.tools import assert_equal
|
|||||||
class TestRotation(object):
|
class TestRotation(object):
|
||||||
|
|
||||||
def test_rotation(self):
|
def test_rotation(self):
|
||||||
assert_equal(is_rotation('o', 'oo'), False)
|
rotation = Rotation()
|
||||||
assert_equal(is_rotation(None, 'foo'), False)
|
assert_equal(rotation.is_rotation('o', 'oo'), False)
|
||||||
assert_equal(is_rotation('', 'foo'), False)
|
assert_equal(rotation.is_rotation(None, 'foo'), False)
|
||||||
assert_equal(is_rotation('', ''), True)
|
assert_equal(rotation.is_rotation('', 'foo'), False)
|
||||||
assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)
|
assert_equal(rotation.is_rotation('', ''), True)
|
||||||
|
assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)
|
||||||
print('Success: test_rotation')
|
print('Success: test_rotation')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user