├── README.md
├── Python DS
└── Basics of python
│ └── main.py
├── ReverseString.ipynb
├── Append_in_Link_List.ipynb
├── Prepend_in_Link_list.ipynb
├── main.ipynb
├── Convert_Decimal_Integer_to_Binary.ipynb
├── Insert_After_Node.ipynb
├── Circular_Link_List_Insertion.ipynb
├── stack.ipynb
├── Remove_Node_Circular_link_list.ipynb
├── Deletion_by_Value_in_Link_list.ipynb
├── Iterative_Implementation_link_list.ipynb
├── Deletion_by_Position.ipynb
├── Recursive_Implementation_.ipynb
├── Reverse_Node.ipynb
├── Swap_Node.ipynb
├── Is_Circular_Linked_List.ipynb
├── Data_Structure_&_Data_Types_.ipynb
├── Merge_Two_Sorted_Link_List.ipynb
├── Remove_Duplicates_in_link_list.ipynb
├── Functions.ipynb
├── BasicCommands.ipynb
├── palindrome_link_list_.ipynb
├── Move_Tail_to_head_in_link_list.ipynb
├── Sum_Two_Linked_Lists.ipynb
└── Introduction_&_Implementation_of_List_,Tuple_&_Dictionary_in_Python_.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # Data-Structures-Algorithms-with-Python
2 | This Repository contain Programs related to the problems that how we structure of our data using Python
3 |
--------------------------------------------------------------------------------
/Python DS/Basics of python/main.py:
--------------------------------------------------------------------------------
1 | print(2+202+307-200)
2 | print(2/202+307-200)
3 | print("Welcome to Data Structures & Algorithms Course(UCC-227205)")
4 | course_name = "DSA Python Class"
5 | print(course_name)
6 | course_name = input("Enter your course Name:")
7 | print(course_name)
8 |
--------------------------------------------------------------------------------
/ReverseString.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyMEiXKXaKbdxlHSy9SAUC8N",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 8,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "bgY5_XIseZUx",
37 | "outputId": "6ecebe81-7467-4c12-9ed8-20a03f893851"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "noitacudE\n"
45 | ]
46 | }
47 | ],
48 | "source": [
49 | "input_str = \"Education\"\n",
50 | "print(input_str[::-1])"
51 | ]
52 | }
53 | ]
54 | }
--------------------------------------------------------------------------------
/Append_in_Link_List.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyM686aaRiPAy3YEnjdd7avy",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "uZV1tYTiJzlg",
37 | "outputId": "270ee299-0675-4827-fe7a-c0648bd279bf"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "A\n",
45 | "B\n",
46 | "C\n",
47 | "D\n"
48 | ]
49 | }
50 | ],
51 | "source": [
52 | "class Node:\n",
53 | " def __init__(self, data):\n",
54 | " self.data = data\n",
55 | " self.next = None\n",
56 | "\n",
57 | "class LinkedList:\n",
58 | " def __init__(self):\n",
59 | " self.head = None\n",
60 | " \n",
61 | " def print_list(self):\n",
62 | " cur_node = self.head\n",
63 | " while cur_node:\n",
64 | " print(cur_node.data)\n",
65 | " cur_node = cur_node.next\n",
66 | "\n",
67 | " def append(self, data):\n",
68 | " new_node = Node(data)\n",
69 | " if self.head is None:\n",
70 | " self.head = new_node\n",
71 | " return\n",
72 | " last_node = self.head\n",
73 | " while last_node.next:\n",
74 | " last_node = last_node.next\n",
75 | " last_node.next = new_node\n",
76 | " \n",
77 | "llist = LinkedList()\n",
78 | "llist.append(\"A\")\n",
79 | "llist.append(\"B\")\n",
80 | "llist.append(\"C\")\n",
81 | "llist.append(\"D\")\n",
82 | "\n",
83 | "\n",
84 | "llist.print_list() "
85 | ]
86 | }
87 | ]
88 | }
--------------------------------------------------------------------------------
/Prepend_in_Link_list.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyMu3Y+obd9MMfXhH/IHU0Lc",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "8niPqFnnKCUx",
37 | "outputId": "33958274-65d9-4615-e8ff-4a1c7cf83f3f"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "D\n",
45 | "A\n",
46 | "B\n",
47 | "C\n"
48 | ]
49 | }
50 | ],
51 | "source": [
52 | "class Node:\n",
53 | " def __init__(self, data):\n",
54 | " self.data = data\n",
55 | " self.next = None\n",
56 | "\n",
57 | "class LinkedList:\n",
58 | " def __init__(self):\n",
59 | " self.head = None\n",
60 | " \n",
61 | " def print_list(self):\n",
62 | " cur_node = self.head\n",
63 | " while cur_node:\n",
64 | " print(cur_node.data)\n",
65 | " cur_node = cur_node.next\n",
66 | "\n",
67 | " def append(self, data):\n",
68 | " new_node = Node(data)\n",
69 | " if self.head is None:\n",
70 | " self.head = new_node\n",
71 | " return\n",
72 | " last_node = self.head\n",
73 | " while last_node.next:\n",
74 | " last_node = last_node.next\n",
75 | " last_node.next = new_node\n",
76 | "\n",
77 | " def prepend(self, data):\n",
78 | " new_node = Node(data)\n",
79 | "\n",
80 | " new_node.next = self.head\n",
81 | " self.head = new_node\n",
82 | " \n",
83 | " \n",
84 | "llist = LinkedList()\n",
85 | "llist.append(\"A\")\n",
86 | "llist.append(\"B\")\n",
87 | "llist.append(\"C\")\n",
88 | "\n",
89 | "llist.prepend(\"D\")\n",
90 | "\n",
91 | "llist.print_list() "
92 | ]
93 | }
94 | ]
95 | }
--------------------------------------------------------------------------------
/main.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyM2vXzXQhLRn7q4QuBnPl9Q",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "_XuZHN66drMe"
34 | },
35 | "outputs": [],
36 | "source": [
37 | "from stack import Stack\n",
38 | "\n",
39 | "def is_match(p1, p2):\n",
40 | " if p1 == \"(\" and p2 == \")\":\n",
41 | " return True\n",
42 | " elif p1 == \"{\" and p2 == \"}\":\n",
43 | " return True\n",
44 | " elif p1 == \"[\" and p2 == \"]\":\n",
45 | " return True\n",
46 | " else:\n",
47 | " return False\n",
48 | "\n",
49 | "\n",
50 | "def is_paren_balanced(paren_string):\n",
51 | " s = Stack()\n",
52 | " is_balanced = True\n",
53 | " index = 0\n",
54 | "\n",
55 | " while index < len(paren_string) and is_balanced:\n",
56 | " paren = paren_string[index]\n",
57 | " if paren in \"([{\":\n",
58 | " s.push(paren)\n",
59 | " else:\n",
60 | " if s.is_empty():\n",
61 | " is_balanced = False\n",
62 | " break\n",
63 | " else:\n",
64 | " top = s.pop()\n",
65 | " if not is_match(top, paren):\n",
66 | " is_balanced = False\n",
67 | " break\n",
68 | " index += 1\n",
69 | "\n",
70 | " if s.is_empty() and is_balanced:\n",
71 | " return True\n",
72 | " else:\n",
73 | " return False\n",
74 | "\n",
75 | "print(\"String : (((({})))) Balanced or not?\")\n",
76 | "print(is_paren_balanced(\"(((({}))))\"))\n",
77 | "\n",
78 | "print(\"String : [][]]] Balanced or not?\")\n",
79 | "print(is_paren_balanced(\"[][]]]\"))\n",
80 | "\n",
81 | "print(\"String : [][] Balanced or not?\")\n",
82 | "print(is_paren_balanced(\"[][]\"))"
83 | ]
84 | }
85 | ]
86 | }
--------------------------------------------------------------------------------
/Convert_Decimal_Integer_to_Binary.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyNAtb/xq1W/DTz9r7EMSJYI",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 2,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "ocCqgOnuKgkU",
37 | "outputId": "97ba8e00-0758-46e5-eb61-caa2f3fd5e33"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "111000\n",
45 | "10\n",
46 | "100000\n",
47 | "1010\n",
48 | "True\n"
49 | ]
50 | }
51 | ],
52 | "source": [
53 | "class Stack():\n",
54 | " def __init__(self):\n",
55 | " self.items = []\n",
56 | "\n",
57 | " def push(self, item):\n",
58 | " self.items.append(item)\t\t\t\t\n",
59 | "\n",
60 | " def pop(self):\n",
61 | " return self.items.pop()\n",
62 | " \n",
63 | " def is_empty(self):\n",
64 | " return self.items == []\n",
65 | " \n",
66 | " def peek(self):\n",
67 | " if not self.is_empty():\n",
68 | " return self.items[-1]\n",
69 | " \n",
70 | " def get_stack(self):\n",
71 | " return self.items\n",
72 | "def convert_int_to_bin(dec_num):\n",
73 | " \n",
74 | " if dec_num == 0:\n",
75 | " return 0\n",
76 | " \n",
77 | " s = Stack()\n",
78 | "\n",
79 | " while dec_num > 0:\n",
80 | " remainder = dec_num % 2\n",
81 | " s.push(remainder)\n",
82 | " dec_num = dec_num // 2\n",
83 | "\n",
84 | " bin_num = \"\"\n",
85 | " while not s.is_empty():\n",
86 | " bin_num += str(s.pop())\n",
87 | "\n",
88 | " return bin_num\n",
89 | "\n",
90 | "print(convert_int_to_bin(56))\n",
91 | "print(convert_int_to_bin(2))\n",
92 | "print(convert_int_to_bin(32))\n",
93 | "print(convert_int_to_bin(10))\n",
94 | "\n",
95 | "print(int(convert_int_to_bin(56),2)==56)"
96 | ]
97 | }
98 | ]
99 | }
--------------------------------------------------------------------------------
/Insert_After_Node.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOoO1waiQgJ+H/SfHdXXLPS",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "oXdQzbdIKai0",
37 | "outputId": "458817e4-86e4-4f58-a1ec-5421742ba04c"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "A\n",
45 | "B\n",
46 | "D\n",
47 | "C\n"
48 | ]
49 | }
50 | ],
51 | "source": [
52 | "class Node:\n",
53 | " def __init__(self, data):\n",
54 | " self.data = data\n",
55 | " self.next = None\n",
56 | "\n",
57 | "class LinkedList:\n",
58 | " def __init__(self):\n",
59 | " self.head = None\n",
60 | " \n",
61 | " def print_list(self):\n",
62 | " cur_node = self.head\n",
63 | " while cur_node:\n",
64 | " print(cur_node.data)\n",
65 | " cur_node = cur_node.next\n",
66 | "\n",
67 | " def append(self, data):\n",
68 | " new_node = Node(data)\n",
69 | " if self.head is None:\n",
70 | " self.head = new_node\n",
71 | " return\n",
72 | " last_node = self.head\n",
73 | " while last_node.next:\n",
74 | " last_node = last_node.next\n",
75 | " last_node.next = new_node\n",
76 | "\n",
77 | " def prepend(self, data):\n",
78 | " new_node = Node(data)\n",
79 | "\n",
80 | " new_node.next = self.head\n",
81 | " self.head = new_node\n",
82 | " \n",
83 | " def insert_after_node(self, prev_node, data):\n",
84 | " if not prev_node:\n",
85 | " print(\"Previous node does not exist.\")\n",
86 | " return\n",
87 | " new_node = Node(data)\n",
88 | "\n",
89 | " new_node.next = prev_node.next\n",
90 | " prev_node.next = new_node\n",
91 | "\n",
92 | " \n",
93 | " \n",
94 | "llist = LinkedList()\n",
95 | "llist.append(\"A\")\n",
96 | "llist.append(\"B\")\n",
97 | "llist.append(\"C\")\n",
98 | "\n",
99 | "\n",
100 | "llist.insert_after_node(llist.head.next, \"D\")\n",
101 | "\n",
102 | "llist.print_list() "
103 | ]
104 | }
105 | ]
106 | }
--------------------------------------------------------------------------------
/Circular_Link_List_Insertion.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOvDT17g3+Px+nM1ZVF4pGH",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "8A9kPjQz_Sk_",
37 | "outputId": "0cb4a4e5-833b-4b33-b4ba-c3d34bd4fe23"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "A\n",
45 | "B\n",
46 | "C\n",
47 | "D\n"
48 | ]
49 | }
50 | ],
51 | "source": [
52 | "class Node:\n",
53 | " def __init__(self, data):\n",
54 | " self.data = data \n",
55 | " self.next = None\n",
56 | "\n",
57 | "\n",
58 | "class CircularLinkedList:\n",
59 | " def __init__(self):\n",
60 | " self.head = None \n",
61 | "\n",
62 | " def prepend(self, data):\n",
63 | " new_node = Node(data)\n",
64 | " cur = self.head \n",
65 | " new_node.next = self.head\n",
66 | "\n",
67 | " if not self.head:\n",
68 | " new_node.next = new_node\n",
69 | " else:\n",
70 | " while cur.next != self.head:\n",
71 | " cur = cur.next\n",
72 | " cur.next = new_node\n",
73 | " self.head = new_node\n",
74 | "\n",
75 | " def append(self, data):\n",
76 | " if not self.head:\n",
77 | " self.head = Node(data)\n",
78 | " self.head.next = self.head\n",
79 | " else:\n",
80 | " new_node = Node(data)\n",
81 | " cur = self.head\n",
82 | " while cur.next != self.head:\n",
83 | " cur = cur.next\n",
84 | " cur.next = new_node\n",
85 | " new_node.next = self.head\n",
86 | "\n",
87 | " def print_list(self):\n",
88 | " cur = self.head \n",
89 | "\n",
90 | " while cur:\n",
91 | " print(cur.data)\n",
92 | " cur = cur.next\n",
93 | " if cur == self.head:\n",
94 | " break\n",
95 | "\n",
96 | "\n",
97 | "cllist = CircularLinkedList()\n",
98 | "cllist.append(\"C\")\n",
99 | "cllist.append(\"D\")\n",
100 | "cllist.prepend(\"B\")\n",
101 | "cllist.prepend(\"A\")\n",
102 | "cllist.print_list()"
103 | ]
104 | }
105 | ]
106 | }
--------------------------------------------------------------------------------
/stack.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyO3hqDX8Aa86ec2Qsep/vGr",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "**Stack Operations**\n"
33 | ],
34 | "metadata": {
35 | "id": "kYz6zIwLaokE"
36 | }
37 | },
38 | {
39 | "cell_type": "markdown",
40 | "source": [
41 | "**Push Operation**
\n",
42 | "The operation to insert elements in a stack is called push. When we push the book on a stack, we put the book on the previous top element which means that the new book becomes the top element. This is what we mean when we use the push operation, we push elements onto a stack. We insert elements onto a stack and the last element to be pushed is the new top of the stack.
\n",
43 | "**Pop**
\n",
44 | "There is another operation that we can perform on the stack, popping. Popping is when we take the top book of the stack and put it down. This implies that when we remove an element from the stack, the stack follows the First-In, Last Out property. This means that the top element, the last to be inserted, is removed when we perform the pop operation.\n",
45 | "\n",
46 | "Push and Pop are two fundamental routines that we’ll need for this data structure.\n",
47 | "\n",
48 | "**Peek**
\n",
49 | "Another thing that we can do is view the top element of the stack so we can ask the data structure: “What’s the top element?” and it can give that to us using the peek operation. Note that the peek operation does not remove the top element, it merely returns it."
50 | ],
51 | "metadata": {
52 | "id": "jf3-p5O7a4XH"
53 | }
54 | },
55 | {
56 | "cell_type": "code",
57 | "execution_count": 4,
58 | "metadata": {
59 | "id": "XD3mFh6IGlFv"
60 | },
61 | "outputs": [],
62 | "source": [
63 | "\"\"\"\n",
64 | "Stack Data Structure.\n",
65 | "\"\"\"\n",
66 | "class Stack():\n",
67 | " def __init__(self):\n",
68 | " self.items = []\n",
69 | "\n",
70 | " def push(self, item):\n",
71 | " self.items.append(item)\t\t\t\t\n",
72 | "\n",
73 | " def pop(self):\n",
74 | " return self.items.pop()\n",
75 | " \n",
76 | " def is_empty(self):\n",
77 | " return self.items == []\n",
78 | " \n",
79 | " def peek(self):\n",
80 | " if not self.is_empty():\n",
81 | " return self.items[-1]\n",
82 | " \n",
83 | " def get_stack(self):\n",
84 | " return self.items\n",
85 | "\n",
86 | "# myStack = Stack()\n",
87 | "# myStack.push(\"A\")\n",
88 | "# myStack.push(\"B\")\n",
89 | "# myStack.push(\"C\")\n",
90 | "# myStack.push(\"D\")\n",
91 | "# print(myStack.peek())\n"
92 | ]
93 | }
94 | ]
95 | }
--------------------------------------------------------------------------------
/Remove_Node_Circular_link_list.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyPpHbGQKlqyzo5R0zm4xuBn",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "SPADPe5N_26o"
34 | },
35 | "outputs": [],
36 | "source": [
37 | "class Node:\n",
38 | " def __init__(self, data):\n",
39 | " self.data = data \n",
40 | " self.next = None\n",
41 | "\n",
42 | "\n",
43 | "class CircularLinkedList:\n",
44 | " def __init__(self):\n",
45 | " self.head = None \n",
46 | "\n",
47 | " def prepend(self, data):\n",
48 | " new_node = Node(data)\n",
49 | " cur = self.head \n",
50 | " new_node.next = self.head\n",
51 | "\n",
52 | " if not self.head:\n",
53 | " new_node.next = new_node\n",
54 | " else:\n",
55 | " while cur.next != self.head:\n",
56 | " cur = cur.next\n",
57 | " cur.next = new_node\n",
58 | " self.head = new_node\n",
59 | "\n",
60 | " def append(self, data):\n",
61 | " if not self.head:\n",
62 | " self.head = Node(data)\n",
63 | " self.head.next = self.head\n",
64 | " else:\n",
65 | " new_node = Node(data)\n",
66 | " cur = self.head\n",
67 | " while cur.next != self.head:\n",
68 | " cur = cur.next\n",
69 | " cur.next = new_node\n",
70 | " new_node.next = self.head\n",
71 | "\n",
72 | " def print_list(self):\n",
73 | " cur = self.head \n",
74 | "\n",
75 | " while cur:\n",
76 | " print(cur.data)\n",
77 | " cur = cur.next\n",
78 | " if cur == self.head:\n",
79 | " break\n",
80 | "\n",
81 | " def remove(self, key):\n",
82 | " if self.head:\n",
83 | " if self.head.data == key:\n",
84 | " cur = self.head \n",
85 | " while cur.next != self.head:\n",
86 | " cur = cur.next \n",
87 | " if self.head == self.head.next:\n",
88 | " self.head = None\n",
89 | " else:\n",
90 | " cur.next = self.head.next\n",
91 | " self.head = self.head.next\n",
92 | " else:\n",
93 | " cur = self.head \n",
94 | " prev = None \n",
95 | " while cur.next != self.head:\n",
96 | " prev = cur \n",
97 | " cur = cur.next\n",
98 | " if cur.data == key:\n",
99 | " prev.next = cur.next \n",
100 | " cur = cur.next\n",
101 | "\n",
102 | "\n",
103 | "cllist = CircularLinkedList()\n",
104 | "cllist.append(\"A\")\n",
105 | "cllist.append(\"B\")\n",
106 | "cllist.append(\"C\")\n",
107 | "cllist.append(\"D\")\n",
108 | "\n",
109 | "cllist.remove(\"A\")\n",
110 | "cllist.remove(\"C\")\n",
111 | "cllist.print_list()"
112 | ]
113 | }
114 | ]
115 | }
--------------------------------------------------------------------------------
/Deletion_by_Value_in_Link_list.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyPgkLTUeAukPZGm5lrwzo9s",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "0KgfehlsKzIL",
37 | "outputId": "94b3a2de-979c-4db5-bab0-e68bd8ef91c2"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "A\n",
45 | "C\n",
46 | "D\n"
47 | ]
48 | }
49 | ],
50 | "source": [
51 | "#Case of Deleting Head \n",
52 | "class Node:\n",
53 | " def __init__(self, data):\n",
54 | " self.data = data\n",
55 | " self.next = None\n",
56 | "\n",
57 | "\n",
58 | "class LinkedList:\n",
59 | " def __init__(self):\n",
60 | " self.head = None\n",
61 | "\n",
62 | " def print_list(self):\n",
63 | " cur_node = self.head\n",
64 | " while cur_node:\n",
65 | " print(cur_node.data)\n",
66 | " cur_node = cur_node.next\n",
67 | "\n",
68 | " def append(self, data):\n",
69 | " new_node = Node(data)\n",
70 | "\n",
71 | " if self.head is None:\n",
72 | " self.head = new_node\n",
73 | " return\n",
74 | "\n",
75 | " last_node = self.head\n",
76 | " while last_node.next:\n",
77 | " last_node = last_node.next\n",
78 | " last_node.next = new_node\n",
79 | "\n",
80 | " def prepend(self, data):\n",
81 | " new_node = Node(data)\n",
82 | "\n",
83 | " new_node.next = self.head\n",
84 | " self.head = new_node\n",
85 | "\n",
86 | " def insert_after_node(self, prev_node, data):\n",
87 | "\n",
88 | " if not prev_node:\n",
89 | " print(\"Previous node does not exist.\")\n",
90 | " return \n",
91 | "\n",
92 | " new_node = Node(data)\n",
93 | "\n",
94 | " new_node.next = prev_node.next\n",
95 | " prev_node.next = new_node\n",
96 | "\n",
97 | " def delete_node(self, key):\n",
98 | "\n",
99 | " cur_node = self.head\n",
100 | "\n",
101 | " if cur_node and cur_node.data == key:\n",
102 | " self.head = cur_node.next\n",
103 | " cur_node = None\n",
104 | " return\n",
105 | "\n",
106 | " prev = None \n",
107 | " while cur_node and cur_node.data != key:\n",
108 | " prev = cur_node\n",
109 | " cur_node = cur_node.next\n",
110 | "\n",
111 | " if cur_node is None:\n",
112 | " return \n",
113 | "\n",
114 | " prev.next = cur_node.next\n",
115 | " cur_node = None\n",
116 | "\n",
117 | "\n",
118 | "llist = LinkedList()\n",
119 | "llist.append(\"A\")\n",
120 | "llist.append(\"B\")\n",
121 | "llist.append(\"C\")\n",
122 | "llist.append(\"D\")\n",
123 | "\n",
124 | "llist.delete_node(\"B\")\n",
125 | "llist.delete_node(\"E\")\n",
126 | "\n",
127 | "llist.print_list()\n"
128 | ]
129 | }
130 | ]
131 | }
--------------------------------------------------------------------------------
/Iterative_Implementation_link_list.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyMry3PW4DWsyyV5Zt01r2YJ",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "XInbAMmwqEZs"
34 | },
35 | "outputs": [],
36 | "source": [
37 | "class Node:\n",
38 | " def __init__(self, data):\n",
39 | " self.data = data\n",
40 | " self.next = None\n",
41 | "\n",
42 | "\n",
43 | "class LinkedList:\n",
44 | " def __init__(self):\n",
45 | " self.head = None\n",
46 | "\n",
47 | " def print_list(self):\n",
48 | " cur_node = self.head\n",
49 | " while cur_node:\n",
50 | " print(cur_node.data)\n",
51 | " cur_node = cur_node.next\n",
52 | "\n",
53 | " def append(self, data):\n",
54 | " new_node = Node(data)\n",
55 | "\n",
56 | " if self.head is None:\n",
57 | " self.head = new_node\n",
58 | " return\n",
59 | "\n",
60 | " last_node = self.head\n",
61 | " while last_node.next:\n",
62 | " last_node = last_node.next\n",
63 | " last_node.next = new_node\n",
64 | "\n",
65 | " def prepend(self, data):\n",
66 | " new_node = Node(data)\n",
67 | "\n",
68 | " new_node.next = self.head\n",
69 | " self.head = new_node\n",
70 | "\n",
71 | " def insert_after_node(self, prev_node, data):\n",
72 | "\n",
73 | " if not prev_node:\n",
74 | " print(\"Previous node does not exist.\")\n",
75 | " return \n",
76 | "\n",
77 | " new_node = Node(data)\n",
78 | "\n",
79 | " new_node.next = prev_node.next\n",
80 | " prev_node.next = new_node\n",
81 | "\n",
82 | " def delete_node(self, key):\n",
83 | "\n",
84 | " cur_node = self.head\n",
85 | "\n",
86 | " if cur_node and cur_node.data == key:\n",
87 | " self.head = cur_node.next\n",
88 | " cur_node = None\n",
89 | " return\n",
90 | "\n",
91 | " prev = None \n",
92 | " while cur_node and cur_node.data != key:\n",
93 | " prev = cur_node\n",
94 | " cur_node = cur_node.next\n",
95 | "\n",
96 | " if cur_node is None:\n",
97 | " return \n",
98 | "\n",
99 | " prev.next = cur_node.next\n",
100 | " cur_node = None\n",
101 | "\n",
102 | " def delete_node_at_pos(self, pos):\n",
103 | "\n",
104 | " cur_node = self.head\n",
105 | "\n",
106 | " if pos == 0:\n",
107 | " self.head = cur_node.next\n",
108 | " cur_node = None\n",
109 | " return\n",
110 | "\n",
111 | " prev = None\n",
112 | " count = 1\n",
113 | " while cur_node and count != pos:\n",
114 | " prev = cur_node \n",
115 | " cur_node = cur_node.next\n",
116 | " count += 1\n",
117 | "\n",
118 | " if cur_node is None:\n",
119 | " return \n",
120 | "\n",
121 | " prev.next = cur_node.next\n",
122 | " cur_node = None\n",
123 | "\n",
124 | " def len_iterative(self):\n",
125 | "\n",
126 | " count = 0\n",
127 | " cur_node = self.head\n",
128 | "\n",
129 | " while cur_node:\n",
130 | " count += 1\n",
131 | " cur_node = cur_node.next\n",
132 | " return count\n",
133 | "\n",
134 | "\n",
135 | "llist = LinkedList()\n",
136 | "llist.append(\"A\")\n",
137 | "llist.append(\"B\")\n",
138 | "llist.append(\"C\")\n",
139 | "llist.append(\"D\")\n",
140 | "\n",
141 | "\n",
142 | "print(llist.len_iterative())"
143 | ]
144 | }
145 | ]
146 | }
--------------------------------------------------------------------------------
/Deletion_by_Position.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOGqWCdfO2j10PLBjEGJT7O",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "xMQEZ6qZLei6",
37 | "outputId": "2191331c-6cac-4598-bc95-b983d0d2b8fb"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "B\n",
45 | "C\n",
46 | "D\n"
47 | ]
48 | }
49 | ],
50 | "source": [
51 | "class Node:\n",
52 | " def __init__(self, data):\n",
53 | " self.data = data\n",
54 | " self.next = None\n",
55 | "\n",
56 | "\n",
57 | "class LinkedList:\n",
58 | " def __init__(self):\n",
59 | " self.head = None\n",
60 | "\n",
61 | " def print_list(self):\n",
62 | " cur_node = self.head\n",
63 | " while cur_node:\n",
64 | " print(cur_node.data)\n",
65 | " cur_node = cur_node.next\n",
66 | "\n",
67 | " def append(self, data):\n",
68 | " new_node = Node(data)\n",
69 | "\n",
70 | " if self.head is None:\n",
71 | " self.head = new_node\n",
72 | " return\n",
73 | "\n",
74 | " last_node = self.head\n",
75 | " while last_node.next:\n",
76 | " last_node = last_node.next\n",
77 | " last_node.next = new_node\n",
78 | "\n",
79 | " def prepend(self, data):\n",
80 | " new_node = Node(data)\n",
81 | "\n",
82 | " new_node.next = self.head\n",
83 | " self.head = new_node\n",
84 | "\n",
85 | " def insert_after_node(self, prev_node, data):\n",
86 | "\n",
87 | " if not prev_node:\n",
88 | " print(\"Previous node does not exist.\")\n",
89 | " return \n",
90 | "\n",
91 | " new_node = Node(data)\n",
92 | "\n",
93 | " new_node.next = prev_node.next\n",
94 | " prev_node.next = new_node\n",
95 | "\n",
96 | " def delete_node(self, key):\n",
97 | "\n",
98 | " cur_node = self.head\n",
99 | "\n",
100 | " if cur_node and cur_node.data == key:\n",
101 | " self.head = cur_node.next\n",
102 | " cur_node = None\n",
103 | " return\n",
104 | "\n",
105 | " prev = None \n",
106 | " while cur_node and cur_node.data != key:\n",
107 | " prev = cur_node\n",
108 | " cur_node = cur_node.next\n",
109 | "\n",
110 | " if cur_node is None:\n",
111 | " return \n",
112 | "\n",
113 | " prev.next = cur_node.next\n",
114 | " cur_node = None\n",
115 | "\n",
116 | " def delete_node_at_pos(self, pos):\n",
117 | " if self.head:\n",
118 | " cur_node = self.head\n",
119 | "\n",
120 | " if pos == 0:\n",
121 | " self.head = cur_node.next\n",
122 | " cur_node = None\n",
123 | " return\n",
124 | "\n",
125 | " prev = None\n",
126 | " count = 0\n",
127 | " while cur_node and count != pos:\n",
128 | " prev = cur_node \n",
129 | " cur_node = cur_node.next\n",
130 | " count += 1\n",
131 | "\n",
132 | " if cur_node is None:\n",
133 | " return \n",
134 | "\n",
135 | " prev.next = cur_node.next\n",
136 | " cur_node = None\n",
137 | "\n",
138 | "\n",
139 | "llist = LinkedList()\n",
140 | "llist.append(\"A\")\n",
141 | "llist.append(\"B\")\n",
142 | "llist.append(\"C\")\n",
143 | "llist.append(\"D\")\n",
144 | "\n",
145 | "llist.delete_node_at_pos(0)\n",
146 | "\n",
147 | "llist.print_list()"
148 | ]
149 | }
150 | ]
151 | }
--------------------------------------------------------------------------------
/Recursive_Implementation_.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyPza0DBCc31zUIjr6ct6jaU",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "-886UPP3qjeu",
37 | "outputId": "83783414-f023-490a-b200-2101e7c3e8d0"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "The length of an empty linked list is:\n",
45 | "0\n",
46 | "The length of the linked list calculated recursively after inserting 4 elements is:\n",
47 | "4\n",
48 | "The length of the linked list calculated iteratively after inserting 4 elements is:\n",
49 | "4\n"
50 | ]
51 | }
52 | ],
53 | "source": [
54 | "class Node:\n",
55 | " def __init__(self, data):\n",
56 | " self.data = data\n",
57 | " self.next = None\n",
58 | "\n",
59 | "\n",
60 | "class LinkedList:\n",
61 | " def __init__(self):\n",
62 | " self.head = None\n",
63 | "\n",
64 | " def print_list(self):\n",
65 | " cur_node = self.head\n",
66 | " while cur_node:\n",
67 | " print(cur_node.data)\n",
68 | " cur_node = cur_node.next\n",
69 | "\n",
70 | " def append(self, data):\n",
71 | " new_node = Node(data)\n",
72 | "\n",
73 | " if self.head is None:\n",
74 | " self.head = new_node\n",
75 | " return\n",
76 | "\n",
77 | " last_node = self.head\n",
78 | " while last_node.next:\n",
79 | " last_node = last_node.next\n",
80 | " last_node.next = new_node\n",
81 | "\n",
82 | " def prepend(self, data):\n",
83 | " new_node = Node(data)\n",
84 | "\n",
85 | " new_node.next = self.head\n",
86 | " self.head = new_node\n",
87 | "\n",
88 | " def insert_after_node(self, prev_node, data):\n",
89 | "\n",
90 | " if not prev_node:\n",
91 | " print(\"Previous node does not exist.\")\n",
92 | " return \n",
93 | "\n",
94 | " new_node = Node(data)\n",
95 | "\n",
96 | " new_node.next = prev_node.next\n",
97 | " prev_node.next = new_node\n",
98 | "\n",
99 | " def delete_node(self, key):\n",
100 | " \n",
101 | " cur_node = self.head\n",
102 | "\n",
103 | " if cur_node and cur_node.data == key:\n",
104 | " self.head = cur_node.next\n",
105 | " cur_node = None\n",
106 | " return\n",
107 | "\n",
108 | " prev = None \n",
109 | " while cur_node and cur_node.data != key:\n",
110 | " prev = cur_node\n",
111 | " cur_node = cur_node.next\n",
112 | "\n",
113 | " if cur_node is None:\n",
114 | " return \n",
115 | "\n",
116 | " prev.next = cur_node.next\n",
117 | " cur_node = None\n",
118 | "\n",
119 | " def delete_node_at_pos(self, pos):\n",
120 | " if self.head:\n",
121 | " cur_node = self.head\n",
122 | "\n",
123 | " if pos == 0:\n",
124 | " self.head = cur_node.next\n",
125 | " cur_node = None\n",
126 | " return\n",
127 | "\n",
128 | " prev = None\n",
129 | " count = 1\n",
130 | " while cur_node and count != pos:\n",
131 | " prev = cur_node \n",
132 | " cur_node = cur_node.next\n",
133 | " count += 1\n",
134 | "\n",
135 | " if cur_node is None:\n",
136 | " return \n",
137 | "\n",
138 | " prev.next = cur_node.next\n",
139 | " cur_node = None\n",
140 | "\n",
141 | " def len_iterative(self):\n",
142 | "\n",
143 | " count = 0\n",
144 | " cur_node = self.head\n",
145 | "\n",
146 | " while cur_node:\n",
147 | " count += 1\n",
148 | " cur_node = cur_node.next\n",
149 | " return count\n",
150 | "\n",
151 | " def len_recursive(self, node):\n",
152 | " if node is None:\n",
153 | " return 0\n",
154 | " return 1 + self.len_recursive(node.next)\n",
155 | "\n",
156 | "\n",
157 | "llist = LinkedList()\n",
158 | "print(\"The length of an empty linked list is:\")\n",
159 | "print(llist.len_recursive(llist.head))\n",
160 | "llist.append(\"A\")\n",
161 | "llist.append(\"B\")\n",
162 | "llist.append(\"C\")\n",
163 | "llist.append(\"D\")\n",
164 | "\n",
165 | "print(\"The length of the linked list calculated recursively after inserting 4 elements is:\")\n",
166 | "print(llist.len_recursive(llist.head))\n",
167 | "print(\"The length of the linked list calculated iteratively after inserting 4 elements is:\")\n",
168 | "print(llist.len_iterative())\n"
169 | ]
170 | }
171 | ]
172 | }
--------------------------------------------------------------------------------
/Reverse_Node.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOLWZsGpJcvvQhtNX2Uwddn",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "OESEPVB2177X",
37 | "outputId": "6103a709-202b-4001-8440-55519131b6d1"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "D\n",
45 | "C\n",
46 | "B\n",
47 | "A\n"
48 | ]
49 | }
50 | ],
51 | "source": [
52 | "class Node:\n",
53 | " def __init__(self, data):\n",
54 | " self.data = data\n",
55 | " self.next = None\n",
56 | "\n",
57 | "\n",
58 | "class LinkedList:\n",
59 | " def __init__(self):\n",
60 | " self.head = None\n",
61 | "\n",
62 | " def print_list(self):\n",
63 | " cur_node = self.head\n",
64 | " while cur_node:\n",
65 | " print(cur_node.data)\n",
66 | " cur_node = cur_node.next\n",
67 | "\n",
68 | " def append(self, data):\n",
69 | " new_node = Node(data)\n",
70 | "\n",
71 | " if self.head is None:\n",
72 | " self.head = new_node\n",
73 | " return\n",
74 | "\n",
75 | " last_node = self.head\n",
76 | " while last_node.next:\n",
77 | " last_node = last_node.next\n",
78 | " last_node.next = new_node\n",
79 | "\n",
80 | " def prepend(self, data):\n",
81 | " new_node = Node(data)\n",
82 | "\n",
83 | " new_node.next = self.head\n",
84 | " self.head = new_node\n",
85 | "\n",
86 | " def insert_after_node(self, prev_node, data):\n",
87 | "\n",
88 | " if not prev_node:\n",
89 | " print(\"Previous node does not exist.\")\n",
90 | " return \n",
91 | "\n",
92 | " new_node = Node(data)\n",
93 | "\n",
94 | " new_node.next = prev_node.next\n",
95 | " prev_node.next = new_node\n",
96 | "\n",
97 | " def delete_node(self, key):\n",
98 | "\n",
99 | " cur_node = self.head\n",
100 | "\n",
101 | " if cur_node and cur_node.data == key:\n",
102 | " self.head = cur_node.next\n",
103 | " cur_node = None\n",
104 | " return\n",
105 | "\n",
106 | " prev = None \n",
107 | " while cur_node and cur_node.data != key:\n",
108 | " prev = cur_node\n",
109 | " cur_node = cur_node.next\n",
110 | "\n",
111 | " if cur_node is None:\n",
112 | " return \n",
113 | "\n",
114 | " prev.next = cur_node.next\n",
115 | " cur_node = None\n",
116 | "\n",
117 | " def delete_node_at_pos(self, pos):\n",
118 | " if self.head:\n",
119 | " cur_node = self.head\n",
120 | "\n",
121 | " if pos == 0:\n",
122 | " self.head = cur_node.next\n",
123 | " cur_node = None\n",
124 | " return\n",
125 | "\n",
126 | " prev = None\n",
127 | " count = 1\n",
128 | " while cur_node and count != pos:\n",
129 | " prev = cur_node \n",
130 | " cur_node = cur_node.next\n",
131 | " count += 1\n",
132 | "\n",
133 | " if cur_node is None:\n",
134 | " return \n",
135 | "\n",
136 | " prev.next = cur_node.next\n",
137 | " cur_node = None\n",
138 | "\n",
139 | " def len_iterative(self):\n",
140 | "\n",
141 | " count = 0\n",
142 | " cur_node = self.head\n",
143 | "\n",
144 | " while cur_node:\n",
145 | " count += 1\n",
146 | " cur_node = cur_node.next\n",
147 | " return count\n",
148 | "\n",
149 | " def len_recursive(self, node):\n",
150 | " if node is None:\n",
151 | " return 0\n",
152 | " return 1 + self.len_recursive(node.next)\n",
153 | "\n",
154 | " def print_helper(self, node, name):\n",
155 | " if node is None:\n",
156 | " print(name + \": None\")\n",
157 | " else:\n",
158 | " print(name + \":\" + node.data)\n",
159 | "\n",
160 | " def reverse_iterative(self):\n",
161 | "\n",
162 | " prev = None \n",
163 | " cur = self.head\n",
164 | " while cur:\n",
165 | " nxt = cur.next\n",
166 | " cur.next = prev\n",
167 | " \n",
168 | " self.print_helper(prev, \"PREV\")\n",
169 | " self.print_helper(cur, \"CUR\")\n",
170 | " self.print_helper(nxt, \"NXT\")\n",
171 | " print(\"\\n\")\n",
172 | "\n",
173 | " prev = cur \n",
174 | " cur = nxt \n",
175 | " self.head = prev\n",
176 | "\n",
177 | " def reverse_recursive(self):\n",
178 | "\n",
179 | " def _reverse_recursive(cur, prev):\n",
180 | " if not cur:\n",
181 | " return prev\n",
182 | "\n",
183 | " nxt = cur.next\n",
184 | " cur.next = prev\n",
185 | " prev = cur \n",
186 | " cur = nxt \n",
187 | " return _reverse_recursive(cur, prev)\n",
188 | "\n",
189 | " self.head = _reverse_recursive(cur=self.head, prev=None)\n",
190 | " \n",
191 | "llist = LinkedList()\n",
192 | "llist.append(\"A\")\n",
193 | "llist.append(\"B\")\n",
194 | "llist.append(\"C\")\n",
195 | "llist.append(\"D\")\n",
196 | "\n",
197 | "llist.reverse_recursive()\n",
198 | "\n",
199 | "llist.print_list()"
200 | ]
201 | }
202 | ]
203 | }
--------------------------------------------------------------------------------
/Swap_Node.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyMjEKuBbPB+o682nVcIHftZ",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "bs6JN_pc1P-u",
37 | "outputId": "4da69cb3-24da-4753-bacc-e20a4a1ec981"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "Original List\n",
45 | "A\n",
46 | "B\n",
47 | "C\n",
48 | "D\n",
49 | "Swapping nodes B and C that are not head nodes\n",
50 | "A\n",
51 | "C\n",
52 | "B\n",
53 | "D\n",
54 | "Swapping nodes A and B where key_1 is head node\n",
55 | "B\n",
56 | "C\n",
57 | "A\n",
58 | "D\n",
59 | "Swapping nodes D and B where key_2 is head node\n",
60 | "D\n",
61 | "C\n",
62 | "A\n",
63 | "B\n",
64 | "Swapping nodes C and C where both keys are same\n",
65 | "D\n",
66 | "C\n",
67 | "A\n",
68 | "B\n"
69 | ]
70 | }
71 | ],
72 | "source": [
73 | "class Node:\n",
74 | " def __init__(self, data):\n",
75 | " self.data = data\n",
76 | " self.next = None\n",
77 | "\n",
78 | "\n",
79 | "class LinkedList:\n",
80 | " def __init__(self):\n",
81 | " self.head = None\n",
82 | "\n",
83 | " def print_list(self):\n",
84 | " cur_node = self.head\n",
85 | " while cur_node:\n",
86 | " print(cur_node.data)\n",
87 | " cur_node = cur_node.next\n",
88 | "\n",
89 | " def append(self, data):\n",
90 | " new_node = Node(data)\n",
91 | "\n",
92 | " if self.head is None:\n",
93 | " self.head = new_node\n",
94 | " return\n",
95 | "\n",
96 | " last_node = self.head\n",
97 | " while last_node.next:\n",
98 | " last_node = last_node.next\n",
99 | " last_node.next = new_node\n",
100 | "\n",
101 | " def prepend(self, data):\n",
102 | " new_node = Node(data)\n",
103 | "\n",
104 | " new_node.next = self.head\n",
105 | " self.head = new_node\n",
106 | "\n",
107 | " def insert_after_node(self, prev_node, data):\n",
108 | "\n",
109 | " if not prev_node:\n",
110 | " print(\"Previous node does not exist.\")\n",
111 | " return \n",
112 | "\n",
113 | " new_node = Node(data)\n",
114 | "\n",
115 | " new_node.next = prev_node.next\n",
116 | " prev_node.next = new_node\n",
117 | "\n",
118 | " def delete_node(self, key):\n",
119 | "\n",
120 | " cur_node = self.head\n",
121 | "\n",
122 | " if cur_node and cur_node.data == key:\n",
123 | " self.head = cur_node.next\n",
124 | " cur_node = None\n",
125 | " return\n",
126 | "\n",
127 | " prev = None \n",
128 | " while cur_node and cur_node.data != key:\n",
129 | " prev = cur_node\n",
130 | " cur_node = cur_node.next\n",
131 | "\n",
132 | " if cur_node is None:\n",
133 | " return \n",
134 | "\n",
135 | " prev.next = cur_node.next\n",
136 | " cur_node = None\n",
137 | "\n",
138 | " def delete_node_at_pos(self, pos):\n",
139 | " if self.head:\n",
140 | " cur_node = self.head\n",
141 | "\n",
142 | " if pos == 0:\n",
143 | " self.head = cur_node.next\n",
144 | " cur_node = None\n",
145 | " return\n",
146 | "\n",
147 | " prev = None\n",
148 | " count = 1\n",
149 | " while cur_node and count != pos:\n",
150 | " prev = cur_node \n",
151 | " cur_node = cur_node.next\n",
152 | " count += 1\n",
153 | "\n",
154 | " if cur_node is None:\n",
155 | " return \n",
156 | "\n",
157 | " prev.next = cur_node.next\n",
158 | " cur_node = None\n",
159 | "\n",
160 | " def len_iterative(self):\n",
161 | "\n",
162 | " count = 0\n",
163 | " cur_node = self.head\n",
164 | "\n",
165 | " while cur_node:\n",
166 | " count += 1\n",
167 | " cur_node = cur_node.next\n",
168 | " return count\n",
169 | "\n",
170 | " def len_recursive(self, node):\n",
171 | " if node is None:\n",
172 | " return 0\n",
173 | " return 1 + self.len_recursive(node.next)\n",
174 | "\n",
175 | "\n",
176 | " def swap_nodes(self, key_1, key_2):\n",
177 | "\n",
178 | " if key_1 == key_2:\n",
179 | " return \n",
180 | "\n",
181 | " prev_1 = None \n",
182 | " curr_1 = self.head \n",
183 | " while curr_1 and curr_1.data != key_1:\n",
184 | " prev_1 = curr_1 \n",
185 | " curr_1 = curr_1.next\n",
186 | "\n",
187 | " prev_2 = None \n",
188 | " curr_2 = self.head \n",
189 | " while curr_2 and curr_2.data != key_2:\n",
190 | " prev_2 = curr_2 \n",
191 | " curr_2 = curr_2.next\n",
192 | "\n",
193 | " if not curr_1 or not curr_2:\n",
194 | " return \n",
195 | "\n",
196 | " if prev_1:\n",
197 | " prev_1.next = curr_2\n",
198 | " else:\n",
199 | " self.head = curr_2\n",
200 | "\n",
201 | " if prev_2:\n",
202 | " prev_2.next = curr_1\n",
203 | " else:\n",
204 | " self.head = curr_1\n",
205 | "\n",
206 | " curr_1.next, curr_2.next = curr_2.next, curr_1.next\n",
207 | "\n",
208 | " \n",
209 | "llist = LinkedList()\n",
210 | "llist.append(\"A\")\n",
211 | "llist.append(\"B\")\n",
212 | "llist.append(\"C\")\n",
213 | "llist.append(\"D\")\n",
214 | "\n",
215 | "print(\"Original List\")\n",
216 | "llist.print_list()\n",
217 | "\n",
218 | "\n",
219 | "llist.swap_nodes(\"B\", \"C\")\n",
220 | "print(\"Swapping nodes B and C that are not head nodes\")\n",
221 | "llist.print_list()\n",
222 | "\n",
223 | "llist.swap_nodes(\"A\", \"B\")\n",
224 | "print(\"Swapping nodes A and B where key_1 is head node\")\n",
225 | "llist.print_list()\n",
226 | "\n",
227 | "llist.swap_nodes(\"D\", \"B\")\n",
228 | "print(\"Swapping nodes D and B where key_2 is head node\")\n",
229 | "llist.print_list()\n",
230 | "\n",
231 | "llist.swap_nodes(\"C\", \"C\")\n",
232 | "print(\"Swapping nodes C and C where both keys are same\")\n",
233 | "llist.print_list()"
234 | ]
235 | }
236 | ]
237 | }
--------------------------------------------------------------------------------
/Is_Circular_Linked_List.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOB5r5NdxU+eVDgX/UdNJKd",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "2BgnwGAias3A"
34 | },
35 | "outputs": [],
36 | "source": [
37 | "class Node:\n",
38 | " def __init__(self, data):\n",
39 | " self.data = data \n",
40 | " self.next = None\n",
41 | "\n",
42 | "\n",
43 | "class LinkedList:\n",
44 | " def __init__(self):\n",
45 | " self.head = None\n",
46 | "\n",
47 | " def print_list(self):\n",
48 | " cur_node = self.head\n",
49 | " while cur_node:\n",
50 | " print(cur_node.data)\n",
51 | " cur_node = cur_node.next\n",
52 | "\n",
53 | " def append(self, data):\n",
54 | " new_node = Node(data)\n",
55 | "\n",
56 | " if self.head is None:\n",
57 | " self.head = new_node\n",
58 | " return\n",
59 | "\n",
60 | " last_node = self.head\n",
61 | " while last_node.next:\n",
62 | " last_node = last_node.next\n",
63 | " last_node.next = new_node\n",
64 | "\n",
65 | "\n",
66 | "class CircularLinkedList:\n",
67 | " def __init__(self):\n",
68 | " self.head = None \n",
69 | "\n",
70 | " def prepend(self, data):\n",
71 | " new_node = Node(data)\n",
72 | " cur = self.head \n",
73 | " new_node.next = self.head\n",
74 | "\n",
75 | " if not self.head:\n",
76 | " new_node.next = new_node\n",
77 | " else:\n",
78 | " while cur.next != self.head:\n",
79 | " cur = cur.next\n",
80 | " cur.next = new_node\n",
81 | " self.head = new_node\n",
82 | "\n",
83 | " def append(self, data):\n",
84 | " if not self.head:\n",
85 | " self.head = Node(data)\n",
86 | " self.head.next = self.head\n",
87 | " else:\n",
88 | " new_node = Node(data)\n",
89 | " cur = self.head\n",
90 | " while cur.next != self.head:\n",
91 | " cur = cur.next\n",
92 | " cur.next = new_node\n",
93 | " new_node.next = self.head\n",
94 | "\n",
95 | " def print_list(self):\n",
96 | " cur = self.head \n",
97 | "\n",
98 | " while cur:\n",
99 | " print(cur.data)\n",
100 | " cur = cur.next\n",
101 | " if cur == self.head:\n",
102 | " break\n",
103 | "\n",
104 | " def __len__(self):\n",
105 | " cur = self.head\n",
106 | " count = 0\n",
107 | " while cur:\n",
108 | " count += 1\n",
109 | " cur = cur.next\n",
110 | " if cur == self.head:\n",
111 | " break\n",
112 | " return count\n",
113 | "\n",
114 | " def split_list(self):\n",
115 | " size = len(self) \n",
116 | "\n",
117 | " if size == 0:\n",
118 | " return None\n",
119 | " if size == 1:\n",
120 | " return self.head\n",
121 | "\n",
122 | " mid = size//2\n",
123 | " count = 0\n",
124 | "\n",
125 | " prev = None\n",
126 | " cur = self.head\n",
127 | "\n",
128 | " while cur and count < mid:\n",
129 | " count += 1\n",
130 | " prev = cur\n",
131 | " cur = cur.next\n",
132 | " prev.next = self.head \n",
133 | "\n",
134 | " split_cllist = CircularLinkedList()\n",
135 | " while cur.next != self.head:\n",
136 | " split_cllist.append(cur.data)\n",
137 | " cur = cur.next\n",
138 | " split_cllist.append(cur.data)\n",
139 | "\n",
140 | " self.print_list()\n",
141 | " print(\"\\n\")\n",
142 | " split_cllist.print_list()\n",
143 | "\n",
144 | " def remove(self, key):\n",
145 | " if self.head:\n",
146 | " if self.head.data == key:\n",
147 | " cur = self.head\n",
148 | " while cur.next != self.head:\n",
149 | " cur = cur.next\n",
150 | " if self.head == self.head.next:\n",
151 | " self.head = None\n",
152 | " else:\n",
153 | " cur.next = self.head.next\n",
154 | " self.head = self.head.next\n",
155 | " else:\n",
156 | " cur = self.head\n",
157 | " prev = None\n",
158 | " while cur.next != self.head:\n",
159 | " prev = cur \n",
160 | " cur = cur.next\n",
161 | " if cur.data == key:\n",
162 | " prev.next = cur.next\n",
163 | " cur = cur.next\n",
164 | "\n",
165 | "\n",
166 | " def remove_node(self, node):\n",
167 | " if self.head:\n",
168 | " if self.head == node:\n",
169 | " cur = self.head\n",
170 | " while cur.next != self.head:\n",
171 | " cur = cur.next\n",
172 | " if self.head == self.head.next:\n",
173 | " self.head = None\n",
174 | " else:\n",
175 | " cur.next = self.head.next\n",
176 | " self.head = self.head.next\n",
177 | " else:\n",
178 | " cur = self.head\n",
179 | " prev = None\n",
180 | " while cur.next != self.head:\n",
181 | " prev = cur \n",
182 | " cur = cur.next\n",
183 | " if cur == node:\n",
184 | " prev.next = cur.next\n",
185 | " cur = cur.next\n",
186 | "\n",
187 | " def josephus_circle(self, step):\n",
188 | " cur = self.head\n",
189 | "\n",
190 | " while len(self) > 1:\n",
191 | " count = 1\n",
192 | " while count != step:\n",
193 | " cur = cur.next\n",
194 | " count += 1\n",
195 | " print(\"REMOVED: \" + str(cur.data))\n",
196 | " self.remove_node(cur)\n",
197 | " cur = cur.next\n",
198 | "\n",
199 | " def is_circular_linked_list(self, input_list):\n",
200 | " if input_list.head:\n",
201 | " cur = input_list.head\n",
202 | " while cur.next:\n",
203 | " cur = cur.next\n",
204 | " if cur.next == input_list.head:\n",
205 | " return True\n",
206 | " return False\n",
207 | " else:\n",
208 | " return False\n",
209 | "\n",
210 | "cllist = CircularLinkedList()\n",
211 | "cllist.append(1)\n",
212 | "cllist.append(2)\n",
213 | "cllist.append(3)\n",
214 | "cllist.append(4)\n",
215 | "\n",
216 | "llist = LinkedList()\n",
217 | "llist.append(1)\n",
218 | "llist.append(2)\n",
219 | "llist.append(3)\n",
220 | "llist.append(4)\n",
221 | "\n",
222 | "print(cllist.is_circular_linked_list(cllist))\n",
223 | "print(cllist.is_circular_linked_list(llist))"
224 | ]
225 | }
226 | ]
227 | }
--------------------------------------------------------------------------------
/Data_Structure_&_Data_Types_.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyN5IxJO3dxoniX1F64XgQKF",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | " **Getting the Data Type**
\n",
33 | "Data structure is a data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.\n",
34 | "
\n",
35 | "On the other hand, Data type is a concept specific to a programming language. In a way, it is a concrete implementation of a data structure in a particular programming language (be it Python or any other language).\n",
36 | "The actual definition of what constitutes a \"type\" varies among programming languages. Talking about Python, there are basic data types like int, float, string etc. You can use the built-in types like list, set etc. which we will be covering in this session.\n",
37 | "
\n",
38 | "Calling these data types as data structures won’t be wrong because there is no major difference between the two in Python."
39 | ],
40 | "metadata": {
41 | "id": "RKBCYcP9n6KR"
42 | }
43 | },
44 | {
45 | "cell_type": "code",
46 | "execution_count": 5,
47 | "metadata": {
48 | "colab": {
49 | "base_uri": "https://localhost:8080/"
50 | },
51 | "id": "4mfLcwaXmnJS",
52 | "outputId": "9cbec85f-40de-4657-cc26-123d83eb10ae"
53 | },
54 | "outputs": [
55 | {
56 | "output_type": "stream",
57 | "name": "stdout",
58 | "text": [
59 | "\n",
60 | "\n",
61 | "\n",
62 | "\n"
63 | ]
64 | }
65 | ],
66 | "source": [
67 | "#You can get the data type of any object by using the type( ) function:\n",
68 | "#Numerical Types: int (integer), float (decimal)\n",
69 | "#Text Type: str (string)\n",
70 | "#Boolean Type: bool (True or False)\n",
71 | "\n",
72 | "a = 4\n",
73 | "print(type(a))\n",
74 | "b=\"Hello\"\n",
75 | "print(type(b))\n",
76 | "c= 10.5\n",
77 | "print(type(c))\n",
78 | "boolvar = True\n",
79 | "print(type(boolvar))\n"
80 | ]
81 | },
82 | {
83 | "cell_type": "markdown",
84 | "source": [
85 | "**Strings**
\n",
86 | "Sequence Types\n",
87 | "* There are seven sequence types: strings, Unicode strings, lists, tuples, bytearrays, buffers, and xrange objects.\n",
88 | "* Sequences allow you to store multiple values in an organized and efficient way.\n",
89 | "\n",
90 | "\n"
91 | ],
92 | "metadata": {
93 | "id": "dVmXftXipwJc"
94 | }
95 | },
96 | {
97 | "cell_type": "markdown",
98 | "source": [
99 | "**Python Strings**
\n",
100 | "* Strings are sequence of characters.\n",
101 | "* Let us see some examples of String: \"My name is Sajid\", \"Sajid\", \"Go to Univeristy\". All these are examples of String.\n",
102 | "* In Python, Strings are called str.\n",
103 | "There is a specific way of defining String in Python – it is defined within single quote (‘) or double quotes (“) or even triple quotes (“‘)."
104 | ],
105 | "metadata": {
106 | "id": "MTC-jvJDqpaS"
107 | }
108 | },
109 | {
110 | "cell_type": "markdown",
111 | "source": [
112 | "**Accessing String Elements**"
113 | ],
114 | "metadata": {
115 | "id": "NUhJYLqqrnnQ"
116 | }
117 | },
118 | {
119 | "cell_type": "code",
120 | "source": [
121 | "# Square brackets can be used to access elements of the string.\n",
122 | "# Remember that the first character has index 0.\n",
123 | "# Index refers to position of a character in a string. In python index number starts from 0.\n",
124 | "\n",
125 | "# String Slicing\n",
126 | "# We can also call out a range of characters from the string using string slicing.\n",
127 | "# Specify the start index and the end index, separated by a colon, to return a part of the string. Note that the character of the end index is not included.\n",
128 | "# Suppose we want to print World from the string “Hello World”. We can do so as below:\n",
129 | "a = \"Pakistan, Zindabad!\"\n",
130 | "print(a[1])\n",
131 | "a = \"Pakistan, Zindabad!\"\n",
132 | "print(a[1:7])\n",
133 | "a = \"Pakistan, Zindabad!\"\n",
134 | "print(a[:])"
135 | ],
136 | "metadata": {
137 | "colab": {
138 | "base_uri": "https://localhost:8080/"
139 | },
140 | "id": "Cu2OvFyxrusS",
141 | "outputId": "ec3ddc1b-2e7a-4632-9868-8dc0d2312e74"
142 | },
143 | "execution_count": 11,
144 | "outputs": [
145 | {
146 | "output_type": "stream",
147 | "name": "stdout",
148 | "text": [
149 | "a\n",
150 | "akista\n",
151 | "Pakistan, Zindabad!\n"
152 | ]
153 | }
154 | ]
155 | },
156 | {
157 | "cell_type": "markdown",
158 | "source": [
159 | "**Negative Indexing**\n",
160 | "* If we have a long string and we want to pinpoint an item towards the end, we can also count backwards from the end of the string, starting at the index number -1.\n",
161 | "* Printing ‘r’ from the string:\n",
162 | "\n"
163 | ],
164 | "metadata": {
165 | "id": "SVAMq_Fhs6ek"
166 | }
167 | },
168 | {
169 | "cell_type": "code",
170 | "source": [
171 | "a = \"Pakistan, Zindabad!\"\n",
172 | "print(a[1:7])\n",
173 | "print(a[-4])\n",
174 | "# Get the characters from position -5 to position -3, starting the count from the end of the string:\n",
175 | "print(a[-5:-2])"
176 | ],
177 | "metadata": {
178 | "colab": {
179 | "base_uri": "https://localhost:8080/"
180 | },
181 | "id": "W-87cK0ftMMt",
182 | "outputId": "3f8fb832-3903-45df-dd7a-6114114baaa5"
183 | },
184 | "execution_count": 12,
185 | "outputs": [
186 | {
187 | "output_type": "stream",
188 | "name": "stdout",
189 | "text": [
190 | "akista\n",
191 | "b\n",
192 | "aba\n"
193 | ]
194 | }
195 | ]
196 | },
197 | {
198 | "cell_type": "markdown",
199 | "source": [
200 | "**String Concatenation**\n",
201 | "\n",
202 | "* String concatenation means adding strings together.
\n",
203 | "* Use the + character to add a variable to another variable:"
204 | ],
205 | "metadata": {
206 | "id": "MPPTE34Wt9d4"
207 | }
208 | },
209 | {
210 | "cell_type": "code",
211 | "source": [
212 | "x = \"Python is \"\n",
213 | "y = \"awesome\"\n",
214 | "z = x + y\n",
215 | "print(z)\n",
216 | "\n",
217 | "# without spaces\n",
218 | "# We can also add spaces between two strings\n",
219 | "x = \"ab \"\n",
220 | "y = \"cd\"\n",
221 | "print(x+y)\n",
222 | "\n",
223 | "# We can also add spaces between two strings\n",
224 | "x = \"ab \"\n",
225 | "y = \"cd\"\n",
226 | "print(x+\" \"+y)"
227 | ],
228 | "metadata": {
229 | "colab": {
230 | "base_uri": "https://localhost:8080/"
231 | },
232 | "id": "SuRDqVjAuGqP",
233 | "outputId": "d7494521-62ed-401a-81e3-8bd7e45b2292"
234 | },
235 | "execution_count": 16,
236 | "outputs": [
237 | {
238 | "output_type": "stream",
239 | "name": "stdout",
240 | "text": [
241 | "Python is awesome\n",
242 | "ab cd\n",
243 | "ab cd\n"
244 | ]
245 | }
246 | ]
247 | },
248 | {
249 | "cell_type": "markdown",
250 | "source": [
251 | "**String Length**\n"
252 | ],
253 | "metadata": {
254 | "id": "L0QmfdB_uxBm"
255 | }
256 | },
257 | {
258 | "cell_type": "code",
259 | "source": [
260 | "# To get the length of a string, use the len( ) function.\n",
261 | "a = \"Pakistan, Zindabad!\"\n",
262 | "print(len(a))\n"
263 | ],
264 | "metadata": {
265 | "colab": {
266 | "base_uri": "https://localhost:8080/"
267 | },
268 | "id": "i6Y6MXKAu1eI",
269 | "outputId": "ec9c6491-23f8-4546-e39e-e4b5ee787329"
270 | },
271 | "execution_count": 17,
272 | "outputs": [
273 | {
274 | "output_type": "stream",
275 | "name": "stdout",
276 | "text": [
277 | "19\n"
278 | ]
279 | }
280 | ]
281 | }
282 | ]
283 | }
--------------------------------------------------------------------------------
/Merge_Two_Sorted_Link_List.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyMiTbqqS+xZtbGf8VyboT7P",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "ZC_Is3aE8lx3",
37 | "outputId": "8fec2980-a57b-441b-9205-d45c47927cbb"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "1\n",
45 | "2\n",
46 | "3\n",
47 | "4\n",
48 | "5\n",
49 | "6\n",
50 | "7\n",
51 | "8\n",
52 | "9\n",
53 | "10\n"
54 | ]
55 | }
56 | ],
57 | "source": [
58 | "class Node:\n",
59 | " def __init__(self, data):\n",
60 | " self.data = data\n",
61 | " self.next = None\n",
62 | "\n",
63 | "\n",
64 | "class LinkedList:\n",
65 | " def __init__(self):\n",
66 | " self.head = None\n",
67 | "\n",
68 | " def print_list(self):\n",
69 | " cur_node = self.head\n",
70 | " while cur_node:\n",
71 | " print(cur_node.data)\n",
72 | " cur_node = cur_node.next\n",
73 | "\n",
74 | " def append(self, data):\n",
75 | " new_node = Node(data)\n",
76 | "\n",
77 | " if self.head is None:\n",
78 | " self.head = new_node\n",
79 | " return\n",
80 | "\n",
81 | " last_node = self.head\n",
82 | " while last_node.next:\n",
83 | " last_node = last_node.next\n",
84 | " last_node.next = new_node\n",
85 | "\n",
86 | " def prepend(self, data):\n",
87 | " new_node = Node(data)\n",
88 | "\n",
89 | " new_node.next = self.head\n",
90 | " self.head = new_node\n",
91 | "\n",
92 | " def insert_after_node(self, prev_node, data):\n",
93 | "\n",
94 | " if not prev_node:\n",
95 | " print(\"Previous node does not exist.\")\n",
96 | " return \n",
97 | "\n",
98 | " new_node = Node(data)\n",
99 | "\n",
100 | " new_node.next = prev_node.next\n",
101 | " prev_node.next = new_node\n",
102 | "\n",
103 | " def delete_node(self, key):\n",
104 | "\n",
105 | " cur_node = self.head\n",
106 | "\n",
107 | " if cur_node and cur_node.data == key:\n",
108 | " self.head = cur_node.next\n",
109 | " cur_node = None\n",
110 | " return\n",
111 | "\n",
112 | " prev = None \n",
113 | " while cur_node and cur_node.data != key:\n",
114 | " prev = cur_node\n",
115 | " cur_node = cur_node.next\n",
116 | "\n",
117 | " if cur_node is None:\n",
118 | " return \n",
119 | "\n",
120 | " prev.next = cur_node.next\n",
121 | " cur_node = None\n",
122 | "\n",
123 | " def delete_node_at_pos(self, pos):\n",
124 | " if self.head:\n",
125 | " cur_node = self.head\n",
126 | "\n",
127 | " if pos == 0:\n",
128 | " self.head = cur_node.next\n",
129 | " cur_node = None\n",
130 | " return\n",
131 | "\n",
132 | " prev = None\n",
133 | " count = 1\n",
134 | " while cur_node and count != pos:\n",
135 | " prev = cur_node \n",
136 | " cur_node = cur_node.next\n",
137 | " count += 1\n",
138 | "\n",
139 | " if cur_node is None:\n",
140 | " return \n",
141 | "\n",
142 | " prev.next = cur_node.next\n",
143 | " cur_node = None\n",
144 | "\n",
145 | " def len_iterative(self):\n",
146 | "\n",
147 | " count = 0\n",
148 | " cur_node = self.head\n",
149 | "\n",
150 | " while cur_node:\n",
151 | " count += 1\n",
152 | " cur_node = cur_node.next\n",
153 | " return count\n",
154 | "\n",
155 | " def len_recursive(self, node):\n",
156 | " if node is None:\n",
157 | " return 0\n",
158 | " return 1 + self.len_recursive(node.next)\n",
159 | "\n",
160 | " def swap_nodes(self, key_1, key_2):\n",
161 | "\n",
162 | " if key_1 == key_2:\n",
163 | " return \n",
164 | "\n",
165 | " prev_1 = None \n",
166 | " curr_1 = self.head \n",
167 | " while curr_1 and curr_1.data != key_1:\n",
168 | " prev_1 = curr_1 \n",
169 | " curr_1 = curr_1.next\n",
170 | "\n",
171 | " prev_2 = None \n",
172 | " curr_2 = self.head \n",
173 | " while curr_2 and curr_2.data != key_2:\n",
174 | " prev_2 = curr_2 \n",
175 | " curr_2 = curr_2.next\n",
176 | "\n",
177 | " if not curr_1 or not curr_2:\n",
178 | " return \n",
179 | "\n",
180 | " if prev_1:\n",
181 | " prev_1.next = curr_2\n",
182 | " else:\n",
183 | " self.head = curr_2\n",
184 | "\n",
185 | " if prev_2:\n",
186 | " prev_2.next = curr_1\n",
187 | " else:\n",
188 | " self.head = curr_1\n",
189 | "\n",
190 | " curr_1.next, curr_2.next = curr_2.next, curr_1.next\n",
191 | "\n",
192 | " def print_helper(self, node, name):\n",
193 | " if node is None:\n",
194 | " print(name + \": None\")\n",
195 | " else:\n",
196 | " print(name + \":\" + node.data)\n",
197 | "\n",
198 | " def reverse_iterative(self):\n",
199 | "\n",
200 | " prev = None \n",
201 | " cur = self.head\n",
202 | " while cur:\n",
203 | " nxt = cur.next\n",
204 | " cur.next = prev\n",
205 | " \n",
206 | " self.print_helper(prev, \"PREV\")\n",
207 | " self.print_helper(cur, \"CUR\")\n",
208 | " self.print_helper(nxt, \"NXT\")\n",
209 | " print(\"\\n\")\n",
210 | "\n",
211 | " prev = cur \n",
212 | " cur = nxt \n",
213 | " self.head = prev\n",
214 | "\n",
215 | " def reverse_recursive(self):\n",
216 | "\n",
217 | " def _reverse_recursive(cur, prev):\n",
218 | " if not cur:\n",
219 | " return prev\n",
220 | "\n",
221 | " nxt = cur.next\n",
222 | " cur.next = prev\n",
223 | " prev = cur \n",
224 | " cur = nxt \n",
225 | " return _reverse_recursive(cur, prev)\n",
226 | "\n",
227 | " self.head = _reverse_recursive(cur=self.head, prev=None)\n",
228 | "\n",
229 | " def merge_sorted(self, llist):\n",
230 | " \n",
231 | " p = self.head \n",
232 | " q = llist.head\n",
233 | " s = None\n",
234 | " \n",
235 | " if not p:\n",
236 | " return q\n",
237 | " if not q:\n",
238 | " return p\n",
239 | "\n",
240 | " if p and q:\n",
241 | " if p.data <= q.data:\n",
242 | " s = p \n",
243 | " p = s.next\n",
244 | " else:\n",
245 | " s = q\n",
246 | " q = s.next\n",
247 | " new_head = s \n",
248 | " while p and q:\n",
249 | " if p.data <= q.data:\n",
250 | " s.next = p \n",
251 | " s = p \n",
252 | " p = s.next\n",
253 | " else:\n",
254 | " s.next = q\n",
255 | " s = q\n",
256 | " q = s.next\n",
257 | " if not p:\n",
258 | " s.next = q \n",
259 | " if not q:\n",
260 | " s.next = p\n",
261 | " \n",
262 | " self.head = new_head \n",
263 | " return self.head\n",
264 | "\n",
265 | "llist_1 = LinkedList()\n",
266 | "llist_2 = LinkedList()\n",
267 | "\n",
268 | "llist_1.append(1)\n",
269 | "llist_1.append(5)\n",
270 | "llist_1.append(7)\n",
271 | "llist_1.append(9)\n",
272 | "llist_1.append(10)\n",
273 | "\n",
274 | "llist_2.append(2)\n",
275 | "llist_2.append(3)\n",
276 | "llist_2.append(4)\n",
277 | "llist_2.append(6)\n",
278 | "llist_2.append(8)\n",
279 | "\n",
280 | "llist_1.merge_sorted(llist_2)\n",
281 | "llist_1.print_list()"
282 | ]
283 | }
284 | ]
285 | }
--------------------------------------------------------------------------------
/Remove_Duplicates_in_link_list.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyP5ezsxn54aNvA8EJkYbrIZ",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "80tClUNO6tZp"
34 | },
35 | "outputs": [],
36 | "source": [
37 | "class Node:\n",
38 | " def __init__(self, data):\n",
39 | " self.data = data\n",
40 | " self.next = None\n",
41 | "\n",
42 | "\n",
43 | "class LinkedList:\n",
44 | " def __init__(self):\n",
45 | " self.head = None\n",
46 | "\n",
47 | " def print_list(self):\n",
48 | " cur_node = self.head\n",
49 | " while cur_node:\n",
50 | " print(cur_node.data)\n",
51 | " cur_node = cur_node.next\n",
52 | "\n",
53 | " def append(self, data):\n",
54 | " new_node = Node(data)\n",
55 | "\n",
56 | " if self.head is None:\n",
57 | " self.head = new_node\n",
58 | " return\n",
59 | "\n",
60 | " last_node = self.head\n",
61 | " while last_node.next:\n",
62 | " last_node = last_node.next\n",
63 | " last_node.next = new_node\n",
64 | "\n",
65 | " def prepend(self, data):\n",
66 | " new_node = Node(data)\n",
67 | "\n",
68 | " new_node.next = self.head\n",
69 | " self.head = new_node\n",
70 | "\n",
71 | " def insert_after_node(self, prev_node, data):\n",
72 | "\n",
73 | " if not prev_node:\n",
74 | " print(\"Previous node does not exist.\")\n",
75 | " return \n",
76 | "\n",
77 | " new_node = Node(data)\n",
78 | "\n",
79 | " new_node.next = prev_node.next\n",
80 | " prev_node.next = new_node\n",
81 | "\n",
82 | " def delete_node(self, key):\n",
83 | "\n",
84 | " cur_node = self.head\n",
85 | "\n",
86 | " if cur_node and cur_node.data == key:\n",
87 | " self.head = cur_node.next\n",
88 | " cur_node = None\n",
89 | " return\n",
90 | "\n",
91 | " prev = None \n",
92 | " while cur_node and cur_node.data != key:\n",
93 | " prev = cur_node\n",
94 | " cur_node = cur_node.next\n",
95 | "\n",
96 | " if cur_node is None:\n",
97 | " return \n",
98 | "\n",
99 | " prev.next = cur_node.next\n",
100 | " cur_node = None\n",
101 | "\n",
102 | " def delete_node_at_pos(self, pos):\n",
103 | " if self.head:\n",
104 | " cur_node = self.head\n",
105 | "\n",
106 | " if pos == 0:\n",
107 | " self.head = cur_node.next\n",
108 | " cur_node = None\n",
109 | " return\n",
110 | "\n",
111 | " prev = None\n",
112 | " count = 1\n",
113 | " while cur_node and count != pos:\n",
114 | " prev = cur_node \n",
115 | " cur_node = cur_node.next\n",
116 | " count += 1\n",
117 | "\n",
118 | " if cur_node is None:\n",
119 | " return \n",
120 | "\n",
121 | " prev.next = cur_node.next\n",
122 | " cur_node = None\n",
123 | "\n",
124 | " def len_iterative(self):\n",
125 | "\n",
126 | " count = 0\n",
127 | " cur_node = self.head\n",
128 | "\n",
129 | " while cur_node:\n",
130 | " count += 1\n",
131 | " cur_node = cur_node.next\n",
132 | " return count\n",
133 | "\n",
134 | " def len_recursive(self, node):\n",
135 | " if node is None:\n",
136 | " return 0\n",
137 | " return 1 + self.len_recursive(node.next)\n",
138 | "\n",
139 | " def swap_nodes(self, key_1, key_2):\n",
140 | "\n",
141 | " if key_1 == key_2:\n",
142 | " return \n",
143 | "\n",
144 | " prev_1 = None \n",
145 | " curr_1 = self.head \n",
146 | " while curr_1 and curr_1.data != key_1:\n",
147 | " prev_1 = curr_1 \n",
148 | " curr_1 = curr_1.next\n",
149 | "\n",
150 | " prev_2 = None \n",
151 | " curr_2 = self.head \n",
152 | " while curr_2 and curr_2.data != key_2:\n",
153 | " prev_2 = curr_2 \n",
154 | " curr_2 = curr_2.next\n",
155 | "\n",
156 | " if not curr_1 or not curr_2:\n",
157 | " return \n",
158 | "\n",
159 | " if prev_1:\n",
160 | " prev_1.next = curr_2\n",
161 | " else:\n",
162 | " self.head = curr_2\n",
163 | "\n",
164 | " if prev_2:\n",
165 | " prev_2.next = curr_1\n",
166 | " else:\n",
167 | " self.head = curr_1\n",
168 | "\n",
169 | " curr_1.next, curr_2.next = curr_2.next, curr_1.next\n",
170 | "\n",
171 | " def print_helper(self, node, name):\n",
172 | " if node is None:\n",
173 | " print(name + \": None\")\n",
174 | " else:\n",
175 | " print(name + \":\" + node.data)\n",
176 | "\n",
177 | " def reverse_iterative(self):\n",
178 | "\n",
179 | " prev = None \n",
180 | " cur = self.head\n",
181 | " while cur:\n",
182 | " nxt = cur.next\n",
183 | " cur.next = prev\n",
184 | " \n",
185 | " self.print_helper(prev, \"PREV\")\n",
186 | " self.print_helper(cur, \"CUR\")\n",
187 | " self.print_helper(nxt, \"NXT\")\n",
188 | " print(\"\\n\")\n",
189 | "\n",
190 | " prev = cur \n",
191 | " cur = nxt \n",
192 | " self.head = prev\n",
193 | "\n",
194 | " def reverse_recursive(self):\n",
195 | "\n",
196 | " def _reverse_recursive(cur, prev):\n",
197 | " if not cur:\n",
198 | " return prev\n",
199 | "\n",
200 | " nxt = cur.next\n",
201 | " cur.next = prev\n",
202 | " prev = cur \n",
203 | " cur = nxt \n",
204 | " return _reverse_recursive(cur, prev)\n",
205 | "\n",
206 | " self.head = _reverse_recursive(cur=self.head, prev=None)\n",
207 | "\n",
208 | " def merge_sorted(self, llist):\n",
209 | " \n",
210 | " p = self.head \n",
211 | " q = llist.head\n",
212 | " s = None\n",
213 | " \n",
214 | " if not p:\n",
215 | " return q\n",
216 | " if not q:\n",
217 | " return p\n",
218 | "\n",
219 | " if p and q:\n",
220 | " if p.data <= q.data:\n",
221 | " s = p \n",
222 | " p = s.next\n",
223 | " else:\n",
224 | " s = q\n",
225 | " q = s.next\n",
226 | " new_head = s \n",
227 | " while p and q:\n",
228 | " if p.data <= q.data:\n",
229 | " s.next = p \n",
230 | " s = p \n",
231 | " p = s.next\n",
232 | " else:\n",
233 | " s.next = q\n",
234 | " s = q\n",
235 | " q = s.next\n",
236 | " if not p:\n",
237 | " s.next = q \n",
238 | " if not q:\n",
239 | " s.next = p \n",
240 | " return new_head\n",
241 | "\n",
242 | " def remove_duplicates(self):\n",
243 | " \n",
244 | " cur = self.head\n",
245 | " prev = None\n",
246 | "\n",
247 | " dup_values = dict()\n",
248 | "\n",
249 | " while cur:\n",
250 | " if cur.data in dup_values:\n",
251 | " # Remove node:\n",
252 | " prev.next = cur.next\n",
253 | " cur = None\n",
254 | " else:\n",
255 | " # Have not encountered element before.\n",
256 | " dup_values[cur.data] = 1\n",
257 | " prev = cur\n",
258 | " cur = prev.next\n",
259 | "\n",
260 | "llist = LinkedList()\n",
261 | "llist.append(1)\n",
262 | "llist.append(6)\n",
263 | "llist.append(1)\n",
264 | "llist.append(4)\n",
265 | "llist.append(2)\n",
266 | "llist.append(2)\n",
267 | "llist.append(4)\n",
268 | "\n",
269 | "print(\"Original Linked List\")\n",
270 | "llist.print_list()\n",
271 | "print(\"Linked List After Removing Duplicates\")\n",
272 | "llist.remove_duplicates()\n",
273 | "llist.print_list()"
274 | ]
275 | }
276 | ]
277 | }
--------------------------------------------------------------------------------
/Functions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyOKkUu7bI71d7DP3+YSTha2",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "**Function**\n",
33 | "\n",
34 | "A function is a block of code which only runs when it is called.\n",
35 | "\n",
36 | "You can pass data, known as parameters, into a function.\n",
37 | "\n",
38 | "A function can return data as a result.\n",
39 | "\n",
40 | "print( ) and input( ) are some functions that you’ve already used. And these are inbuilt functions.\n"
41 | ],
42 | "metadata": {
43 | "id": "zek-lhzaGf9w"
44 | }
45 | },
46 | {
47 | "cell_type": "code",
48 | "execution_count": 2,
49 | "metadata": {
50 | "colab": {
51 | "base_uri": "https://localhost:8080/"
52 | },
53 | "id": "7mkTi-lZGSf1",
54 | "outputId": "4fe84d76-d4b5-4dae-8369-bf5b81087a08"
55 | },
56 | "outputs": [
57 | {
58 | "output_type": "stream",
59 | "name": "stdout",
60 | "text": [
61 | "Welcome to DSA with Python\n"
62 | ]
63 | }
64 | ],
65 | "source": [
66 | "# Python Functions are a block of statements that return the specific task.\n",
67 | "\n",
68 | "# Decleration of a function\n",
69 | "def subject():\n",
70 | " print(\"Welcome to DSA with Python\")\n",
71 | "\n",
72 | "# Driver code to call a function\n",
73 | "subject()\n"
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "source": [
79 | "#Defining and calling a function with parameters\n",
80 | "def add(num1: int, num2: int) -> int:\n",
81 | " \"\"\"Add two numbers\"\"\"\n",
82 | " num3 = num1 + num2\n",
83 | " return num3\n",
84 | "\n",
85 | "# Driver code\n",
86 | "num1, num2 = 5, 15\n",
87 | "ans = add(num1, num2)\n",
88 | "print(f\"The addition of {num1} and {num2} results {ans}.\")\n"
89 | ],
90 | "metadata": {
91 | "colab": {
92 | "base_uri": "https://localhost:8080/"
93 | },
94 | "id": "n93Y7z2KHasO",
95 | "outputId": "7e304ab3-c638-4f42-ce3e-469f289202ab"
96 | },
97 | "execution_count": 3,
98 | "outputs": [
99 | {
100 | "output_type": "stream",
101 | "name": "stdout",
102 | "text": [
103 | "The addition of 5 and 15 results 20.\n"
104 | ]
105 | }
106 | ]
107 | },
108 | {
109 | "cell_type": "code",
110 | "source": [
111 | "# Examples of Python built-in functions\n",
112 | "\n",
113 | "#abs(x) \n",
114 | "# Returns the absolute value of a number. In case a complex number is passed, the magnitude of that number is returned. It is the same as the distance from the origin of a point on an x-y graph. For e.g.-\n",
115 | "# Abs(-3) =3 \n",
116 | "# abs(3+4i) = 5\n",
117 | "a = 12\n",
118 | "b = -4\n",
119 | "c = 3+4j\n",
120 | "d = 7.90\n",
121 | "print(abs(a))\n",
122 | "print(abs(b))\n",
123 | "print(abs(c))\n",
124 | "print(abs(d))\n",
125 | "\n",
126 | "#bin()\n",
127 | "#This function returns the binary value of a number.\n",
128 | "a=5\n",
129 | "print(bin(a))\n",
130 | "\n",
131 | "age = 25\n",
132 | "print(bin(age))\n",
133 | "\n",
134 | "# round()\n",
135 | "#It gives a roundoff value for a number, i.e. gives the nearest integer value for a number. This function accepts one argument, either decimal, float or integer and gives roundoff output.\n",
136 | "print(round(4.5))\n",
137 | "print(round(-7.7))\n",
138 | "\n",
139 | "# list()\n",
140 | "\n",
141 | "# This function is used to convert an object to a list object.\n",
142 | "print(list()) #returns empty list\n",
143 | "stringobj = 'PALINDROME'\n",
144 | "print(list(stringobj))\n",
145 | "tupleobj = ('a', 'e', 'i', 'o', 'u')\n",
146 | "print(list(tupleobj))\n",
147 | "listobj = ['1', '2', '3', 'o', '10u']\n",
148 | "print(list(listobj))\n",
149 | "\n",
150 | "# len()\n",
151 | "# This function gives the length of the object as an output.\n",
152 | "stringobj = 'PALINDROME'\n",
153 | "print(len(stringobj))\n",
154 | "tupleobj = ('a', 'e', 'i', 'o', 'u')\n",
155 | "print(len(tupleobj))\n",
156 | "listobj = ['1', '2', '3', 'o', '10u']\n",
157 | "print(len(listobj))\n",
158 | "\n",
159 | "# max()\n",
160 | "# This function returns the largest number in the given iterable object or the maximum of two or more numbers given as arguments.\n",
161 | "num = [11, 13, 12, 15, 14]\n",
162 | "print('Maximum is:', max(num))\n",
163 | "\n",
164 | "# min()\n",
165 | "# This function returns the minimum value from the collection object or the values defined as arguments.\n",
166 | "print(min(2,5,3,1,0,99))\n",
167 | "sampleObj = ['B','a','t','A']\n",
168 | "print(min(sampleObj))\n",
169 | "\n",
170 | "# map()\n",
171 | "# This function helps in debugging as it provides the result after an operation is applied to each of the items in an iterable object.\n",
172 | "\n",
173 | "numList = (11, 21, 13, 41)\n",
174 | "res = map(lambda x: x + x, numList)\n",
175 | "print(list(res))\n",
176 | "\n",
177 | "#pow()\n",
178 | "#This function returns the value of the power of 1 number to another number.\n",
179 | "print(pow(2,-3))\n",
180 | "print(pow(2,4.5))\n",
181 | "print(pow(3,0))\n",
182 | "\n",
183 | "\n",
184 | "#sorted()\n",
185 | "#This function has made the sorting of the numbers very easy.\n",
186 | "\n",
187 | "sampleObj = (3,6,8,2,5,8,10)\n",
188 | "print(sorted(sampleObj,reverse=True))\n",
189 | "sampledict = {'a':'sss','g':'wq','t':2}\n",
190 | "print(sorted(sampledict,key= len))\n",
191 | "\n",
192 | "#sum()\n",
193 | "#This function helps to sum the member of an iterable object.\n",
194 | "\n",
195 | "num = [2.5, 3, 4, -5]\n",
196 | "numSum = sum(num)\n",
197 | "print(numSum)\n",
198 | "numSum = sum(num, 20)\n",
199 | "print(numSum)\n",
200 | "\n",
201 | "#range(start,stop,step)\n",
202 | "\n",
203 | "# Here, start- an Integer which marks the start of the series.\n",
204 | "# A stop-an integer that marks the last number of the series. The last number in the range is stop-1.\n",
205 | "#Step – an integer that lets to increment the loop with a specific number. By default, it is +1.\n",
206 | "\n",
207 | "res = 1\n",
208 | "for i in range(1, 10,2):\n",
209 | " res = res * i\n",
210 | "print(\"multiplication of first 10 natural number :\", res)\n"
211 | ],
212 | "metadata": {
213 | "colab": {
214 | "base_uri": "https://localhost:8080/"
215 | },
216 | "id": "_-VMP-3kIQ67",
217 | "outputId": "dd884d3b-f049-4a28-bf8e-dc60d6b1e7d2"
218 | },
219 | "execution_count": 15,
220 | "outputs": [
221 | {
222 | "output_type": "stream",
223 | "name": "stdout",
224 | "text": [
225 | "12\n",
226 | "4\n",
227 | "5.0\n",
228 | "7.9\n",
229 | "0b101\n",
230 | "0b11001\n",
231 | "4\n",
232 | "-8\n",
233 | "[]\n",
234 | "['P', 'A', 'L', 'I', 'N', 'D', 'R', 'O', 'M', 'E']\n",
235 | "['a', 'e', 'i', 'o', 'u']\n",
236 | "['1', '2', '3', 'o', '10u']\n",
237 | "10\n",
238 | "5\n",
239 | "5\n",
240 | "Maximum is: 15\n",
241 | "0\n",
242 | "A\n",
243 | "[22, 42, 26, 82]\n",
244 | "0.125\n",
245 | "22.627416997969522\n",
246 | "1\n",
247 | "[10, 8, 8, 6, 5, 3, 2]\n",
248 | "['a', 'g', 't']\n",
249 | "4.5\n",
250 | "24.5\n",
251 | "multiplication of first 10 natural number : 945\n"
252 | ]
253 | }
254 | ]
255 | },
256 | {
257 | "cell_type": "markdown",
258 | "source": [
259 | "**Methods**
\n",
260 | "A method in python is somewhat similar to a function, except it is associated with object/classes. Methods in python are very similar to functions except for two major differences.\n",
261 | "\n",
262 | "* The method is implicitly used for an object for which it is called.\n",
263 | "\n",
264 | "* The method is accessible to data that is contained within the class."
265 | ],
266 | "metadata": {
267 | "id": "tKsdbdhlTKsI"
268 | }
269 | },
270 | {
271 | "cell_type": "code",
272 | "source": [
273 | "class car(object):\n",
274 | " def my_method(self):\n",
275 | " print(\"I am a Swift Dzire 900\")\n",
276 | "maruti = car()\n",
277 | "maruti.my_method()"
278 | ],
279 | "metadata": {
280 | "colab": {
281 | "base_uri": "https://localhost:8080/"
282 | },
283 | "id": "QlrEZfoMTRjF",
284 | "outputId": "64db1f5b-02a4-4173-b451-1cf8e0b8203a"
285 | },
286 | "execution_count": 16,
287 | "outputs": [
288 | {
289 | "output_type": "stream",
290 | "name": "stdout",
291 | "text": [
292 | "I am a Swift Dzire 900\n"
293 | ]
294 | }
295 | ]
296 | }
297 | ]
298 | }
--------------------------------------------------------------------------------
/BasicCommands.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyO+Jgnxb9xUtH7HoMY+iveA",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "**Basics of Python**"
33 | ],
34 | "metadata": {
35 | "id": "s-DPe74Ja57h"
36 | }
37 | },
38 | {
39 | "cell_type": "code",
40 | "execution_count": null,
41 | "metadata": {
42 | "colab": {
43 | "base_uri": "https://localhost:8080/"
44 | },
45 | "id": "2G7sZcWEZhfs",
46 | "outputId": "b8c87939-14e0-481e-eded-b3a0ab0db2dd"
47 | },
48 | "outputs": [
49 | {
50 | "output_type": "stream",
51 | "name": "stdout",
52 | "text": [
53 | "Learning Python\n"
54 | ]
55 | }
56 | ],
57 | "source": [
58 | "print(\"Learning Python\")"
59 | ]
60 | },
61 | {
62 | "cell_type": "markdown",
63 | "source": [
64 | "**Comments in Python**"
65 | ],
66 | "metadata": {
67 | "id": "RmgKQopnbEBo"
68 | }
69 | },
70 | {
71 | "cell_type": "code",
72 | "source": [
73 | "# Assign a value 25 to number (# symbol used for Comments)\n",
74 | "number = 25\n",
75 | "number"
76 | ],
77 | "metadata": {
78 | "colab": {
79 | "base_uri": "https://localhost:8080/"
80 | },
81 | "id": "Y3miR9TfbJ3A",
82 | "outputId": "0e262ad1-2e11-446d-9992-9cc68222e57b"
83 | },
84 | "execution_count": null,
85 | "outputs": [
86 | {
87 | "output_type": "execute_result",
88 | "data": {
89 | "text/plain": [
90 | "25"
91 | ]
92 | },
93 | "metadata": {},
94 | "execution_count": 3
95 | }
96 | ]
97 | },
98 | {
99 | "cell_type": "markdown",
100 | "source": [
101 | "**Operators in Python**"
102 | ],
103 | "metadata": {
104 | "id": "9ME8m-tbb5iQ"
105 | }
106 | },
107 | {
108 | "cell_type": "code",
109 | "source": [
110 | "# Arithmetic Operators\n",
111 | "# We can perform some basic arithmetic operations such as addition, subtraction, multiplication, etc. using Python.\n",
112 | "#Example:\n",
113 | "\n",
114 | "x = 20\n",
115 | "y = 3\n",
116 | "print(x + y) # Addition\n",
117 | "print(x * y) # Multiplication\n",
118 | "print(x - y) # Substraction\n",
119 | "print(x / y) # Division\n",
120 | "print(x % y) # Modulus\n",
121 | "print(x ** y) # Exponent\n",
122 | "print(x // y) # Floor \n"
123 | ],
124 | "metadata": {
125 | "colab": {
126 | "base_uri": "https://localhost:8080/"
127 | },
128 | "id": "Z2AB1gtGb8AH",
129 | "outputId": "9a866135-06cb-4c05-9059-ceabfc4d5a45"
130 | },
131 | "execution_count": null,
132 | "outputs": [
133 | {
134 | "output_type": "stream",
135 | "name": "stdout",
136 | "text": [
137 | "23\n",
138 | "60\n",
139 | "17\n",
140 | "6.666666666666667\n",
141 | "2\n",
142 | "8000\n",
143 | "6\n"
144 | ]
145 | }
146 | ]
147 | },
148 | {
149 | "cell_type": "markdown",
150 | "source": [
151 | "\n",
152 | "**Comparison Operator**"
153 | ],
154 | "metadata": {
155 | "id": "tU6xnEKuhAat"
156 | }
157 | },
158 | {
159 | "cell_type": "code",
160 | "source": [
161 | "# These are used to compare two values\n",
162 | "# Gives a Boolean result (True/False)\n",
163 | "\n",
164 | "# Numeric Calculation\n",
165 | "number_1 = 25\n",
166 | "number_2 = 30\n",
167 | "\n",
168 | "print(number_1 > number_2)\n",
169 | "print(number_1 < number_2)\n",
170 | "print(number_1 == number_2)\n",
171 | "print(number_1 != number_2)\n",
172 | "print(number_1 >= number_2)\n",
173 | "print(number_1 <= number_2)"
174 | ],
175 | "metadata": {
176 | "colab": {
177 | "base_uri": "https://localhost:8080/"
178 | },
179 | "id": "S_OUe1c_hQSs",
180 | "outputId": "b97c597b-51f6-4181-ab8c-f9ebb9c4ae40"
181 | },
182 | "execution_count": null,
183 | "outputs": [
184 | {
185 | "output_type": "stream",
186 | "name": "stdout",
187 | "text": [
188 | "False\n",
189 | "True\n",
190 | "False\n",
191 | "True\n",
192 | "False\n",
193 | "True\n"
194 | ]
195 | }
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "source": [
201 | "# Other Comparision\n",
202 | "fruit= \"Apple\"\n",
203 | "print (fruit == 'Apple')\t\n",
204 | "print (fruit != 'Apple')\t\n",
205 | "print (fruit < 'Apple')\t\n",
206 | "print (fruit > 'Apple')\t\n",
207 | "print (fruit <= 'Apple')\t\n",
208 | "print (fruit >= 'Apple')\t"
209 | ],
210 | "metadata": {
211 | "colab": {
212 | "base_uri": "https://localhost:8080/"
213 | },
214 | "id": "YmonGw3AiXfz",
215 | "outputId": "65766996-abfc-4eca-9ead-a7c44b39a76c"
216 | },
217 | "execution_count": null,
218 | "outputs": [
219 | {
220 | "output_type": "stream",
221 | "name": "stdout",
222 | "text": [
223 | "True\n",
224 | "False\n",
225 | "False\n",
226 | "False\n",
227 | "True\n",
228 | "True\n"
229 | ]
230 | }
231 | ]
232 | },
233 | {
234 | "cell_type": "markdown",
235 | "source": [
236 | "**Logical Operator**"
237 | ],
238 | "metadata": {
239 | "id": "Q1ofM76flSGE"
240 | }
241 | },
242 | {
243 | "cell_type": "code",
244 | "source": [
245 | "# Logical operators are used to combine conditional statements\n",
246 | "# Gives a Boolean result (True/False)\n",
247 | "number_1 = 25\n",
248 | "print (number_1<10 and number_1>50) \n",
249 | "print (number_1>10 and number_1<50)\n",
250 | "print (number_1>10 or number_1<50)\n",
251 | "print (number_1<10 or number_1>50)"
252 | ],
253 | "metadata": {
254 | "colab": {
255 | "base_uri": "https://localhost:8080/"
256 | },
257 | "id": "imxScvv7liOI",
258 | "outputId": "30219f48-a5d1-4385-ed28-4d87c7c4e3b9"
259 | },
260 | "execution_count": null,
261 | "outputs": [
262 | {
263 | "output_type": "stream",
264 | "name": "stdout",
265 | "text": [
266 | "False\n",
267 | "True\n",
268 | "True\n",
269 | "False\n"
270 | ]
271 | }
272 | ]
273 | },
274 | {
275 | "cell_type": "markdown",
276 | "source": [
277 | "**Variable**"
278 | ],
279 | "metadata": {
280 | "id": "IPQw-0I0m2o9"
281 | }
282 | },
283 | {
284 | "cell_type": "code",
285 | "source": [
286 | "# A variable can be considered as a storage container for data.\n",
287 | "# Every variable will have a name.\n",
288 | "# It is a good way to store information while making it easy to refer to that information in our code later.\n",
289 | "# The equal sign (=) is used to assign values to variables.\n",
290 | "# The syntax for assigning values to a variable is as follows: Variable name = value or information.\n",
291 | "\n",
292 | "Name = \"Sajid\"\n",
293 | "Age = 30\n",
294 | "IP = \"192.168.0.0\"\n",
295 | "print (Name)\n",
296 | "print (Age)\n",
297 | "print (IP)"
298 | ],
299 | "metadata": {
300 | "colab": {
301 | "base_uri": "https://localhost:8080/"
302 | },
303 | "id": "bH8_1RdTm475",
304 | "outputId": "079d3490-37eb-47f9-c1cf-8c32caf05e26"
305 | },
306 | "execution_count": null,
307 | "outputs": [
308 | {
309 | "output_type": "stream",
310 | "name": "stdout",
311 | "text": [
312 | "Sajid\n",
313 | "30\n",
314 | "192.168.0.0\n"
315 | ]
316 | }
317 | ]
318 | },
319 | {
320 | "cell_type": "markdown",
321 | "source": [
322 | "**Input and Output in Python**"
323 | ],
324 | "metadata": {
325 | "id": "qDjj-ncqbKfb"
326 | }
327 | },
328 | {
329 | "cell_type": "code",
330 | "source": [
331 | "# Print Function\n",
332 | "print (\"Data Structure with Python\")\n",
333 | "# Input Function\n",
334 | "age = int(input(\"What is your Age:\"))\n",
335 | "print (age)\n",
336 | "print(type(age))\n",
337 | "name = input(\"What is your Name:\")\n",
338 | "print (name)\n",
339 | "print(type(name))\n",
340 | "x= 2*2**3\n",
341 | "print(x)"
342 | ],
343 | "metadata": {
344 | "colab": {
345 | "base_uri": "https://localhost:8080/"
346 | },
347 | "id": "nKnzQClgchJ8",
348 | "outputId": "a61d044a-bb5f-4cb8-e8b1-8249ea1bf854"
349 | },
350 | "execution_count": null,
351 | "outputs": [
352 | {
353 | "output_type": "stream",
354 | "name": "stdout",
355 | "text": [
356 | "Data Structure with Python\n",
357 | "What is your Age:2\n",
358 | "2\n",
359 | "\n",
360 | "What is your Name:sajid\n",
361 | "sajid\n",
362 | "\n",
363 | "16\n"
364 | ]
365 | }
366 | ]
367 | },
368 | {
369 | "cell_type": "markdown",
370 | "source": [
371 | "**Task Excercise**
\n",
372 | "**1-Consider two numbers, a = 4, b = 16. Write a Python program to print the sum of these two numbers.**
\n",
373 | "**2- Write a Python program to take a number from the user as input and print the cube of the number.**"
374 | ],
375 | "metadata": {
376 | "id": "j9_0-CrmgIR9"
377 | }
378 | },
379 | {
380 | "cell_type": "code",
381 | "source": [
382 | "#Task 1\n",
383 | "# Take two number a & B\n",
384 | "a = 4\n",
385 | "b = 16\n",
386 | "print(a+b)\n",
387 | "\n",
388 | "#Task 2\n",
389 | "number = int (input(\"Enter Your Number:\"))\n",
390 | "print(number**3)"
391 | ],
392 | "metadata": {
393 | "colab": {
394 | "base_uri": "https://localhost:8080/"
395 | },
396 | "id": "LrzL0v_Vgz3z",
397 | "outputId": "4048111c-e293-43f9-dc8d-515b4c34741f"
398 | },
399 | "execution_count": null,
400 | "outputs": [
401 | {
402 | "output_type": "stream",
403 | "name": "stdout",
404 | "text": [
405 | "20\n",
406 | "Enter Your Number:3\n",
407 | "27\n"
408 | ]
409 | }
410 | ]
411 | }
412 | ]
413 | }
--------------------------------------------------------------------------------
/palindrome_link_list_.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyPdmQRKPMk3vpiKNixrmfPv",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 1,
32 | "metadata": {
33 | "colab": {
34 | "base_uri": "https://localhost:8080/"
35 | },
36 | "id": "XwyAA6Y5Z0CW",
37 | "outputId": "f1ba4e73-21f8-47ed-d829-32851d3446b9"
38 | },
39 | "outputs": [
40 | {
41 | "output_type": "stream",
42 | "name": "stdout",
43 | "text": [
44 | "True\n",
45 | "True\n",
46 | "True\n",
47 | "False\n",
48 | "False\n",
49 | "False\n"
50 | ]
51 | }
52 | ],
53 | "source": [
54 | "class Node:\n",
55 | " def __init__(self, data):\n",
56 | " self.data = data\n",
57 | " self.next = None\n",
58 | "\n",
59 | "\n",
60 | "class LinkedList:\n",
61 | " def __init__(self):\n",
62 | " self.head = None\n",
63 | "\n",
64 | " def print_list(self):\n",
65 | " cur_node = self.head\n",
66 | " while cur_node:\n",
67 | " print(cur_node.data)\n",
68 | " cur_node = cur_node.next\n",
69 | "\n",
70 | " def append(self, data):\n",
71 | " new_node = Node(data)\n",
72 | "\n",
73 | " if self.head is None:\n",
74 | " self.head = new_node\n",
75 | " return\n",
76 | "\n",
77 | " last_node = self.head\n",
78 | " while last_node.next:\n",
79 | " last_node = last_node.next\n",
80 | " last_node.next = new_node\n",
81 | "\n",
82 | " def prepend(self, data):\n",
83 | " new_node = Node(data)\n",
84 | "\n",
85 | " new_node.next = self.head\n",
86 | " self.head = new_node\n",
87 | "\n",
88 | " def insert_after_node(self, prev_node, data):\n",
89 | "\n",
90 | " if not prev_node:\n",
91 | " print(\"Previous node does not exist.\")\n",
92 | " return \n",
93 | "\n",
94 | " new_node = Node(data)\n",
95 | "\n",
96 | " new_node.next = prev_node.next\n",
97 | " prev_node.next = new_node\n",
98 | "\n",
99 | " def delete_node(self, key):\n",
100 | "\n",
101 | " cur_node = self.head\n",
102 | "\n",
103 | " if cur_node and cur_node.data == key:\n",
104 | " self.head = cur_node.next\n",
105 | " cur_node = None\n",
106 | " return\n",
107 | "\n",
108 | " prev = None \n",
109 | " while cur_node and cur_node.data != key:\n",
110 | " prev = cur_node\n",
111 | " cur_node = cur_node.next\n",
112 | "\n",
113 | " if cur_node is None:\n",
114 | " return \n",
115 | "\n",
116 | " prev.next = cur_node.next\n",
117 | " cur_node = None\n",
118 | "\n",
119 | " def delete_node_at_pos(self, pos):\n",
120 | " if self.head:\n",
121 | " cur_node = self.head\n",
122 | "\n",
123 | " if pos == 0:\n",
124 | " self.head = cur_node.next\n",
125 | " cur_node = None\n",
126 | " return\n",
127 | "\n",
128 | " prev = None\n",
129 | " count = 1\n",
130 | " while cur_node and count != pos:\n",
131 | " prev = cur_node \n",
132 | " cur_node = cur_node.next\n",
133 | " count += 1\n",
134 | "\n",
135 | " if cur_node is None:\n",
136 | " return \n",
137 | "\n",
138 | " prev.next = cur_node.next\n",
139 | " cur_node = None\n",
140 | "\n",
141 | " def len_iterative(self):\n",
142 | "\n",
143 | " count = 0\n",
144 | " cur_node = self.head\n",
145 | "\n",
146 | " while cur_node:\n",
147 | " count += 1\n",
148 | " cur_node = cur_node.next\n",
149 | " return count\n",
150 | "\n",
151 | " def len_recursive(self, node):\n",
152 | " if node is None:\n",
153 | " return 0\n",
154 | " return 1 + self.len_recursive(node.next)\n",
155 | "\n",
156 | " def swap_nodes(self, key_1, key_2):\n",
157 | "\n",
158 | " if key_1 == key_2:\n",
159 | " return \n",
160 | "\n",
161 | " prev_1 = None \n",
162 | " curr_1 = self.head \n",
163 | " while curr_1 and curr_1.data != key_1:\n",
164 | " prev_1 = curr_1 \n",
165 | " curr_1 = curr_1.next\n",
166 | "\n",
167 | " prev_2 = None \n",
168 | " curr_2 = self.head \n",
169 | " while curr_2 and curr_2.data != key_2:\n",
170 | " prev_2 = curr_2 \n",
171 | " curr_2 = curr_2.next\n",
172 | "\n",
173 | " if not curr_1 or not curr_2:\n",
174 | " return \n",
175 | "\n",
176 | " if prev_1:\n",
177 | " prev_1.next = curr_2\n",
178 | " else:\n",
179 | " self.head = curr_2\n",
180 | "\n",
181 | " if prev_2:\n",
182 | " prev_2.next = curr_1\n",
183 | " else:\n",
184 | " self.head = curr_1\n",
185 | "\n",
186 | " curr_1.next, curr_2.next = curr_2.next, curr_1.next\n",
187 | "\n",
188 | " def print_helper(self, node, name):\n",
189 | " if node is None:\n",
190 | " print(name + \": None\")\n",
191 | " else:\n",
192 | " print(name + \":\" + node.data)\n",
193 | "\n",
194 | " def reverse_iterative(self):\n",
195 | "\n",
196 | " prev = None \n",
197 | " cur = self.head\n",
198 | " while cur:\n",
199 | " nxt = cur.next\n",
200 | " cur.next = prev\n",
201 | " \n",
202 | " self.print_helper(prev, \"PREV\")\n",
203 | " self.print_helper(cur, \"CUR\")\n",
204 | " self.print_helper(nxt, \"NXT\")\n",
205 | " print(\"\\n\")\n",
206 | "\n",
207 | " prev = cur \n",
208 | " cur = nxt \n",
209 | " self.head = prev\n",
210 | "\n",
211 | " def reverse_recursive(self):\n",
212 | "\n",
213 | " def _reverse_recursive(cur, prev):\n",
214 | " if not cur:\n",
215 | " return prev\n",
216 | "\n",
217 | " nxt = cur.next\n",
218 | " cur.next = prev\n",
219 | " prev = cur \n",
220 | " cur = nxt \n",
221 | " return _reverse_recursive(cur, prev)\n",
222 | "\n",
223 | " self.head = _reverse_recursive(cur=self.head, prev=None)\n",
224 | "\n",
225 | " def merge_sorted(self, llist):\n",
226 | " \n",
227 | " p = self.head \n",
228 | " q = llist.head\n",
229 | " s = None\n",
230 | " \n",
231 | " if not p:\n",
232 | " return q\n",
233 | " if not q:\n",
234 | " return p\n",
235 | "\n",
236 | " if p and q:\n",
237 | " if p.data <= q.data:\n",
238 | " s = p \n",
239 | " p = s.next\n",
240 | " else:\n",
241 | " s = q\n",
242 | " q = s.next\n",
243 | " new_head = s \n",
244 | " while p and q:\n",
245 | " if p.data <= q.data:\n",
246 | " s.next = p \n",
247 | " s = p \n",
248 | " p = s.next\n",
249 | " else:\n",
250 | " s.next = q\n",
251 | " s = q\n",
252 | " q = s.next\n",
253 | " if not p:\n",
254 | " s.next = q \n",
255 | " if not q:\n",
256 | " s.next = p \n",
257 | " return new_head\n",
258 | "\n",
259 | " def remove_duplicates(self):\n",
260 | " \n",
261 | " cur = self.head\n",
262 | " prev = None\n",
263 | "\n",
264 | " dup_values = dict()\n",
265 | "\n",
266 | " while cur:\n",
267 | " if cur.data in dup_values:\n",
268 | " # Remove node:\n",
269 | " prev.next = cur.next\n",
270 | " cur = None\n",
271 | " else:\n",
272 | " # Have not encountered element before.\n",
273 | " dup_values[cur.data] = 1\n",
274 | " prev = cur\n",
275 | " cur = prev.next\n",
276 | "\n",
277 | " def print_nth_from_last(self, n, method):\n",
278 | " if method == 1:\n",
279 | " #Method 1:\n",
280 | " total_len = self.len_iterative()\n",
281 | " cur = self.head \n",
282 | " while cur:\n",
283 | " if total_len == n:\n",
284 | " #print(cur.data)\n",
285 | " return cur.data\n",
286 | " total_len -= 1\n",
287 | " cur = cur.next\n",
288 | " if cur is None:\n",
289 | " return\n",
290 | "\n",
291 | " elif method == 2:\n",
292 | " # Method 2:\n",
293 | " p = self.head\n",
294 | " q = self.head\n",
295 | "\n",
296 | " count = 0\n",
297 | " while q:\n",
298 | " count += 1\n",
299 | " if(count>=n):\n",
300 | " break\n",
301 | " q = q.next\n",
302 | " \n",
303 | " if not q:\n",
304 | " print(str(n) + \" is greater than the number of nodes in list.\")\n",
305 | " return\n",
306 | "\n",
307 | " while p and q.next:\n",
308 | " p = p.next\n",
309 | " q = q.next\n",
310 | " return p.data\n",
311 | "\n",
312 | " def rotate(self, k):\n",
313 | " if self.head and self.head.next:\n",
314 | " p = self.head \n",
315 | " q = self.head \n",
316 | " prev = None \n",
317 | " count = 0\n",
318 | " \n",
319 | " while p and count < k:\n",
320 | " prev = p\n",
321 | " p = p.next \n",
322 | " q = q.next \n",
323 | " count += 1\n",
324 | " p = prev\n",
325 | " while q:\n",
326 | " prev = q \n",
327 | " q = q.next \n",
328 | " q = prev \n",
329 | "\n",
330 | " q.next = self.head \n",
331 | " self.head = p.next \n",
332 | " p.next = None\n",
333 | "\n",
334 | " def count_occurences_iterative(self, data):\n",
335 | " count = 0\n",
336 | " cur = self.head\n",
337 | " while cur:\n",
338 | " if cur.data == data:\n",
339 | " count += 1\n",
340 | " cur = cur.next\n",
341 | " return count \n",
342 | "\n",
343 | " def count_occurences_recursive(self, node, data):\n",
344 | " if not node:\n",
345 | " return 0 \n",
346 | " if node.data == data:\n",
347 | " return 1 + self.count_occurences_recursive(node.next, data)\n",
348 | " else:\n",
349 | " return self.count_occurences_recursive(node.next, data)\n",
350 | "\n",
351 | " def is_palindrome_1(self):\n",
352 | " # Solution 1:\n",
353 | " s = \"\"\n",
354 | " p = self.head \n",
355 | " while p:\n",
356 | " s += p.data\n",
357 | " p = p.next\n",
358 | " return s == s[::-1]\n",
359 | " \n",
360 | " def is_palindrome_2(self):\n",
361 | " # Solution 2:\n",
362 | " p = self.head \n",
363 | " s = []\n",
364 | " while p:\n",
365 | " s.append(p.data)\n",
366 | " p = p.next\n",
367 | " p = self.head\n",
368 | " while p:\n",
369 | " data = s.pop()\n",
370 | " if p.data != data:\n",
371 | " return False\n",
372 | " p = p.next\n",
373 | " return True\n",
374 | " \n",
375 | " def is_palindrome_3(self):\n",
376 | " if self.head:\n",
377 | " p = self.head \n",
378 | " q = self.head \n",
379 | " prev = []\n",
380 | " \n",
381 | " i = 0\n",
382 | " while q:\n",
383 | " prev.append(q)\n",
384 | " q = q.next\n",
385 | " i += 1\n",
386 | " q = prev[i-1]\n",
387 | " \n",
388 | " count = 1\n",
389 | "\n",
390 | " while count <= i//2 + 1:\n",
391 | " if prev[-count].data != p.data:\n",
392 | " return False\n",
393 | " p = p.next\n",
394 | " count += 1\n",
395 | " return True\n",
396 | " else:\n",
397 | " return True\n",
398 | "\n",
399 | " def is_palindrome(self,method):\n",
400 | " if method == 1:\n",
401 | " return self.is_palindrome_1()\n",
402 | " elif method == 2:\n",
403 | " return self.is_palindrome_2()\n",
404 | " elif method == 3:\n",
405 | " return self.is_palindrome_3()\n",
406 | " \n",
407 | "\n",
408 | "# Example palindromes:\n",
409 | "# RACECAR, RADAR\n",
410 | "\n",
411 | "# Example non-palindromes:\n",
412 | "# TEST, ABC, HELLO\n",
413 | "\n",
414 | "llist = LinkedList()\n",
415 | "\n",
416 | "\n",
417 | "llist_2 = LinkedList()\n",
418 | "llist_2.append(\"A\")\n",
419 | "llist_2.append(\"B\")\n",
420 | "llist_2.append(\"C\")\n",
421 | "\n",
422 | "print(llist.is_palindrome(1))\n",
423 | "print(llist.is_palindrome(2))\n",
424 | "print(llist.is_palindrome(3))\n",
425 | "print(llist_2.is_palindrome(1))\n",
426 | "print(llist_2.is_palindrome(2))\n",
427 | "print(llist_2.is_palindrome(3))"
428 | ]
429 | }
430 | ]
431 | }
--------------------------------------------------------------------------------
/Move_Tail_to_head_in_link_list.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyM5qxfP2mmwyIRzgDcuHigq",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "\n",
33 | "\n",
34 | "```\n",
35 | "You are required to solve the Move Tail to Head problem in a linked list. In this exercise, you are supposed to move the tail (or last) node in a singly linked list to the front of the linked list so that it becomes the new head of the linked list.\n",
36 | "\n",
37 | "For example, in the illustration below, the tail node (D) moves to the start of the linked list and replaces the head node (A).\n"
38 | ],
39 | "metadata": {
40 | "id": "b5iVS9N6xglC"
41 | }
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "source": [],
46 | "metadata": {
47 | "id": "fXvCbSR5xyaL"
48 | }
49 | },
50 | {
51 | "cell_type": "code",
52 | "execution_count": null,
53 | "metadata": {
54 | "id": "eiZYJgV1xDVH"
55 | },
56 | "outputs": [],
57 | "source": [
58 | "\n",
59 | "class Node:\n",
60 | " def __init__(self, data):\n",
61 | " self.data = data\n",
62 | " self.next = None\n",
63 | "\n",
64 | "\n",
65 | "class LinkedList:\n",
66 | " def __init__(self):\n",
67 | " self.head = None\n",
68 | "\n",
69 | " def print_list(self):\n",
70 | " cur_node = self.head\n",
71 | " while cur_node:\n",
72 | " print(cur_node.data)\n",
73 | " cur_node = cur_node.next\n",
74 | "\n",
75 | " def append(self, data):\n",
76 | " new_node = Node(data)\n",
77 | "\n",
78 | " if self.head is None:\n",
79 | " self.head = new_node\n",
80 | " return\n",
81 | "\n",
82 | " last_node = self.head\n",
83 | " while last_node.next:\n",
84 | " last_node = last_node.next\n",
85 | " last_node.next = new_node\n",
86 | "\n",
87 | " def prepend(self, data):\n",
88 | " new_node = Node(data)\n",
89 | "\n",
90 | " new_node.next = self.head\n",
91 | " self.head = new_node\n",
92 | "\n",
93 | " def insert_after_node(self, prev_node, data):\n",
94 | "\n",
95 | " if not prev_node:\n",
96 | " print(\"Previous node does not exist.\")\n",
97 | " return \n",
98 | "\n",
99 | " new_node = Node(data)\n",
100 | "\n",
101 | " new_node.next = prev_node.next\n",
102 | " prev_node.next = new_node\n",
103 | "\n",
104 | " def delete_node(self, key):\n",
105 | "\n",
106 | " cur_node = self.head\n",
107 | "\n",
108 | " if cur_node and cur_node.data == key:\n",
109 | " self.head = cur_node.next\n",
110 | " cur_node = None\n",
111 | " return\n",
112 | "\n",
113 | " prev = None \n",
114 | " while cur_node and cur_node.data != key:\n",
115 | " prev = cur_node\n",
116 | " cur_node = cur_node.next\n",
117 | "\n",
118 | " if cur_node is None:\n",
119 | " return \n",
120 | "\n",
121 | " prev.next = cur_node.next\n",
122 | " cur_node = None\n",
123 | "\n",
124 | " def delete_node_at_pos(self, pos):\n",
125 | " if self.head:\n",
126 | " cur_node = self.head\n",
127 | "\n",
128 | " if pos == 0:\n",
129 | " self.head = cur_node.next\n",
130 | " cur_node = None\n",
131 | " return\n",
132 | "\n",
133 | " prev = None\n",
134 | " count = 1\n",
135 | " while cur_node and count != pos:\n",
136 | " prev = cur_node \n",
137 | " cur_node = cur_node.next\n",
138 | " count += 1\n",
139 | "\n",
140 | " if cur_node is None:\n",
141 | " return \n",
142 | "\n",
143 | " prev.next = cur_node.next\n",
144 | " cur_node = None\n",
145 | "\n",
146 | " def len_iterative(self):\n",
147 | "\n",
148 | " count = 0\n",
149 | " cur_node = self.head\n",
150 | "\n",
151 | " while cur_node:\n",
152 | " count += 1\n",
153 | " cur_node = cur_node.next\n",
154 | " return count\n",
155 | "\n",
156 | " def len_recursive(self, node):\n",
157 | " if node is None:\n",
158 | " return 0\n",
159 | " return 1 + self.len_recursive(node.next)\n",
160 | "\n",
161 | " def swap_nodes(self, key_1, key_2):\n",
162 | "\n",
163 | " if key_1 == key_2:\n",
164 | " return \n",
165 | "\n",
166 | " prev_1 = None \n",
167 | " curr_1 = self.head \n",
168 | " while curr_1 and curr_1.data != key_1:\n",
169 | " prev_1 = curr_1 \n",
170 | " curr_1 = curr_1.next\n",
171 | "\n",
172 | " prev_2 = None \n",
173 | " curr_2 = self.head \n",
174 | " while curr_2 and curr_2.data != key_2:\n",
175 | " prev_2 = curr_2 \n",
176 | " curr_2 = curr_2.next\n",
177 | "\n",
178 | " if not curr_1 or not curr_2:\n",
179 | " return \n",
180 | "\n",
181 | " if prev_1:\n",
182 | " prev_1.next = curr_2\n",
183 | " else:\n",
184 | " self.head = curr_2\n",
185 | "\n",
186 | " if prev_2:\n",
187 | " prev_2.next = curr_1\n",
188 | " else:\n",
189 | " self.head = curr_1\n",
190 | "\n",
191 | " curr_1.next, curr_2.next = curr_2.next, curr_1.next\n",
192 | "\n",
193 | " def print_helper(self, node, name):\n",
194 | " if node is None:\n",
195 | " print(name + \": None\")\n",
196 | " else:\n",
197 | " print(name + \":\" + node.data)\n",
198 | "\n",
199 | " def reverse_iterative(self):\n",
200 | "\n",
201 | " prev = None \n",
202 | " cur = self.head\n",
203 | " while cur:\n",
204 | " nxt = cur.next\n",
205 | " cur.next = prev\n",
206 | " \n",
207 | " self.print_helper(prev, \"PREV\")\n",
208 | " self.print_helper(cur, \"CUR\")\n",
209 | " self.print_helper(nxt, \"NXT\")\n",
210 | " print(\"\\n\")\n",
211 | "\n",
212 | " prev = cur \n",
213 | " cur = nxt \n",
214 | " self.head = prev\n",
215 | "\n",
216 | " def reverse_recursive(self):\n",
217 | "\n",
218 | " def _reverse_recursive(cur, prev):\n",
219 | " if not cur:\n",
220 | " return prev\n",
221 | "\n",
222 | " nxt = cur.next\n",
223 | " cur.next = prev\n",
224 | " prev = cur \n",
225 | " cur = nxt \n",
226 | " return _reverse_recursive(cur, prev)\n",
227 | "\n",
228 | " self.head = _reverse_recursive(cur=self.head, prev=None)\n",
229 | "\n",
230 | " def merge_sorted(self, llist):\n",
231 | " \n",
232 | " p = self.head \n",
233 | " q = llist.head\n",
234 | " s = None\n",
235 | " \n",
236 | " if not p:\n",
237 | " return q\n",
238 | " if not q:\n",
239 | " return p\n",
240 | "\n",
241 | " if p and q:\n",
242 | " if p.data <= q.data:\n",
243 | " s = p \n",
244 | " p = s.next\n",
245 | " else:\n",
246 | " s = q\n",
247 | " q = s.next\n",
248 | " new_head = s \n",
249 | " while p and q:\n",
250 | " if p.data <= q.data:\n",
251 | " s.next = p \n",
252 | " s = p \n",
253 | " p = s.next\n",
254 | " else:\n",
255 | " s.next = q\n",
256 | " s = q\n",
257 | " q = s.next\n",
258 | " if not p:\n",
259 | " s.next = q \n",
260 | " if not q:\n",
261 | " s.next = p \n",
262 | " return new_head\n",
263 | "\n",
264 | " def remove_duplicates(self):\n",
265 | " \n",
266 | " cur = self.head\n",
267 | " prev = None\n",
268 | "\n",
269 | " dup_values = dict()\n",
270 | "\n",
271 | " while cur:\n",
272 | " if cur.data in dup_values:\n",
273 | " # Remove node:\n",
274 | " prev.next = cur.next\n",
275 | " cur = None\n",
276 | " else:\n",
277 | " # Have not encountered element before.\n",
278 | " dup_values[cur.data] = 1\n",
279 | " prev = cur\n",
280 | " cur = prev.next\n",
281 | "\n",
282 | " def print_nth_from_last(self, n, method):\n",
283 | " if method == 1:\n",
284 | " #Method 1:\n",
285 | " total_len = self.len_iterative()\n",
286 | " cur = self.head \n",
287 | " while cur:\n",
288 | " if total_len == n:\n",
289 | " #print(cur.data)\n",
290 | " return cur.data\n",
291 | " total_len -= 1\n",
292 | " cur = cur.next\n",
293 | " if cur is None:\n",
294 | " return\n",
295 | "\n",
296 | " elif method == 2:\n",
297 | " # Method 2:\n",
298 | " p = self.head\n",
299 | " q = self.head\n",
300 | "\n",
301 | " count = 0\n",
302 | " while q:\n",
303 | " count += 1\n",
304 | " if(count>=n):\n",
305 | " break\n",
306 | " q = q.next\n",
307 | " \n",
308 | " if not q:\n",
309 | " print(str(n) + \" is greater than the number of nodes in list.\")\n",
310 | " return\n",
311 | "\n",
312 | " while p and q.next:\n",
313 | " p = p.next\n",
314 | " q = q.next\n",
315 | " return p.data\n",
316 | "\n",
317 | " def rotate(self, k):\n",
318 | " if self.head and self.head.next:\n",
319 | " p = self.head \n",
320 | " q = self.head \n",
321 | " prev = None \n",
322 | " count = 0\n",
323 | " \n",
324 | " while p and count < k:\n",
325 | " prev = p\n",
326 | " p = p.next \n",
327 | " q = q.next \n",
328 | " count += 1\n",
329 | " p = prev\n",
330 | " while q:\n",
331 | " prev = q \n",
332 | " q = q.next \n",
333 | " q = prev \n",
334 | "\n",
335 | " q.next = self.head \n",
336 | " self.head = p.next \n",
337 | " p.next = None\n",
338 | "\n",
339 | " def count_occurences_iterative(self, data):\n",
340 | " count = 0\n",
341 | " cur = self.head\n",
342 | " while cur:\n",
343 | " if cur.data == data:\n",
344 | " count += 1\n",
345 | " cur = cur.next\n",
346 | " return count \n",
347 | "\n",
348 | " def count_occurences_recursive(self, node, data):\n",
349 | " if not node:\n",
350 | " return 0 \n",
351 | " if node.data == data:\n",
352 | " return 1 + self.count_occurences_recursive(node.next, data)\n",
353 | " else:\n",
354 | " return self.count_occurences_recursive(node.next, data)\n",
355 | "\n",
356 | " def is_palindrome_1(self):\n",
357 | " # Solution 1:\n",
358 | " s = \"\"\n",
359 | " p = self.head \n",
360 | " while p:\n",
361 | " s += p.data\n",
362 | " p = p.next\n",
363 | " return s == s[::-1]\n",
364 | " \n",
365 | " def is_palindrome_2(self):\n",
366 | " # Solution 2:\n",
367 | " p = self.head \n",
368 | " s = []\n",
369 | " while p:\n",
370 | " s.append(p.data)\n",
371 | " p = p.next\n",
372 | " p = self.head\n",
373 | " while p:\n",
374 | " data = s.pop()\n",
375 | " if p.data != data:\n",
376 | " return False\n",
377 | " p = p.next\n",
378 | " return True\n",
379 | " \n",
380 | " def is_palindrome_3(self):\n",
381 | " if self.head:\n",
382 | " p = self.head \n",
383 | " q = self.head \n",
384 | " prev = []\n",
385 | " \n",
386 | " i = 0\n",
387 | " while q:\n",
388 | " prev.append(q)\n",
389 | " q = q.next\n",
390 | " i += 1\n",
391 | " q = prev[i-1]\n",
392 | " \n",
393 | " count = 1\n",
394 | "\n",
395 | " while count <= i//2 + 1:\n",
396 | " if prev[-count].data != p.data:\n",
397 | " return False\n",
398 | " p = p.next\n",
399 | " count += 1\n",
400 | " return True\n",
401 | " else:\n",
402 | " return True\n",
403 | "\n",
404 | " def is_palindrome(self,method):\n",
405 | " if method == 1:\n",
406 | " return self.is_palindrome_1()\n",
407 | " elif method == 2:\n",
408 | " return self.is_palindrome_2()\n",
409 | " elif method == 3:\n",
410 | " return self.is_palindrome_3()\n",
411 | "\n",
412 | " def move_tail_to_head(self):\n",
413 | " if self.head and self.head.next:\n",
414 | " last = self.head \n",
415 | " second_to_last = None\n",
416 | " while last.next:\n",
417 | " second_to_last = last\n",
418 | " last = last.next\n",
419 | " last.next = self.head \n",
420 | " second_to_last.next = None \n",
421 | " self.head = last\n",
422 | "\n",
423 | "# A -> B -> C -> D -> Null\n",
424 | "# D -> A -> B -> C -> Null\n",
425 | "llist = LinkedList()\n",
426 | "llist.append(\"A\")\n",
427 | "llist.append(\"B\")\n",
428 | "llist.append(\"C\")\n",
429 | "llist.append(\"D\")\n",
430 | "\n",
431 | "llist.print_list()\n",
432 | "llist.move_tail_to_head()\n",
433 | "print(\"\\n\")\n",
434 | "llist.print_list()"
435 | ]
436 | }
437 | ]
438 | }
--------------------------------------------------------------------------------
/Sum_Two_Linked_Lists.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyMqLwmekfKTxjgW+aNOHw2l",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": null,
32 | "metadata": {
33 | "id": "s8qPcxna77Na"
34 | },
35 | "outputs": [],
36 | "source": [
37 | "class Node:\n",
38 | " def __init__(self, data):\n",
39 | " self.data = data\n",
40 | " self.next = None\n",
41 | "\n",
42 | "\n",
43 | "class LinkedList:\n",
44 | " def __init__(self):\n",
45 | " self.head = None\n",
46 | "\n",
47 | " def print_list(self):\n",
48 | " cur_node = self.head\n",
49 | " while cur_node:\n",
50 | " print(cur_node.data)\n",
51 | " cur_node = cur_node.next\n",
52 | "\n",
53 | " def append(self, data):\n",
54 | " new_node = Node(data)\n",
55 | "\n",
56 | " if self.head is None:\n",
57 | " self.head = new_node\n",
58 | " return\n",
59 | "\n",
60 | " last_node = self.head\n",
61 | " while last_node.next:\n",
62 | " last_node = last_node.next\n",
63 | " last_node.next = new_node\n",
64 | "\n",
65 | " def prepend(self, data):\n",
66 | " new_node = Node(data)\n",
67 | "\n",
68 | " new_node.next = self.head\n",
69 | " self.head = new_node\n",
70 | "\n",
71 | " def insert_after_node(self, prev_node, data):\n",
72 | "\n",
73 | " if not prev_node:\n",
74 | " print(\"Previous node does not exist.\")\n",
75 | " return \n",
76 | "\n",
77 | " new_node = Node(data)\n",
78 | "\n",
79 | " new_node.next = prev_node.next\n",
80 | " prev_node.next = new_node\n",
81 | "\n",
82 | " def delete_node(self, key):\n",
83 | "\n",
84 | " cur_node = self.head\n",
85 | "\n",
86 | " if cur_node and cur_node.data == key:\n",
87 | " self.head = cur_node.next\n",
88 | " cur_node = None\n",
89 | " return\n",
90 | "\n",
91 | " prev = None \n",
92 | " while cur_node and cur_node.data != key:\n",
93 | " prev = cur_node\n",
94 | " cur_node = cur_node.next\n",
95 | "\n",
96 | " if cur_node is None:\n",
97 | " return \n",
98 | "\n",
99 | " prev.next = cur_node.next\n",
100 | " cur_node = None\n",
101 | "\n",
102 | " def delete_node_at_pos(self, pos):\n",
103 | " if self.head:\n",
104 | " cur_node = self.head\n",
105 | "\n",
106 | " if pos == 0:\n",
107 | " self.head = cur_node.next\n",
108 | " cur_node = None\n",
109 | " return\n",
110 | "\n",
111 | " prev = None\n",
112 | " count = 1\n",
113 | " while cur_node and count != pos:\n",
114 | " prev = cur_node \n",
115 | " cur_node = cur_node.next\n",
116 | " count += 1\n",
117 | "\n",
118 | " if cur_node is None:\n",
119 | " return \n",
120 | "\n",
121 | " prev.next = cur_node.next\n",
122 | " cur_node = None\n",
123 | "\n",
124 | " def len_iterative(self):\n",
125 | "\n",
126 | " count = 0\n",
127 | " cur_node = self.head\n",
128 | "\n",
129 | " while cur_node:\n",
130 | " count += 1\n",
131 | " cur_node = cur_node.next\n",
132 | " return count\n",
133 | "\n",
134 | " def len_recursive(self, node):\n",
135 | " if node is None:\n",
136 | " return 0\n",
137 | " return 1 + self.len_recursive(node.next)\n",
138 | "\n",
139 | " def swap_nodes(self, key_1, key_2):\n",
140 | "\n",
141 | " if key_1 == key_2:\n",
142 | " return \n",
143 | "\n",
144 | " prev_1 = None \n",
145 | " curr_1 = self.head \n",
146 | " while curr_1 and curr_1.data != key_1:\n",
147 | " prev_1 = curr_1 \n",
148 | " curr_1 = curr_1.next\n",
149 | "\n",
150 | " prev_2 = None \n",
151 | " curr_2 = self.head \n",
152 | " while curr_2 and curr_2.data != key_2:\n",
153 | " prev_2 = curr_2 \n",
154 | " curr_2 = curr_2.next\n",
155 | "\n",
156 | " if not curr_1 or not curr_2:\n",
157 | " return \n",
158 | "\n",
159 | " if prev_1:\n",
160 | " prev_1.next = curr_2\n",
161 | " else:\n",
162 | " self.head = curr_2\n",
163 | "\n",
164 | " if prev_2:\n",
165 | " prev_2.next = curr_1\n",
166 | " else:\n",
167 | " self.head = curr_1\n",
168 | "\n",
169 | " curr_1.next, curr_2.next = curr_2.next, curr_1.next\n",
170 | "\n",
171 | " def print_helper(self, node, name):\n",
172 | " if node is None:\n",
173 | " print(name + \": None\")\n",
174 | " else:\n",
175 | " print(name + \":\" + node.data)\n",
176 | "\n",
177 | " def reverse_iterative(self):\n",
178 | "\n",
179 | " prev = None \n",
180 | " cur = self.head\n",
181 | " while cur:\n",
182 | " nxt = cur.next\n",
183 | " cur.next = prev\n",
184 | " \n",
185 | " self.print_helper(prev, \"PREV\")\n",
186 | " self.print_helper(cur, \"CUR\")\n",
187 | " self.print_helper(nxt, \"NXT\")\n",
188 | " print(\"\\n\")\n",
189 | "\n",
190 | " prev = cur \n",
191 | " cur = nxt \n",
192 | " self.head = prev\n",
193 | "\n",
194 | " def reverse_recursive(self):\n",
195 | "\n",
196 | " def _reverse_recursive(cur, prev):\n",
197 | " if not cur:\n",
198 | " return prev\n",
199 | "\n",
200 | " nxt = cur.next\n",
201 | " cur.next = prev\n",
202 | " prev = cur \n",
203 | " cur = nxt \n",
204 | " return _reverse_recursive(cur, prev)\n",
205 | "\n",
206 | " self.head = _reverse_recursive(cur=self.head, prev=None)\n",
207 | "\n",
208 | " def merge_sorted(self, llist):\n",
209 | " \n",
210 | " p = self.head \n",
211 | " q = llist.head\n",
212 | " s = None\n",
213 | " \n",
214 | " if not p:\n",
215 | " return q\n",
216 | " if not q:\n",
217 | " return p\n",
218 | "\n",
219 | " if p and q:\n",
220 | " if p.data <= q.data:\n",
221 | " s = p \n",
222 | " p = s.next\n",
223 | " else:\n",
224 | " s = q\n",
225 | " q = s.next\n",
226 | " new_head = s \n",
227 | " while p and q:\n",
228 | " if p.data <= q.data:\n",
229 | " s.next = p \n",
230 | " s = p \n",
231 | " p = s.next\n",
232 | " else:\n",
233 | " s.next = q\n",
234 | " s = q\n",
235 | " q = s.next\n",
236 | " if not p:\n",
237 | " s.next = q \n",
238 | " if not q:\n",
239 | " s.next = p \n",
240 | " return new_head\n",
241 | "\n",
242 | " def remove_duplicates(self):\n",
243 | " \n",
244 | " cur = self.head\n",
245 | " prev = None\n",
246 | "\n",
247 | " dup_values = dict()\n",
248 | "\n",
249 | " while cur:\n",
250 | " if cur.data in dup_values:\n",
251 | " # Remove node:\n",
252 | " prev.next = cur.next\n",
253 | " cur = None\n",
254 | " else:\n",
255 | " # Have not encountered element before.\n",
256 | " dup_values[cur.data] = 1\n",
257 | " prev = cur\n",
258 | " cur = prev.next\n",
259 | "\n",
260 | " def print_nth_from_last(self, n, method):\n",
261 | " if method == 1:\n",
262 | " #Method 1:\n",
263 | " total_len = self.len_iterative()\n",
264 | " cur = self.head \n",
265 | " while cur:\n",
266 | " if total_len == n:\n",
267 | " #print(cur.data)\n",
268 | " return cur.data\n",
269 | " total_len -= 1\n",
270 | " cur = cur.next\n",
271 | " if cur is None:\n",
272 | " return\n",
273 | "\n",
274 | " elif method == 2:\n",
275 | " # Method 2:\n",
276 | " p = self.head\n",
277 | " q = self.head\n",
278 | "\n",
279 | " count = 0\n",
280 | " while q:\n",
281 | " count += 1\n",
282 | " if(count>=n):\n",
283 | " break\n",
284 | " q = q.next\n",
285 | " \n",
286 | " if not q:\n",
287 | " print(str(n) + \" is greater than the number of nodes in list.\")\n",
288 | " return\n",
289 | "\n",
290 | " while p and q.next:\n",
291 | " p = p.next\n",
292 | " q = q.next\n",
293 | " return p.data\n",
294 | "\n",
295 | " def rotate(self, k):\n",
296 | " if self.head and self.head.next:\n",
297 | " p = self.head \n",
298 | " q = self.head \n",
299 | " prev = None \n",
300 | " count = 0\n",
301 | " \n",
302 | " while p and count < k:\n",
303 | " prev = p\n",
304 | " p = p.next \n",
305 | " q = q.next \n",
306 | " count += 1\n",
307 | " p = prev\n",
308 | " while q:\n",
309 | " prev = q \n",
310 | " q = q.next \n",
311 | " q = prev \n",
312 | "\n",
313 | " q.next = self.head \n",
314 | " self.head = p.next \n",
315 | " p.next = None\n",
316 | "\n",
317 | " def count_occurences_iterative(self, data):\n",
318 | " count = 0\n",
319 | " cur = self.head\n",
320 | " while cur:\n",
321 | " if cur.data == data:\n",
322 | " count += 1\n",
323 | " cur = cur.next\n",
324 | " return count \n",
325 | "\n",
326 | " def count_occurences_recursive(self, node, data):\n",
327 | " if not node:\n",
328 | " return 0 \n",
329 | " if node.data == data:\n",
330 | " return 1 + self.count_occurences_recursive(node.next, data)\n",
331 | " else:\n",
332 | " return self.count_occurences_recursive(node.next, data)\n",
333 | "\n",
334 | " def is_palindrome_1(self):\n",
335 | " # Solution 1:\n",
336 | " s = \"\"\n",
337 | " p = self.head \n",
338 | " while p:\n",
339 | " s += p.data\n",
340 | " p = p.next\n",
341 | " return s == s[::-1]\n",
342 | " \n",
343 | " def is_palindrome_2(self):\n",
344 | " # Solution 2:\n",
345 | " p = self.head \n",
346 | " s = []\n",
347 | " while p:\n",
348 | " s.append(p.data)\n",
349 | " p = p.next\n",
350 | " p = self.head\n",
351 | " while p:\n",
352 | " data = s.pop()\n",
353 | " if p.data != data:\n",
354 | " return False\n",
355 | " p = p.next\n",
356 | " return True\n",
357 | " \n",
358 | " def is_palindrome_3(self):\n",
359 | " if self.head:\n",
360 | " p = self.head \n",
361 | " q = self.head \n",
362 | " prev = []\n",
363 | " \n",
364 | " i = 0\n",
365 | " while q:\n",
366 | " prev.append(q)\n",
367 | " q = q.next\n",
368 | " i += 1\n",
369 | " q = prev[i-1]\n",
370 | " \n",
371 | " count = 1\n",
372 | "\n",
373 | " while count <= i//2 + 1:\n",
374 | " if prev[-count].data != p.data:\n",
375 | " return False\n",
376 | " p = p.next\n",
377 | " count += 1\n",
378 | " return True\n",
379 | " else:\n",
380 | " return True\n",
381 | "\n",
382 | " def is_palindrome(self,method):\n",
383 | " if method == 1:\n",
384 | " return self.is_palindrome_1()\n",
385 | " elif method == 2:\n",
386 | " return self.is_palindrome_2()\n",
387 | " elif method == 3:\n",
388 | " return self.is_palindrome_3()\n",
389 | "\n",
390 | " def move_tail_to_head(self):\n",
391 | " if self.head and self.head.next:\n",
392 | " last = self.head \n",
393 | " second_to_last = None\n",
394 | " while last.next:\n",
395 | " second_to_last = last\n",
396 | " last = last.next\n",
397 | " last.next = self.head \n",
398 | " second_to_last.next = None \n",
399 | " self.head = last\n",
400 | " \n",
401 | " def sum_two_lists(self, llist):\n",
402 | " p = self.head \n",
403 | " q = llist.head\n",
404 | "\n",
405 | " sum_llist = LinkedList()\n",
406 | "\n",
407 | " carry = 0\n",
408 | " while p or q:\n",
409 | " if not p:\n",
410 | " i = 0\n",
411 | " else:\n",
412 | " i = p.data\n",
413 | " if not q:\n",
414 | " j = 0 \n",
415 | " else:\n",
416 | " j = q.data\n",
417 | " s = i + j + carry\n",
418 | " if s >= 10:\n",
419 | " carry = 1\n",
420 | " remainder = s % 10\n",
421 | " sum_llist.append(remainder)\n",
422 | " else:\n",
423 | " carry = 0\n",
424 | " sum_llist.append(s)\n",
425 | " if p:\n",
426 | " p = p.next\n",
427 | " if q:\n",
428 | " q = q.next\n",
429 | " sum_llist.print_list()\n",
430 | "\n",
431 | "\n",
432 | "# 3 6 5 \n",
433 | "# 4 2 \n",
434 | "# ------\n",
435 | "# \n",
436 | "llist1 = LinkedList()\n",
437 | "llist1.append(5)\n",
438 | "llist1.append(6)\n",
439 | "llist1.append(3)\n",
440 | "\n",
441 | "llist2 = LinkedList()\n",
442 | "llist2.append(8)\n",
443 | "llist2.append(4)\n",
444 | "llist2.append(2)\n",
445 | "\n",
446 | "print(365 + 248)\n",
447 | "llist1.sum_two_lists(llist2)"
448 | ]
449 | }
450 | ]
451 | }
--------------------------------------------------------------------------------
/Introduction_&_Implementation_of_List_,Tuple_&_Dictionary_in_Python_.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "provenance": [],
7 | "authorship_tag": "ABX9TyNgWIyqJO5cqH41GqkyulSR",
8 | "include_colab_link": true
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | },
14 | "language_info": {
15 | "name": "python"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "source": [
32 | "**List**
\n",
33 | "Lists are used to store multiple items in a single variable.
\n",
34 | "Declaring a List is fairly straightforward. You use square brackets ([]) and separate the items by a comma.
Let me write an example -
\n",
35 | "A = [\"Meat\", \"Ali\", 48851964400, 3.14, \"Bread\"]
\n",
36 | "Lists are mutable. Say if you want to change some item on a List, you can do that. For example, if I don’t like 'Meat', and want to replace this with 'Morning Walk', I can do it –
\n",
37 | "A = [\"Morning Walk\", \"Leuven\", 48851964400, 3.14, \"Mom\"]
\n",
38 | "Some essential features of Python lists are:
\n",
39 | "* Collection of values\n",
40 | "* Can be of any data type\n",
41 | "* Can be a combination of different types"
42 | ],
43 | "metadata": {
44 | "id": "I20HL3xxn9OE"
45 | }
46 | },
47 | {
48 | "cell_type": "code",
49 | "source": [
50 | "fruit_list = [\"Apple\",\"Berry\",\"Banana\",\"Pine Apple\"]\n",
51 | "print(fruit_list)"
52 | ],
53 | "metadata": {
54 | "id": "MwdKTcPAqy6T",
55 | "colab": {
56 | "base_uri": "https://localhost:8080/"
57 | },
58 | "outputId": "5db84250-eecc-44ee-8c10-280bb8be6226"
59 | },
60 | "execution_count": null,
61 | "outputs": [
62 | {
63 | "output_type": "stream",
64 | "name": "stdout",
65 | "text": [
66 | "['Apple', 'Berry', 'Banana', 'Pine Apple']\n"
67 | ]
68 | }
69 | ]
70 | },
71 | {
72 | "cell_type": "markdown",
73 | "source": [
74 | "**List Method**\n",
75 | "\n",
76 | "\n"
77 | ],
78 | "metadata": {
79 | "id": "2N834ZhgvDDF"
80 | }
81 | },
82 | {
83 | "cell_type": "code",
84 | "source": [
85 | "#.append()\n",
86 | "#In Python, you can add values to the end of a list using the .append() method.\n",
87 | "\n",
88 | "fruit_list = [\"Apple\",\"Berry\",\"Banana\",\"Pine Apple\",\"Banana\"]\n",
89 | "print(fruit_list)\n",
90 | "fruit_list.append(\"Guava\")\n",
91 | "print(fruit_list)\n",
92 | "\n",
93 | "#.count()\n",
94 | "#The .count() Python list method searches a list for whatever search term it receives as an argument, then returns the number of matching entries found.\n",
95 | "\n",
96 | "fruit=fruit_list.count(\"Banana\")\n",
97 | "print(fruit)\n",
98 | "\n",
99 | "#len()\n",
100 | "#The Python len() function can be used to determine the number of items found in the list it accepts as an argument.\n",
101 | "\n",
102 | "print(len(fruit_list))\n",
103 | "\n",
104 | "#.sort()\n",
105 | "#The .sort() Python list method will sort the contents of whatever list it is called on. Numerical lists will be sorted in ascending order, and lists of Strings will be sorted into alphabetical order. It modifies the original list, and has no return value.\n",
106 | "\n",
107 | "fruit_list.sort()\n",
108 | "print(fruit_list)\n",
109 | "\n",
110 | "# .index\n",
111 | "print(fruit_list.index(\"Guava\"))\n",
112 | "\n",
113 | "# Create new List\n",
114 | "\n",
115 | "l= [\"Kiwi\",\"Raspberry\",\"Orange\"]\n",
116 | "print(l)\n",
117 | "\n",
118 | "# Concatination of two list\n",
119 | "new_list = fruit_list + l\n",
120 | "print(new_list)\n",
121 | "\n",
122 | "del l\n",
123 | "print(l)\n"
124 | ],
125 | "metadata": {
126 | "id": "K-CIzbDzph1L",
127 | "colab": {
128 | "base_uri": "https://localhost:8080/",
129 | "height": 339
130 | },
131 | "outputId": "54198847-a921-431a-8b36-9794b10ea858"
132 | },
133 | "execution_count": null,
134 | "outputs": [
135 | {
136 | "output_type": "stream",
137 | "name": "stdout",
138 | "text": [
139 | "['Apple', 'Berry', 'Banana', 'Pine Apple', 'Banana']\n",
140 | "['Apple', 'Berry', 'Banana', 'Pine Apple', 'Banana', 'Guava']\n",
141 | "2\n",
142 | "6\n",
143 | "['Apple', 'Banana', 'Banana', 'Berry', 'Guava', 'Pine Apple']\n",
144 | "4\n",
145 | "['Kiwi', 'Raspberry', 'Orange']\n",
146 | "['Apple', 'Banana', 'Banana', 'Berry', 'Guava', 'Pine Apple', 'Kiwi', 'Raspberry', 'Orange']\n"
147 | ]
148 | },
149 | {
150 | "output_type": "error",
151 | "ename": "NameError",
152 | "evalue": "ignored",
153 | "traceback": [
154 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
155 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
156 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;32mdel\u001b[0m \u001b[0ml\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
157 | "\u001b[0;31mNameError\u001b[0m: name 'l' is not defined"
158 | ]
159 | }
160 | ]
161 | },
162 | {
163 | "cell_type": "markdown",
164 | "source": [
165 | "**TUPLE** \n",
166 | "\n",
167 | "* Tuple is also an ordered sequence of items as List. Tuple also holds multiple data types.\n",
168 | "\n",
169 | "* The only difference in Tuple & List is that Tuple is immutable; once created it cannot be changed.\n",
170 | "\n",
171 | "* Creating a tuple is as simple as putting different comma-separated values within round brackets.
\n",
172 | "\n",
173 | "Example:
\n",
174 | "A = ('Bread', 'Ahsan', 48851964400, 3.14, 'Egg')"
175 | ],
176 | "metadata": {
177 | "id": "Nm6IP19N2QQW"
178 | }
179 | },
180 | {
181 | "cell_type": "code",
182 | "source": [
183 | "t = (4,5,3,4,5,2,7,2165, 58, 45)\n",
184 | "print(len(t))"
185 | ],
186 | "metadata": {
187 | "colab": {
188 | "base_uri": "https://localhost:8080/"
189 | },
190 | "id": "DfP5c5iT2SCu",
191 | "outputId": "9f311f67-5707-4b00-d530-4bc7d97f30dd"
192 | },
193 | "execution_count": null,
194 | "outputs": [
195 | {
196 | "output_type": "stream",
197 | "name": "stdout",
198 | "text": [
199 | "10\n"
200 | ]
201 | }
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "source": [],
207 | "metadata": {
208 | "id": "qE8PIsTBFIUZ"
209 | },
210 | "execution_count": null,
211 | "outputs": []
212 | },
213 | {
214 | "cell_type": "code",
215 | "source": [
216 | "# finding Element in a tuple\n",
217 | "print(t[0])\n",
218 | "print(t[7])\n",
219 | "print(t[110]) # Error here"
220 | ],
221 | "metadata": {
222 | "colab": {
223 | "base_uri": "https://localhost:8080/",
224 | "height": 235
225 | },
226 | "id": "K9MRhYT4Edd4",
227 | "outputId": "8cca0932-ac50-47ce-c86d-0d9b2aa10cb5"
228 | },
229 | "execution_count": null,
230 | "outputs": [
231 | {
232 | "output_type": "stream",
233 | "name": "stdout",
234 | "text": [
235 | "4\n",
236 | "2165\n"
237 | ]
238 | },
239 | {
240 | "output_type": "error",
241 | "ename": "IndexError",
242 | "evalue": "ignored",
243 | "traceback": [
244 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
245 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
246 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\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[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m7\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----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m110\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",
247 | "\u001b[0;31mIndexError\u001b[0m: tuple index out of range"
248 | ]
249 | }
250 | ]
251 | },
252 | {
253 | "cell_type": "code",
254 | "source": [
255 | "# Assigning New Element in a tuple\n",
256 | "\n",
257 | "t[10]= 240 # Due to Imutable feature tuple doesn't support assignment\n",
258 | "print(t)\n"
259 | ],
260 | "metadata": {
261 | "colab": {
262 | "base_uri": "https://localhost:8080/",
263 | "height": 217
264 | },
265 | "id": "saJDj-5KFIWY",
266 | "outputId": "bb802abc-e377-45fe-ef3e-46154c7c1af3"
267 | },
268 | "execution_count": null,
269 | "outputs": [
270 | {
271 | "output_type": "error",
272 | "ename": "TypeError",
273 | "evalue": "ignored",
274 | "traceback": [
275 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
276 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
277 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Assigning New Element in a tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m=\u001b[0m \u001b[0;36m240\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mt\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
278 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
279 | ]
280 | }
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "source": [
286 | "# functions with tuples\n",
287 | "print(t.count(9))\n",
288 | "print(t.count(5))\n",
289 | "print(t.index(5))\n",
290 | "print(sorted(t))\n",
291 | "print(len(t))\n",
292 | "print(sum(t))\n",
293 | "print(max(t))\n",
294 | "print(min(t))\n",
295 | "\n",
296 | "\n",
297 | "# Tuples conatining Elements\n",
298 | "\n",
299 | "tuple_1 = (1,\"two\",2.5)\n",
300 | "print(tuple_1)\n",
301 | "\n",
302 | "\n",
303 | "# Concatination of tuple\n",
304 | "new_tuple = t + tuple_1\n",
305 | "print(new_tuple)\n",
306 | "\n",
307 | "# Delete tuple\n",
308 | "del(new_tuple)\n",
309 | "print(new_tuple)"
310 | ],
311 | "metadata": {
312 | "colab": {
313 | "base_uri": "https://localhost:8080/",
314 | "height": 373
315 | },
316 | "id": "1KzconSPFncK",
317 | "outputId": "903a459a-aec4-477b-c38c-23a0962ff2dc"
318 | },
319 | "execution_count": null,
320 | "outputs": [
321 | {
322 | "output_type": "stream",
323 | "name": "stdout",
324 | "text": [
325 | "0\n",
326 | "2\n",
327 | "1\n",
328 | "[2, 3, 4, 4, 5, 5, 7, 45, 58, 2165]\n",
329 | "10\n",
330 | "2298\n",
331 | "2165\n",
332 | "2\n",
333 | "(1, 'two', 2.5)\n",
334 | "(4, 5, 3, 4, 5, 2, 7, 2165, 58, 45, 1, 'two', 2.5)\n"
335 | ]
336 | },
337 | {
338 | "output_type": "error",
339 | "ename": "NameError",
340 | "evalue": "ignored",
341 | "traceback": [
342 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
343 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
344 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;31m# Delete tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;32mdel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnew_tuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 24\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnew_tuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
345 | "\u001b[0;31mNameError\u001b[0m: name 'new_tuple' is not defined"
346 | ]
347 | }
348 | ]
349 | },
350 | {
351 | "cell_type": "code",
352 | "source": [
353 | "a=('hi,') * 6\n",
354 | "print(a)"
355 | ],
356 | "metadata": {
357 | "colab": {
358 | "base_uri": "https://localhost:8080/"
359 | },
360 | "id": "v-Dujk8CJudS",
361 | "outputId": "b0bff2f5-3f49-41a4-f2f8-e80f1b59438c"
362 | },
363 | "execution_count": null,
364 | "outputs": [
365 | {
366 | "output_type": "stream",
367 | "name": "stdout",
368 | "text": [
369 | "hi,hi,hi,hi,hi,hi,\n"
370 | ]
371 | }
372 | ]
373 | },
374 | {
375 | "cell_type": "markdown",
376 | "source": [
377 | "**Dictionary**
\n",
378 | "* Dictionary is an unordered collection of key-value pairs.\n",
379 | "\n",
380 | "* Real word dictionaries are a good analogy to understand them: they contain a list of items(words), and each item has a key(the word) and a value(the word’s meaning).\n",
381 | "\n",
382 | "* It generally is used when we have a huge amount of data.\n",
383 | "\n",
384 | "* It is defined within braces, with each item being in the form of key: value pair. Syntax –
\n",
385 | "my_dict = {\n",
386 | "\"key1\":\"value1\",\n",
387 | "\"key2\":\"value2\",\n",
388 | "}\n",
389 | "
\n",
390 | "* The keys in a dictionary must always be unique and immutable. This is the reason dictionary keys can be String but not List.\n",
391 | "\n",
392 | "* On the other hand, Values in a dictionary can be of any datatype and can be duplicated.\n",
393 | "\n",
394 | "* Dictionary keys are case sensitive, same name but different cases of Key will be treated distinctly\n"
395 | ],
396 | "metadata": {
397 | "id": "PZG1qwSUN_yB"
398 | }
399 | },
400 | {
401 | "cell_type": "code",
402 | "source": [
403 | "my_dic = {1:\"Blue\",2:\"Green\",3:\"Red\"}\n",
404 | "print(my_dic)\n",
405 | "\n",
406 | "# Creating a Dictionary with Mixed keys\n",
407 | "Dict = {'Name': 'Muhammad', 1: [1, 2, 3, 4]}\n",
408 | "print(\"\\nDictionary with the use of Mixed Keys: \")\n",
409 | "print(Dict)\n",
410 | "\n",
411 | "\n",
412 | "# Adding elements one at a time\n",
413 | "Dict[0] = 'Python'\n",
414 | "Dict[2] = 'Skills'\n",
415 | "Dict[3] = 1\n",
416 | "print(\"\\nDictionary after adding 3 elements: \")\n",
417 | "print(Dict)\n",
418 | "\n",
419 | "# Adding set of values to a single Key\n",
420 | "Dict['Tuple'] = 2, 3, 4\n",
421 | "print(\"\\nDictionary after adding 3 elements: \")\n",
422 | "print(Dict)\n",
423 | "\n",
424 | "# Updating existing Key's Value\n",
425 | "Dict[2] = 'Grand Welcome'\n",
426 | "print(\"\\nUpdated key value: \")\n",
427 | "print(Dict)\n",
428 | "\n",
429 | "# Adding Nested Key value to Dictionary\n",
430 | "Dict[5] = {'Nested': {'1': 'Life', '2': 'Geeks'}}\n",
431 | "print(\"\\nAdding a Nested Key: \")\n",
432 | "print(Dict)"
433 | ],
434 | "metadata": {
435 | "colab": {
436 | "base_uri": "https://localhost:8080/",
437 | "height": 550
438 | },
439 | "id": "DzrQ0hfTOy9b",
440 | "outputId": "7f7cdb04-0243-41a2-9e54-8de4e2e3ea29"
441 | },
442 | "execution_count": null,
443 | "outputs": [
444 | {
445 | "output_type": "stream",
446 | "name": "stdout",
447 | "text": [
448 | "{1: 'Blue', 2: 'Green', 3: 'Red'}\n",
449 | "\n",
450 | "Dictionary with the use of Mixed Keys: \n",
451 | "{'Name': 'Muhammad', 1: [1, 2, 3, 4]}\n",
452 | "\n",
453 | "Dictionary after adding 3 elements: \n",
454 | "{'Name': 'Muhammad', 1: [1, 2, 3, 4], 0: 'Python', 2: 'Skills', 3: 1}\n",
455 | "\n",
456 | "Dictionary after adding 3 elements: \n",
457 | "{'Name': 'Muhammad', 1: [1, 2, 3, 4], 0: 'Python', 2: 'Skills', 3: 1, 'Tuple': (2, 3, 4)}\n",
458 | "\n",
459 | "Updated key value: \n",
460 | "{'Name': 'Muhammad', 1: [1, 2, 3, 4], 0: 'Python', 2: 'Grand Welcome', 3: 1, 'Tuple': (2, 3, 4)}\n",
461 | "\n",
462 | "Adding a Nested Key: \n",
463 | "{'Name': 'Muhammad', 1: [1, 2, 3, 4], 0: 'Python', 2: 'Grand Welcome', 3: 1, 'Tuple': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'Geeks'}}}\n",
464 | "Accessing a element using key:\n"
465 | ]
466 | },
467 | {
468 | "output_type": "error",
469 | "ename": "KeyError",
470 | "evalue": "ignored",
471 | "traceback": [
472 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
473 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
474 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0;31m# accessing a element using key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Accessing a element using key:\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mDict\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Data'\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[1;32m 36\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0;31m# accessing a element using key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
475 | "\u001b[0;31mKeyError\u001b[0m: 'Data'"
476 | ]
477 | }
478 | ]
479 | },
480 | {
481 | "cell_type": "code",
482 | "source": [
483 | "# Create New Dictionary\n",
484 | "Dict = {1: 'Python for', 'Data': 'Science', 3: 'Learning'}\n",
485 | "\n",
486 | "# accessing a element using key\n",
487 | "print(\"Accessing a element using key:\")\n",
488 | "print(Dict['Data'])\n",
489 | "\n",
490 | "# accessing a element using key\n",
491 | "print(\"Accessing a element using key:\")\n",
492 | "print(Dict[1])\n"
493 | ],
494 | "metadata": {
495 | "colab": {
496 | "base_uri": "https://localhost:8080/"
497 | },
498 | "id": "oXQ2E11SRaLK",
499 | "outputId": "f419f467-8e8c-40c0-c2f7-886f8499183e"
500 | },
501 | "execution_count": null,
502 | "outputs": [
503 | {
504 | "output_type": "stream",
505 | "name": "stdout",
506 | "text": [
507 | "Accessing a element using key:\n",
508 | "Science\n",
509 | "Accessing a element using key:\n",
510 | "Python for\n"
511 | ]
512 | }
513 | ]
514 | },
515 | {
516 | "cell_type": "code",
517 | "source": [
518 | "# A collection of popular Dictionary methods that we use in our daily lives\n",
519 | "dictionary = {1: \"Python\", 2: \"C++\", 3: \"Ruby\", 4: \"R\"}\n",
520 | "\n",
521 | "# copy() method\n",
522 | "dict2 = dictionary.copy()\n",
523 | "print(dict2)\n",
524 | "\n",
525 | "# clear() method\n",
526 | "dictionary.clear()\n",
527 | "print(dictionary)\n",
528 | "\n",
529 | "# get() method\n",
530 | "print(dict2.get(3))\n",
531 | "\n",
532 | "# items() method\n",
533 | "print(dict2.items())\n",
534 | "\n",
535 | "# keys() method\n",
536 | "print(dict2.keys())\n",
537 | "\n",
538 | "# pop() method\n",
539 | "dict2.pop(2)\n",
540 | "print(dict2)\n",
541 | "\n",
542 | "# popitem() method\n",
543 | "dict2.popitem()\n",
544 | "print(dict2)\n",
545 | "\n",
546 | "# update() method\n",
547 | "dict2.update({4: \"Kotlin\"})\n",
548 | "print(dict2)\n",
549 | "\n",
550 | "# values() method\n",
551 | "print(dict2.values())"
552 | ],
553 | "metadata": {
554 | "colab": {
555 | "base_uri": "https://localhost:8080/"
556 | },
557 | "id": "R70RYuK_RuND",
558 | "outputId": "af0439af-42a6-4ea0-dad8-bdd99cab3332"
559 | },
560 | "execution_count": 43,
561 | "outputs": [
562 | {
563 | "output_type": "stream",
564 | "name": "stdout",
565 | "text": [
566 | "{1: 'Python', 2: 'C++', 3: 'Ruby', 4: 'R'}\n",
567 | "{}\n",
568 | "Ruby\n",
569 | "dict_items([(1, 'Python'), (2, 'C++'), (3, 'Ruby'), (4, 'R')])\n",
570 | "dict_keys([1, 2, 3, 4])\n",
571 | "{1: 'Python', 3: 'Ruby', 4: 'R'}\n",
572 | "{1: 'Python', 3: 'Ruby'}\n",
573 | "{1: 'Python', 3: 'Ruby', 4: 'Kotlin'}\n",
574 | "dict_values(['Python', 'Ruby', 'Kotlin'])\n",
575 | "[2, 8, 7]\n"
576 | ]
577 | }
578 | ]
579 | }
580 | ]
581 | }
582 |
--------------------------------------------------------------------------------