Added rotation challenge and solution.

This commit is contained in:
Donne Martin
2015-06-29 20:30:47 -04:00
parent a7a0b0a77c
commit c8e65593d4
3 changed files with 195 additions and 15 deletions

View File

@@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Determine if a string s1 is a rotation of another string s2. Also write a function is_substring which you can only call once to determine whether a rotation occurs\n",
"## Problem: Determine if a string s1 is a rotation of another string s2, by calling (only once) a function is_substring\n",
"\n",
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
"* [Test Cases](#Test-Cases)\n",
@@ -43,8 +43,8 @@
"source": [
"## Test Cases\n",
"\n",
"* Any strings that differ in size results in False\n",
"* NULL, 'foo' -> False (any NULL results in False)\n",
"* Any strings that differ in size -> False\n",
"* None, 'foo' -> False (any None results in False)\n",
"* ' ', 'foo' -> False\n",
"* ' ', ' ' -> True\n",
"* 'foobarbaz', 'barbazfoo' -> True"
@@ -84,7 +84,7 @@
},
"outputs": [],
"source": [
"def is_substring(s1, s2):\n",
"def is_substring(s1, s2): \n",
" return s1 in s2\n",
"\n",
"def is_rotation(s1, s2):\n",
@@ -121,25 +121,50 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_is_rotation\n"
"Overwriting test_rotation.py\n"
]
}
],
"source": [
"%%writefile test_rotation.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_is_rotation(self, func):\n",
" assert_equal(func('o', 'oo'), False)\n",
" assert_equal(func(None, 'foo'), False)\n",
" assert_equal(func('', 'foo'), False)\n",
" assert_equal(func('', ''), True)\n",
" assert_equal(func('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_is_rotation')\n",
"\n",
"class TestRotation(object):\n",
" \n",
" def test_rotation(self):\n",
" assert_equal(is_rotation('o', 'oo'), False)\n",
" assert_equal(is_rotation(None, 'foo'), False)\n",
" assert_equal(is_rotation('', 'foo'), False)\n",
" assert_equal(is_rotation('', ''), True)\n",
" assert_equal(is_rotation('foobarbaz', 'barbazfoo'), True)\n",
" print('Success: test_rotation')\n",
"\n",
"def main():\n",
" test = TestRotation()\n",
" test.test_rotation()\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_is_rotation(is_rotation)"
" main()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_rotation\n"
]
}
],
"source": [
"%run -i test_rotation.py"
]
}
],