diff --git a/Nikhila_DS_Assignment_2.ipynb b/Nikhila_DS_Assignment_2.ipynb new file mode 100644 index 0000000..58adfc5 --- /dev/null +++ b/Nikhila_DS_Assignment_2.ipynb @@ -0,0 +1,273 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 2, 4, 5]\n" + ] + } + ], + "source": [ + "# Counting Sort\n", + "def countSort(l,k):\n", + " l1=[0 for i in l]\n", + " l2=[0 for i in range(k+1)]\n", + " \n", + " for i in range(0,len(l)):\n", + " l2[l[i]]=l2[l[i]]+1\n", + " \n", + " for i in range(1,k+1):\n", + " l2[i]=l2[i]+l2[i-1]\n", + " \n", + " for i in range(len(l)-1,-1,-1):\n", + " tmp=l[i]\n", + " tmp2=l2[tmp]-1\n", + " l1[tmp2]=tmp\n", + " l2[tmp]=l2[tmp]-1\n", + " return l1\n", + "\n", + "print(countSort([2,1,4,5,1,1],10))" + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": {}, + "outputs": [], + "source": [ + "#LINKED LIST IMPLEMENTATION\n", + "class Node():\n", + " def __init__(self,data):\n", + " self.data=data\n", + " self.next=None\n", + " \n", + "class LinkedListImpl():\n", + " def __init__(self):\n", + " self.head=None\n", + " self.len=0\n", + " def __str__(self):\n", + " s=self.head\n", + " if s is None:\n", + " return \"\"\n", + " st=str(s.data)\n", + " while s.next is not None:\n", + " s=s.next\n", + " st=st+\"-->\"+str(s.data)\n", + " return st\n", + " \n", + " def add(self,data):\n", + " if self.find(data) and self.len==1:\n", + " self.remove_last()\n", + " elif self.find(data):\n", + " self.remove(data[0])\n", + " n=Node(data)\n", + " self.len+=1\n", + " curr=self.head\n", + " if self.head==None:\n", + " self.head=n\n", + " else:\n", + " while(curr.next is not None):\n", + " curr=curr.next\n", + " curr.next=n\n", + " \n", + " def find(self,data):\n", + " f=self.head\n", + " fn=None\n", + " c=0\n", + " while (f is not None):\n", + " if f.data[0]==data[0]:\n", + " fn=f\n", + " c=c+1\n", + " break\n", + " f=f.next\n", + " c=c+1\n", + " if fn is not None:\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + " def get_val(self,data):\n", + " f=self.head\n", + " fn=None\n", + " c=0\n", + " while (f is not None):\n", + " if f.data[0]==data:\n", + " fn=f.data\n", + " c=c+1\n", + " break\n", + " f=f.next\n", + " c=c+1\n", + " if fn is not None:\n", + " print(fn[1])\n", + " #return fn\n", + " else:\n", + " print(\"Key not found\",data)\n", + " #return \"Key not found\",data\n", + " \n", + " def remove(self,data):\n", + " r=self.head\n", + " #print(self.len)\n", + " while(r is not None):\n", + " if r.data[0]==data:\n", + " if r.next is None:\n", + " self.remove_last()\n", + " \n", + " #break\n", + " else:\n", + " r1=r.next\n", + " d=r1.data\n", + " r.next=r.next.next\n", + " r.data=d\n", + " #break;\n", + " self.len-=1\n", + " break \n", + " else:\n", + " r=r.next\n", + " \n", + " def remove_last(self):\n", + " if self.len==1:\n", + " self.head=None\n", + " else:\n", + " r=self.head\n", + " for i in range(0,self.len):\n", + " if i==self.len-1:\n", + " r.next=None\n", + " else:\n", + " r=r.next\n", + " self.len-=1\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 134, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "Key not found 11\n", + "\n", + "(1, 2)-->(23, 4)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "(10, 3)\n" + ] + } + ], + "source": [ + "# Hash Table implementation using linear chaining. \n", + "# The key value pairs are stored in the form of tuples. \n", + "# If same key is inserted again, its value gets replaced with new one\n", + "\n", + "HashTable=[LinkedListImpl() for i in range(11)]\n", + "def Hash_fun(k):\n", + " return k%11\n", + "\n", + "def insert(k,v):\n", + " HashTable[Hash_fun(k)].add((k,v))\n", + " \n", + "def get_val(k):\n", + " HashTable[Hash_fun(k)].get_val(k)\n", + " \n", + "def delete(k):\n", + " HashTable[Hash_fun(k)].remove(k)\n", + " \n", + "insert(1,2)\n", + "insert(12,3)\n", + "insert(23,4)\n", + "delete(12)\n", + "insert(10,2)\n", + "insert(11,5)\n", + "insert(10,3)\n", + "delete(11)\n", + "get_val(10)\n", + "get_val(11)\n", + "for i in HashTable:\n", + " print(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 135, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211]" + ] + }, + "execution_count": 135, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Pattern\n", + "\n", + "def get_pattern(n):\n", + " p=[1]\n", + " for i in range(n-1):\n", + " s=str(p[-1])\n", + " k=\"\"\n", + " c=1\n", + " for i in range(len(s)):\n", + " if i