Преглед на файлове

added linked list content

Harsh Parikh преди 2 години
родител
ревизия
1e22af2bcb

+ 0 - 0
data_structures/arrays/THEORY.md → data_structures/array/THEORY.md


+ 0 - 0
data_structures/arrays/interview_questions/array_interview_questions.ipynb → data_structures/array/interview_questions/array_interview_questions.ipynb


+ 0 - 0
data_structures/arrays/practice/arrays.ipynb → data_structures/array/practice/arrays.ipynb


+ 0 - 0
data_structures/arrays/practice/arrays_practice_problems.ipynb → data_structures/array/practice/arrays_practice_problems.ipynb


+ 140 - 0
data_structures/dictionary/practice_problems/dictionary.ipynb

@@ -0,0 +1,140 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Dictionary basics"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "{'Key': 'value'}\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Insering an element inside a dictionary\n",
+    "mydict = dict()\n",
+    "mydict[\"Key\"] = \"value\"\n",
+    "print(mydict)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "France 1\n",
+      "Apple 2\n",
+      "Banana 3\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Traversing through a dictionary\n",
+    "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n",
+    "for key, value in mydict.items():\n",
+    "    print(key, value)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "{'Apple': 2, 'Banana': 3}\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Deleting an element from a dictionary\n",
+    "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n",
+    "mydict.pop(\"France\")\n",
+    "print(mydict)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Finding an element in a dictionary\n",
+    "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n",
+    "mydict.get(\"France\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "{}\n"
+     ]
+    }
+   ],
+   "source": [
+    "# Deleting all the elements from a dictionary\n",
+    "mydict = {\"France\" : 1, \"Apple\": 2, \"Banana\": 3}\n",
+    "mydict.clear()\n",
+    "print(mydict)"
+   ]
+  }
+ ],
+ "metadata": {
+  "interpreter": {
+   "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
+  },
+  "kernelspec": {
+   "display_name": "Python 3.8.3 ('base')",
+   "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.8.3"
+  },
+  "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 91 - 0
data_structures/dictionary/practice_problems/dictionary_practice.ipynb

@@ -0,0 +1,91 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Dictionary practice problems\n"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "### Problem 1\n",
+    "Given a paragraph, write a function which returns the word count of each element.\n",
+    "\n",
+    "- Input: \"Hello I am Nick Hello\"\n",
+    "- Output: \"Hello\":2, \"I\":1, \"am\":1, Nick:\"1\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "{'Commodo': 1, 'exercitation': 1, 'mollit': 3, 'culpa': 1, 'aute': 2, 'nostrud': 1, 'dolore.': 1, '\\\\': 1, 'Ullamco': 1, 'et': 1, 'sit': 2, 'eiusmod': 1, 'anim': 1, 'cillum': 2, 'ipsum': 2, 'amet': 1, 'ex': 1, 'occaecat': 1, 'irure.': 1, 'Occaecat': 1, 'eu': 1, 'aliqua': 1, 'enim': 1, 'sunt.': 1, 'Consequat': 1, 'magna': 1, 'elit': 1, 'irure': 1, 'et.': 1}\n"
+     ]
+    }
+   ],
+   "source": [
+    "def wordCount(text):\n",
+    "    counts = {}\n",
+    "    for word in text.split():\n",
+    "        if word not in counts:\n",
+    "            counts[word] = 0\n",
+    "        counts[word] += 1\n",
+    "    return counts\n",
+    "\n",
+    "paragraph = \"\"\"Commodo exercitation mollit mollit culpa aute nostrud dolore. \\ \n",
+    "            Ullamco et sit eiusmod anim cillum ipsum amet mollit ex occaecat aute irure. \\\n",
+    "            Occaecat eu aliqua cillum sit enim sunt. Consequat magna elit ipsum irure et.\"\"\"\n",
+    "\n",
+    "count = wordCount(paragraph)\n",
+    "print(count)        "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "interpreter": {
+   "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
+  },
+  "kernelspec": {
+   "display_name": "Python 3.8.3 ('base')",
+   "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.8.3"
+  },
+  "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 27 - 0
data_structures/linked_list/THEORY.md

@@ -0,0 +1,27 @@
+# Linked List
+
+
+<img src="https://media.geeksforgeeks.org/wp-content/cdn-uploads/gq/2013/03/Linkedlist.png" width="700" height="80" />
+<font size = "1">
+
+[Image Source](https://www.geeksforgeeks.org/types-of-linked-list/)</font>
+
+### Definition
+- A linked list is a sequence of data structures, which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array. <font size = "0.5"> [source](https://www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm#:~:text=A%20linked%20list%20is%20a,used%20data%20structure%20after%20array.) </font>
+### Basic elements of a linked list:
+1. Links
+1. Nodes
+1. References  
+### Types of linked lists
+1. Simple Linked List
+2. Doubly Linked List
+3. Circular Linked List.
+
+### Basic Operations in the linked list.
+1. Insertion: Adding an element at the beginning of the list.
+1. Deletion: Deleting an element at the beginning of the list.
+1. Display: Displays the complete list.
+1. Search: Searches an element using it's key
+1. Delete: Deletes an element using it's key.
+
+[More about linked lists](https://en.wikipedia.org/wiki/Linked_list#)

+ 135 - 0
data_structures/linked_list/practice_problems/linked_list.ipynb

@@ -0,0 +1,135 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Linked Lists"
+   ]
+  },
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "\n",
+    "\n",
+    "### Steps for creating a linked list:\n",
+    "1. Create head and tail, initialize with null\n",
+    "2. Create a blank node and assign a value to it and reference to null.\n",
+    "3. Link head and tail with these nodes\n",
+    "\n",
+    "[head] -> [node] -> [tail]\n",
+    "\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [
+    {
+     "ename": "AttributeError",
+     "evalue": "'NoneType' object has no attribute 'value'",
+     "output_type": "error",
+     "traceback": [
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
+      "\u001b[0;32m<ipython-input-4-75cdb50160b5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlinkedlist\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[0m",
+      "\u001b[0;32m<ipython-input-4-75cdb50160b5>\u001b[0m in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m     24\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     25\u001b[0m \u001b[0mlinkedlist\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtail\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnode_2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 26\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mlinkedlist\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[0m",
+      "\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'value'"
+     ]
+    }
+   ],
+   "source": [
+    "class Node:\n",
+    "    def __init__(self, value = None):\n",
+    "        self.value = value\n",
+    "        self.next = None\n",
+    "\n",
+    "class LinkedList:\n",
+    "    def __init__(self):\n",
+    "        self.head = None\n",
+    "        self.tail = None\n",
+    "        self.next = None\n",
+    "\n",
+    "    def __iter__(self):\n",
+    "        node = self.head\n",
+    "        while node:\n",
+    "            yield node\n",
+    "            node = node.next\n",
+    "\n",
+    "    def __insert__(self, value, location = None):\n",
+    "        new_node = Node(value)\n",
+    "        if self.head is None:\n",
+    "            self.head = new_node\n",
+    "            self.tail = new_node\n",
+    "\n",
+    "        else:\n",
+    "\n",
+    "            if location == 0:\n",
+    "                new_node.next = self.head\n",
+    "                self.head = new_node\n",
+    "\n",
+    "            elif location == -1:\n",
+    "                new_node.next = Node\n",
+    "                self.tail.next = new_node\n",
+    "                self.tail = new_node\n",
+    "\n",
+    "            else:\n",
+    "                temp = self.head\n",
+    "                idx = 0\n",
+    "                while idx < location -1:\n",
+    "                    temp = temp.next\n",
+    "                    idx += 1\n",
+    "                next_node = temp.next\n",
+    "                temp.next = new_node\n",
+    "                new_node.next = next_node\n",
+    "                \n",
+    "                if temp == self.tail:\n",
+    "                    self.tail = new_node\n",
+    "\n",
+    "\n",
+    "linkedlist = LinkedList()\n",
+    "node_1 = Node(1)\n",
+    "node_2 = Node(2)\n",
+    "\n",
+    "linkedlist.head = node_1\n",
+    "linkedlist.head.next = node_2\n",
+    "linkedlist.tail = node_2\n",
+    "print([node.value for node in linkedlist])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "interpreter": {
+   "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
+  },
+  "kernelspec": {
+   "display_name": "Python 3.8.3 ('base')",
+   "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.8.3"
+  },
+  "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}