Added an O(1) memory solution for the string rotation challenge

This commit is contained in:
Dan Monroe 2018-12-09 09:31:48 -06:00
parent fe227a3fe3
commit f10bdf45a8
3 changed files with 139 additions and 51 deletions

View File

@ -77,9 +77,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class Rotation(object):\n", "class Rotation(object):\n",
@ -113,9 +111,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# %load test_rotation.py\n", "# %load test_rotation.py\n",
@ -124,19 +120,26 @@
"\n", "\n",
"class TestRotation(object):\n", "class TestRotation(object):\n",
"\n", "\n",
" def test_rotation(self):\n", " def test_rotation(self, func):\n",
" rotation = Rotation()\n", " assert_equal(func('o', 'oo'), False)\n",
" assert_equal(rotation.is_rotation('o', 'oo'), False)\n", " assert_equal(func(None, 'foo'), False)\n",
" assert_equal(rotation.is_rotation(None, 'foo'), False)\n", " assert_equal(func('', 'foo'), False)\n",
" assert_equal(rotation.is_rotation('', 'foo'), False)\n", " assert_equal(func('', ''), True)\n",
" assert_equal(rotation.is_rotation('', ''), True)\n", " assert_equal(func('foobarbaz', 'barbazfoo'), 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",
"def main():\n", "def main():\n",
" test = TestRotation()\n", " test = TestRotation()\n",
" test.test_rotation()\n", " rotation = Rotation()\n",
" test.test_rotation(rotation.is_rotation)\n",
" try:\n",
" rotation_in_place = RotationInPlace()\n",
" test.test_rotation(rotation_in_place.is_rotation)\n",
" except NameError:\n",
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
" pass\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@ -169,9 +172,9 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.0" "version": "3.5.2"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -62,7 +62,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Algorithm\n", "## Algorithm: Using the Python 'in' operation\n",
"\n", "\n",
"Examine the following test case:\n", "Examine the following test case:\n",
"* s1 = 'barbazfoo'\n", "* s1 = 'barbazfoo'\n",
@ -72,6 +72,10 @@
"* s2 = 'foobarbaz'\n", "* s2 = 'foobarbaz'\n",
"* s3 = 'barbaz*foobarbaz*foo'\n", "* s3 = 'barbaz*foobarbaz*foo'\n",
"\n", "\n",
"Notes:\n",
"* When we concatenate the strings, we need to create a new one. This is why the extra space required is O(n).\n",
"* However, this approach is very readable and concise, and using built-in Python collections operators is generally faster than writing your own, even if the theoretical time complexity is the same. Unless memory is constrained this is the better approach.\n",
"\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\n", "* Time: O(n)\n",
"* Space: O(n)" "* Space: O(n)"
@ -81,15 +85,13 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Code" "## Code: Using the Python 'in' operation"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [], "outputs": [],
"source": [ "source": [
"class Rotation(object):\n", "class Rotation(object):\n",
@ -109,15 +111,85 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Unit Test" "## Algorithm: Comparing In Place\n",
"\n",
"Notes:\n",
"* This is taking the same high-level approach as the previous solution, but it is operating on the strings in place without creating any new string.\n",
"* This is a bit more complex and less readable.\n",
"* This approach is not using a built-in Python collection operator, so it will be slower.\n",
"\n",
"Complexity:\n",
"* Time: O(n)\n",
"* Space: O(1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code: Comparing In Place"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": false "outputs": [],
}, "source": [
"class RotationInPlace(object):\n",
"\n",
" def is_substring(self, s1, s2):\n",
" # We already assured the strings are the same length; this is for readability\n",
" length = len(s1)\n",
"\n",
" # If both strings are empty, they are trivially a rotation of one another\n",
" if length == 0:\n",
" return True\n",
"\n",
" i1, i2, matching_count = 0, 0, 0\n",
" # This loop achieves the same as concatenating a string with itself\n",
" for _ in range(2*length):\n",
" \n",
" # Count consecutive character matches.\n",
" if s1[i1] == s2[i2]:\n",
" matching_count += 1\n",
" \n",
" # If the count is equal to the length of the strings, we have found a rotation\n",
" if matching_count == length:\n",
" return True\n",
" \n",
" i2 += 1\n",
" # If there is a mismatch, start the count over again\n",
" else:\n",
" matching_count, i2 = 0, 0\n",
"\n",
" # Increment i1 and wrap around if necessary\n",
" i1 += 1\n",
" if i1 == length:\n",
" i1 = 0\n",
" \n",
" # Assume there is no rotation by default\n",
" return False\n",
"\n",
" def is_rotation(self, s1, s2):\n",
" if s1 is None or s2 is None:\n",
" return False\n",
" if len(s1) != len(s2):\n",
" return False\n",
" return self.is_substring(s1, s2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -134,19 +206,26 @@
"\n", "\n",
"class TestRotation(object):\n", "class TestRotation(object):\n",
"\n", "\n",
" def test_rotation(self):\n", " def test_rotation(self, func):\n",
" rotation = Rotation()\n", " assert_equal(func('o', 'oo'), False)\n",
" assert_equal(rotation.is_rotation('o', 'oo'), False)\n", " assert_equal(func(None, 'foo'), False)\n",
" assert_equal(rotation.is_rotation(None, 'foo'), False)\n", " assert_equal(func('', 'foo'), False)\n",
" assert_equal(rotation.is_rotation('', 'foo'), False)\n", " assert_equal(func('', ''), True)\n",
" assert_equal(rotation.is_rotation('', ''), True)\n", " assert_equal(func('foobarbaz', 'barbazfoo'), 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",
"def main():\n", "def main():\n",
" test = TestRotation()\n", " test = TestRotation()\n",
" test.test_rotation()\n", " rotation = Rotation()\n",
" test.test_rotation(rotation.is_rotation)\n",
" try:\n",
" rotation_in_place = RotationInPlace()\n",
" test.test_rotation(rotation_in_place.is_rotation)\n",
" except NameError:\n",
" # Alternate solutions are only defined\n",
" # in the solutions file\n",
" pass\n",
"\n", "\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
@ -155,15 +234,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": false
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Success: test_rotation\n",
"Success: test_rotation\n" "Success: test_rotation\n"
] ]
} }
@ -175,23 +253,23 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 2",
"language": "python", "language": "python",
"name": "python3" "name": "python2"
}, },
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 3 "version": 2
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython2",
"version": "3.5.0" "version": "2.7.12"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 0 "nbformat_minor": 1
} }

View File

@ -3,19 +3,26 @@ from nose.tools import assert_equal
class TestRotation(object): class TestRotation(object):
def test_rotation(self): def test_rotation(self, func):
rotation = Rotation() assert_equal(func('o', 'oo'), False)
assert_equal(rotation.is_rotation('o', 'oo'), False) assert_equal(func(None, 'foo'), False)
assert_equal(rotation.is_rotation(None, 'foo'), False) assert_equal(func('', 'foo'), False)
assert_equal(rotation.is_rotation('', 'foo'), False) assert_equal(func('', ''), True)
assert_equal(rotation.is_rotation('', ''), True) assert_equal(func('foobarbaz', 'barbazfoo'), True)
assert_equal(rotation.is_rotation('foobarbaz', 'barbazfoo'), True)
print('Success: test_rotation') print('Success: test_rotation')
def main(): def main():
test = TestRotation() test = TestRotation()
test.test_rotation() rotation = Rotation()
test.test_rotation(rotation.is_rotation)
try:
rotation_in_place = RotationInPlace()
test.test_rotation(rotation_in_place.is_rotation)
except NameError:
# Alternate solutions are only defined
# in the solutions file
pass
if __name__ == '__main__': if __name__ == '__main__':