Added palindrome challenge.

This commit is contained in:
Donne Martin
2015-06-30 07:31:34 -04:00
parent bf8d05c295
commit 61e1b2de8e
5 changed files with 272 additions and 24 deletions

View File

@@ -41,12 +41,11 @@
"## Test Cases\n",
"\n",
"\n",
"* Empty list\n",
"* Single element list\n",
"* Two element list, not a palindrome\n",
"* Three element list, not a palindrome\n",
"* General case: Palindrome with even length\n",
"* General case: Palindrome with odd length"
"* Empty list -> False\n",
"* Single element list -> False\n",
"* Two or more element list, not a palindrome -> False\n",
"* General case: Palindrome with even length -> True\n",
"* General case: Palindrome with odd length -> True"
]
},
{
@@ -78,11 +77,11 @@
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
"collapsed": false
},
"outputs": [],
"source": [
"%run linked_list.py"
"%run ../linked_list/linked_list.py"
]
},
{
@@ -94,6 +93,7 @@
"outputs": [],
"source": [
"class MyLinkedList(LinkedList):\n",
" \n",
" def is_palindrome(self):\n",
" if self.head is None or self.head.next is None:\n",
" return False\n",
@@ -145,21 +145,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Empty list\n",
"Test: Single element list\n",
"Test: Two element list, not a palindrome\n",
"Test: Three element list, not a palindrome\n",
"Test: General case: Palindrome with even length\n",
"Test: General case: Palindrome with odd length\n",
"Success: test_is_palindrome\n"
"Overwriting test_palindrome.py\n"
]
}
],
"source": [
"%%writefile test_palindrome.py\n",
"from nose.tools import assert_equal\n",
"\n",
"class Test(object):\n",
" def test_is_palindrome(self):\n",
"\n",
"class TestPalindrome(object):\n",
" \n",
" def test_palindrome(self):\n",
" print('Test: Empty list')\n",
" linked_list = MyLinkedList()\n",
" assert_equal(linked_list.is_palindrome(), False)\n",
@@ -173,10 +170,6 @@
" linked_list.append(2)\n",
" assert_equal(linked_list.is_palindrome(), False)\n",
"\n",
" print('Test: Three element list, not a palindrome')\n",
" linked_list.append(3)\n",
" assert_equal(linked_list.is_palindrome(), False)\n",
"\n",
" print('Test: General case: Palindrome with even length')\n",
" head = Node('a')\n",
" linked_list = MyLinkedList(head)\n",
@@ -194,11 +187,38 @@
" linked_list.append(1)\n",
" assert_equal(linked_list.is_palindrome(), True)\n",
" \n",
" print('Success: test_is_palindrome')\n",
" print('Success: test_palindrome')\n",
"\n",
"def main():\n",
" test = TestPalindrome()\n",
" test.test_palindrome()\n",
"\n",
"if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_is_palindrome()"
" main()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test: Empty list\n",
"Test: Single element list\n",
"Test: Two element list, not a palindrome\n",
"Test: General case: Palindrome with even length\n",
"Test: General case: Palindrome with odd length\n",
"Success: test_palindrome\n"
]
}
],
"source": [
"%run -i test_palindrome.py"
]
}
],