mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 17:38:02 +00:00
Added palindrome challenge.
This commit is contained in:
0
linked_lists/linked_list/__init__.py
Normal file
0
linked_lists/linked_list/__init__.py
Normal file
0
linked_lists/palindrome/__init__.py
Normal file
0
linked_lists/palindrome/__init__.py
Normal file
184
linked_lists/palindrome/palindrome_challenge.ipynb
Normal file
184
linked_lists/palindrome/palindrome_challenge.ipynb
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Problem: Determine if a linked list is a palindrome.\n",
|
||||||
|
"\n",
|
||||||
|
"* [Constraints](#Constraints)\n",
|
||||||
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
|
"* [Algorithm](#Algorithm)\n",
|
||||||
|
"* [Code](#Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Constraints\n",
|
||||||
|
"\n",
|
||||||
|
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||||
|
"\n",
|
||||||
|
"* Is a single character or number a palindrome?\n",
|
||||||
|
" * No\n",
|
||||||
|
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||||
|
" * Yes"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Test Cases\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"* 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"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Algorithm\n",
|
||||||
|
"\n",
|
||||||
|
"Refer to the solution notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Code"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run ../linked_list/linked_list.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"class MyLinkedList(LinkedList):\n",
|
||||||
|
" \n",
|
||||||
|
" def is_palindrome(self):\n",
|
||||||
|
" # TODO: Implement me\n",
|
||||||
|
" pass"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Unit Test"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# %load test_palindrome.py\n",
|
||||||
|
"from nose.tools import assert_equal\n",
|
||||||
|
"\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",
|
||||||
|
"\n",
|
||||||
|
" print('Test: Single element list')\n",
|
||||||
|
" head = Node(1)\n",
|
||||||
|
" linked_list = MyLinkedList(head)\n",
|
||||||
|
" assert_equal(linked_list.is_palindrome(), False)\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: Two element list, not a palindrome')\n",
|
||||||
|
" linked_list.append(2)\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",
|
||||||
|
" linked_list.append('b')\n",
|
||||||
|
" linked_list.append('b')\n",
|
||||||
|
" linked_list.append('a')\n",
|
||||||
|
" assert_equal(linked_list.is_palindrome(), True)\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: General case: Palindrome with odd length')\n",
|
||||||
|
" head = Node(1)\n",
|
||||||
|
" linked_list = MyLinkedList(head)\n",
|
||||||
|
" linked_list.append(2)\n",
|
||||||
|
" linked_list.append(3)\n",
|
||||||
|
" linked_list.append(2)\n",
|
||||||
|
" linked_list.append(1)\n",
|
||||||
|
" assert_equal(linked_list.is_palindrome(), True)\n",
|
||||||
|
" \n",
|
||||||
|
" print('Success: test_palindrome')\n",
|
||||||
|
"\n",
|
||||||
|
"def main():\n",
|
||||||
|
" test = TestPalindrome()\n",
|
||||||
|
" test.test_palindrome()\n",
|
||||||
|
"\n",
|
||||||
|
"if __name__ == '__main__':\n",
|
||||||
|
" main()"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 2",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python2"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 2
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython2",
|
||||||
|
"version": "2.7.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
@@ -41,12 +41,11 @@
|
|||||||
"## Test Cases\n",
|
"## Test Cases\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Empty list\n",
|
"* Empty list -> False\n",
|
||||||
"* Single element list\n",
|
"* Single element list -> False\n",
|
||||||
"* Two element list, not a palindrome\n",
|
"* Two or more element list, not a palindrome -> False\n",
|
||||||
"* Three element list, not a palindrome\n",
|
"* General case: Palindrome with even length -> True\n",
|
||||||
"* General case: Palindrome with even length\n",
|
"* General case: Palindrome with odd length -> True"
|
||||||
"* General case: Palindrome with odd length"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -78,11 +77,11 @@
|
|||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"%run linked_list.py"
|
"%run ../linked_list/linked_list.py"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -94,6 +93,7 @@
|
|||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class MyLinkedList(LinkedList):\n",
|
"class MyLinkedList(LinkedList):\n",
|
||||||
|
" \n",
|
||||||
" def is_palindrome(self):\n",
|
" def is_palindrome(self):\n",
|
||||||
" if self.head is None or self.head.next is None:\n",
|
" if self.head is None or self.head.next is None:\n",
|
||||||
" return False\n",
|
" return False\n",
|
||||||
@@ -145,21 +145,18 @@
|
|||||||
"name": "stdout",
|
"name": "stdout",
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"text": [
|
"text": [
|
||||||
"Test: Empty list\n",
|
"Overwriting test_palindrome.py\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"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"%%writefile test_palindrome.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Test(object):\n",
|
"\n",
|
||||||
" def test_is_palindrome(self):\n",
|
"class TestPalindrome(object):\n",
|
||||||
|
" \n",
|
||||||
|
" def test_palindrome(self):\n",
|
||||||
" print('Test: Empty list')\n",
|
" print('Test: Empty list')\n",
|
||||||
" linked_list = MyLinkedList()\n",
|
" linked_list = MyLinkedList()\n",
|
||||||
" assert_equal(linked_list.is_palindrome(), False)\n",
|
" assert_equal(linked_list.is_palindrome(), False)\n",
|
||||||
@@ -173,10 +170,6 @@
|
|||||||
" linked_list.append(2)\n",
|
" linked_list.append(2)\n",
|
||||||
" assert_equal(linked_list.is_palindrome(), False)\n",
|
" assert_equal(linked_list.is_palindrome(), False)\n",
|
||||||
"\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",
|
" print('Test: General case: Palindrome with even length')\n",
|
||||||
" head = Node('a')\n",
|
" head = Node('a')\n",
|
||||||
" linked_list = MyLinkedList(head)\n",
|
" linked_list = MyLinkedList(head)\n",
|
||||||
@@ -194,11 +187,38 @@
|
|||||||
" linked_list.append(1)\n",
|
" linked_list.append(1)\n",
|
||||||
" assert_equal(linked_list.is_palindrome(), True)\n",
|
" assert_equal(linked_list.is_palindrome(), True)\n",
|
||||||
" \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",
|
"\n",
|
||||||
"if __name__ == '__main__':\n",
|
"if __name__ == '__main__':\n",
|
||||||
" test = Test()\n",
|
" main()"
|
||||||
" test.test_is_palindrome()"
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|||||||
44
linked_lists/palindrome/test_palindrome.py
Normal file
44
linked_lists/palindrome/test_palindrome.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
from nose.tools import assert_equal
|
||||||
|
|
||||||
|
|
||||||
|
class TestPalindrome(object):
|
||||||
|
|
||||||
|
def test_palindrome(self):
|
||||||
|
print('Test: Empty list')
|
||||||
|
linked_list = MyLinkedList()
|
||||||
|
assert_equal(linked_list.is_palindrome(), False)
|
||||||
|
|
||||||
|
print('Test: Single element list')
|
||||||
|
head = Node(1)
|
||||||
|
linked_list = MyLinkedList(head)
|
||||||
|
assert_equal(linked_list.is_palindrome(), False)
|
||||||
|
|
||||||
|
print('Test: Two element list, not a palindrome')
|
||||||
|
linked_list.append(2)
|
||||||
|
assert_equal(linked_list.is_palindrome(), False)
|
||||||
|
|
||||||
|
print('Test: General case: Palindrome with even length')
|
||||||
|
head = Node('a')
|
||||||
|
linked_list = MyLinkedList(head)
|
||||||
|
linked_list.append('b')
|
||||||
|
linked_list.append('b')
|
||||||
|
linked_list.append('a')
|
||||||
|
assert_equal(linked_list.is_palindrome(), True)
|
||||||
|
|
||||||
|
print('Test: General case: Palindrome with odd length')
|
||||||
|
head = Node(1)
|
||||||
|
linked_list = MyLinkedList(head)
|
||||||
|
linked_list.append(2)
|
||||||
|
linked_list.append(3)
|
||||||
|
linked_list.append(2)
|
||||||
|
linked_list.append(1)
|
||||||
|
assert_equal(linked_list.is_palindrome(), True)
|
||||||
|
|
||||||
|
print('Success: test_palindrome')
|
||||||
|
|
||||||
|
def main():
|
||||||
|
test = TestPalindrome()
|
||||||
|
test.test_palindrome()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user