diff --git a/data_structures/2_Arrays/array_solution.ipynb b/data_structures/2_Arrays/array_solution.ipynb new file mode 100644 index 0000000..ca21f1f --- /dev/null +++ b/data_structures/2_Arrays/array_solution.ipynb @@ -0,0 +1,32 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "unit4sprint2", + "language": "python", + "name": "unit4sprint2" + }, + "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.7.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/data_structures/3_LinkedList/my_linked_list_class.ipynb b/data_structures/3_LinkedList/my_linked_list_class.ipynb new file mode 100644 index 0000000..dc03758 --- /dev/null +++ b/data_structures/3_LinkedList/my_linked_list_class.ipynb @@ -0,0 +1,301 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# linked list:\n", + "# array vs linked list\n", + " # addeding elements to array has to move all other elements pasted the inserted element\n", + " # or if the array is full it has to copy the contents to another array and allocate more space\n", + " # O(n)\n", + " \n", + "# linked list: -> [] -> [] -> null\n", + "# adding/deleting to the beginning is O(n) and insert is O(n)\n", + " # dont need to pre allocate space\n", + " # insertion is easier ( dont have to shift all elements past index i)\n", + " # treversal O(n)\n", + " # Accessing element value O(n)\n", + "\n", + "# double linked listP: [] <-> [] <-> [] <-> null" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [], + "source": [ + "# linked list class\n", + "\n", + "# class for \"node\" or in common terms \"element\"\n", + "class Node:\n", + " def __init__(self, data=None, next=None):\n", + " self.data = data\n", + " self.next = next\n", + "\n", + "class LinkedList:\n", + " def __init__(self):\n", + " self.head = None # points to the head of the linked list\n", + " \n", + " def insert_at_begining(self, data):\n", + " \n", + " # node with value of \"data\" and next element will be the head\n", + " # we add the element to the beginning and set the \"Next\" argument to the previous head\n", + " # this puts the current data to the beginning of the list.\n", + " node = Node(data, self.head)\n", + " \n", + " # we then set the current node as the head.\n", + " self.head = node\n", + " \n", + " def insert_at_end(self, data):\n", + " \n", + " # if linked list is empty set the current data to the head.\n", + " if self.head is None:\n", + " self.head = Node(data, None)\n", + " return\n", + " \n", + " # iterate through the entire linked list\n", + " # once we hit an itr that is None we know we're at the end\n", + " # we then set that node to the current data then set the next element to None\n", + " itr = self.head\n", + " while itr.next:\n", + " itr = itr.next\n", + " \n", + " itr.next = Node(data, None)\n", + " \n", + " # insert a list of values as a linked list\n", + " def insert_values(self, data_list):\n", + " self.head = None\n", + " for data in data_list:\n", + " self.insert_at_end(data)\n", + " \n", + " def get_length(self):\n", + " count = 0\n", + " itr = self.head\n", + " while itr:\n", + " count += 1\n", + " itr = itr.next\n", + " return count\n", + " \n", + " def remove_at(self, index):\n", + " # if the index is less then 0 or greater than the count, its invalid\n", + " if index < 0 or index >= self.get_length():\n", + " raise Exception('not valid index')\n", + " \n", + " # if index is 0 then just point the head to the next element.\n", + " # python handles garbage\n", + " if index==0:\n", + " self.head = self.head.next\n", + " \n", + " # keep count of where we are\n", + " count = 0\n", + " # start at the head\n", + " itr = self.head\n", + " # iter through linked list\n", + " while itr:\n", + " # if we reach the index BEFORE the index to drop move that index to the next.next\n", + " # skipping the one we want to drop python garbage will clean.\n", + " if count == index - 1:\n", + " itr.next = itr.next.next\n", + " break\n", + " # if not n-1 count + 1 move to next.\n", + " itr = itr.next\n", + " count += 1\n", + " \n", + " def insert_at(self, index, data):\n", + " if index < 0 or index >= self.get_length():\n", + " raise Exception('invalid index')\n", + " \n", + " if index == 0:\n", + " self.insert_at_begining(data)\n", + " \n", + " count = 0\n", + " itr = self.head\n", + " while itr:\n", + " if count == index - 1:\n", + " node = Node(data,itr.next)\n", + " itr.next = node\n", + " break\n", + " itr = itr.next\n", + " count += 1\n", + " \n", + " \n", + " # following links and print\n", + " def print(self):\n", + " \n", + " # if the list is empty print nothing\n", + " if self.head is None:\n", + " print(\"list is empty\")\n", + " return\n", + " \n", + " # while itr is anything other than None, print it, else quit.\n", + " itr = self.head\n", + " linked_list = ''\n", + " \n", + " while itr:\n", + " linked_list += str(itr.data) + ' --> '\n", + " itr = itr.next\n", + " \n", + " print(linked_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6 --> 5 --> 7 --> \n" + ] + } + ], + "source": [ + "ll = LinkedList()\n", + "ll.insert_at_begining(5)\n", + "ll.insert_at_begining(6)\n", + "ll.insert_at_end(7)\n", + "\n", + "ll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 --> 2 --> 3 --> 4 --> 5 --> \n" + ] + } + ], + "source": [ + "ll.insert_values([1,2,3,4,5])\n", + "ll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 117, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ll.get_length()" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 --> 2 --> 3 --> 4 --> 5 --> \n", + "1 --> 2 --> 3 --> 5 --> \n" + ] + } + ], + "source": [ + "ll.insert_values([1,2,3,4,5])\n", + "ll.print()\n", + "ll.remove_at(3)\n", + "ll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": {}, + "outputs": [ + { + "ename": "Exception", + "evalue": "not valid index", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mException\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mll\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove_at\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m\u001b[0m in \u001b[0;36mremove_at\u001b[0;34m(self, index)\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;31m# if the index is less then 0 or greater than the count, its invalid\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 55\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mindex\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mindex\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_length\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 56\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'not valid index'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 57\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0;31m# if index is 0 then just point the head to the next element.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mException\u001b[0m: not valid index" + ] + } + ], + "source": [ + "ll.remove_at(30)" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 --> 2 --> 3 --> 4 --> 5 --> \n", + "1 --> 2 --> 3 --> 5 --> \n", + "1 --> 2 --> 3 --> 4 --> 5 --> \n" + ] + } + ], + "source": [ + "ll.insert_values([1,2,3,4,5])\n", + "ll.print()\n", + "ll.remove_at(3)\n", + "ll.print()\n", + "ll.insert_at(3,4)\n", + "ll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "unit4sprint2", + "language": "python", + "name": "unit4sprint2" + }, + "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.7.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}