{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Challenge Notebook"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Problem: Implement a linked list with insert, append, find, delete, length, and print methods.\n",
    "\n",
    "* [Constraints](#Constraints)\n",
    "* [Test Cases](#Test-Cases)\n",
    "* [Algorithm](#Algorithm)\n",
    "* [Code](#Code)\n",
    "* [Unit Test](#Unit-Test)\n",
    "* [Solution Notebook](#Solution-Notebook)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constraints\n",
    "\n",
    "* Can we assume this is a non-circular, singly linked list?\n",
    "    * Yes\n",
    "* Do we keep track of the tail or just the head?\n",
    "    * Just the head\n",
    "* Can we insert None values?\n",
    "    * No"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test Cases\n",
    "\n",
    "### Insert to Front\n",
    "\n",
    "* Insert a None\n",
    "* Insert in an empty list\n",
    "* Insert in a list with one element or more elements\n",
    "\n",
    "### Append\n",
    "\n",
    "* Append a None\n",
    "* Append in an empty list\n",
    "* Insert in a list with one element or more elements\n",
    "\n",
    "### Find\n",
    "\n",
    "* Find a None\n",
    "* Find in an empty list\n",
    "* Find in a list with one element or more matching elements\n",
    "* Find in a list with no matches\n",
    "\n",
    "### Delete\n",
    "\n",
    "* Delete a None\n",
    "* Delete in an empty list\n",
    "* Delete in a list with one element or more matching elements\n",
    "* Delete in a list with no matches\n",
    "\n",
    "### Length\n",
    "\n",
    "* Length of zero or more elements\n",
    "\n",
    "### Print\n",
    "\n",
    "* Print an empty list\n",
    "* Print a list with one or more elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Algorithm\n",
    "\n",
    "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/linked_lists/linked_list/linked_list_solution.ipynb).  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": [
    "class Node(object):\n",
    "\n",
    "    def __init__(self, data, next_node=None):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def __str__(self):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "\n",
    "class LinkedList(object):\n",
    "\n",
    "    def __init__(self, head=None):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def __len__(self):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def insert_to_front(self, data):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def append(self, data):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def find(self, data):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def delete(self, data):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def print_list(self):\n",
    "        pass\n",
    "        # TODO: Implement me\n",
    "\n",
    "    def get_all_data(self):\n",
    "        pass\n",
    "        # TODO: Implement me"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "**The following unit test is expected to fail until you solve the challenge.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# %load test_linked_list.py\n",
    "from nose.tools import assert_equal\n",
    "\n",
    "\n",
    "class TestLinkedList(object):\n",
    "\n",
    "    def test_insert_to_front(self):\n",
    "        print('Test: insert_to_front on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        linked_list.insert_to_front(10)\n",
    "        assert_equal(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: insert_to_front on a None')\n",
    "        linked_list.insert_to_front(None)\n",
    "        assert_equal(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: insert_to_front general case')\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
    "\n",
    "        print('Success: test_insert_to_front\\n')\n",
    "\n",
    "    def test_append(self):\n",
    "        print('Test: append on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        linked_list.append(10)\n",
    "        assert_equal(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: append a None')\n",
    "        linked_list.append(None)\n",
    "        assert_equal(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: append general case')\n",
    "        linked_list.append('a')\n",
    "        linked_list.append('bc')\n",
    "        assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
    "\n",
    "        print('Success: test_append\\n')\n",
    "\n",
    "    def test_find(self):\n",
    "        print('Test: find on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        node = linked_list.find('a')\n",
    "        assert_equal(node, None)\n",
    "\n",
    "        print('Test: find a None')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        node = linked_list.find(None)\n",
    "        assert_equal(node, None)\n",
    "\n",
    "        print('Test: find general case with matches')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        node = linked_list.find('a')\n",
    "        assert_equal(str(node), 'a')\n",
    "\n",
    "        print('Test: find general case with no matches')\n",
    "        node = linked_list.find('aaa')\n",
    "        assert_equal(node, None)\n",
    "\n",
    "        print('Success: test_find\\n')\n",
    "\n",
    "    def test_delete(self):\n",
    "        print('Test: delete on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        linked_list.delete('a')\n",
    "        assert_equal(linked_list.get_all_data(), [])\n",
    "\n",
    "        print('Test: delete a None')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.delete(None)\n",
    "        assert_equal(linked_list.get_all_data(), [10])\n",
    "\n",
    "        print('Test: delete general case with matches')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        linked_list.delete('a')\n",
    "        assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
    "\n",
    "        print('Test: delete general case with no matches')\n",
    "        linked_list.delete('aa')\n",
    "        assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
    "\n",
    "        print('Success: test_delete\\n')\n",
    "\n",
    "    def test_len(self):\n",
    "        print('Test: len on an empty list')\n",
    "        linked_list = LinkedList(None)\n",
    "        assert_equal(len(linked_list), 0)\n",
    "\n",
    "        print('Test: len general case')\n",
    "        head = Node(10)\n",
    "        linked_list = LinkedList(head)\n",
    "        linked_list.insert_to_front('a')\n",
    "        linked_list.insert_to_front('bc')\n",
    "        assert_equal(len(linked_list), 3)\n",
    "\n",
    "        print('Success: test_len\\n')\n",
    "\n",
    "\n",
    "def main():\n",
    "    test = TestLinkedList()\n",
    "    test.test_insert_to_front()\n",
    "    test.test_append()\n",
    "    test.test_find()\n",
    "    test.test_delete()\n",
    "    test.test_len()\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Solution Notebook\n",
    "\n",
    "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/linked_lists/linked_list/linked_list_solution.ipynb) for a discussion on algorithms and code solutions."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
