diff --git a/Python/02 - Data_Structures/01 - Arrays/Medium/Remove_duplicates.ipynb b/Python/02 - Data_Structures/01 - Arrays/Medium/Remove_duplicates.ipynb new file mode 100644 index 0000000..d7d1c47 --- /dev/null +++ b/Python/02 - Data_Structures/01 - Arrays/Medium/Remove_duplicates.ipynb @@ -0,0 +1,81 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Problem: Remove Duplicates from Array\n", + "\n", + "- **Problem**: Given an array, write a function to remove duplicates without using extra space.\n", + "- **Example**:\n", + " ```python\n", + " arr = [1, 2, 2, 3, 4, 4, 5]\n", + " # Output: [1, 2, 3, 4, 5]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ] + } + ], + "source": [ + "def remove_duplicates(arr):\n", + " if len(arr) == 0:\n", + " return []\n", + " \n", + " # Pointer for the position of the unique element\n", + " unique_index = 0\n", + "\n", + " # Iterate through the array, starting from the second element\n", + " for i in range(1, len(arr)):\n", + " # If the current element is not equal to the element at unique_index\n", + " if arr[i] != arr[unique_index]:\n", + " unique_index += 1 # Move the unique_index\n", + " arr[unique_index] = arr[i] # Place the non-duplicate element at unique_index\n", + " \n", + " # Return the portion of the array with unique elements\n", + " return arr[:unique_index + 1]\n", + "\n", + "# Example\n", + "arr = [1, 2, 2, 3, 4, 4, 5]\n", + "print(remove_duplicates(arr)) # Output: [1, 2, 3, 4, 5]\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Python/02 - Data_Structures/02 - Stack/Easy/Stack_operations.ipynb b/Python/02 - Data_Structures/02 - Stack/Easy/Stack_operations.ipynb new file mode 100644 index 0000000..a15755f --- /dev/null +++ b/Python/02 - Data_Structures/02 - Stack/Easy/Stack_operations.ipynb @@ -0,0 +1,100 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Problem: Basic Stack Operations\n", + "\n", + "- **Problem**: Implement a stack with basic operations such as `push`, `pop`, `peek`, and `is_empty`. Stacks follow a Last In, First Out (LIFO) structure.\n", + "- **Example**:\n", + " ```python\n", + " stack = Stack()\n", + " stack.push(10) # Stack: [10]\n", + " stack.push(20) # Stack: [10, 20]\n", + " print(stack.pop()) # Output: 20, Stack: [10]\n", + " print(stack.peek()) # Output: 10, Stack: [10]\n", + " print(stack.is_empty()) # Output: False\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20\n", + "10\n", + "False\n", + "[10]\n" + ] + } + ], + "source": [ + "class Stack:\n", + " def __init__(self):\n", + " # Initialize an empty list to hold stack elements\n", + " self.stack = []\n", + " \n", + " def push(self, value):\n", + " \"\"\"Push an element onto the stack.\"\"\"\n", + " self.stack.append(value)\n", + " \n", + " def pop(self):\n", + " \"\"\"Remove and return the top element of the stack.\"\"\"\n", + " if not self.is_empty():\n", + " return self.stack.pop()\n", + " else:\n", + " return \"Stack is empty\"\n", + " \n", + " def peek(self):\n", + " \"\"\"Return the top element without removing it.\"\"\"\n", + " if not self.is_empty():\n", + " return self.stack[-1]\n", + " else:\n", + " return \"Stack is empty\"\n", + " \n", + " def is_empty(self):\n", + " \"\"\"Check if the stack is empty.\"\"\"\n", + " return len(self.stack) == 0\n", + " \n", + " def display(self):\n", + " \"\"\"Display the stack.\"\"\"\n", + " return self.stack\n", + "\n", + "# Example\n", + "stack = Stack()\n", + "stack.push(10) # Stack: [10]\n", + "stack.push(20) # Stack: [10, 20]\n", + "print(stack.pop()) # Output: 20, Stack: [10]\n", + "print(stack.peek()) # Output: 10, Stack: [10]\n", + "print(stack.is_empty()) # Output: False\n", + "print(stack.display()) # Output: [10]\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Python/02 - Data_Structures/02 - Stack/Medium/Reverse_string_using_stack.ipynb b/Python/02 - Data_Structures/02 - Stack/Medium/Reverse_string_using_stack.ipynb new file mode 100644 index 0000000..1d7ec92 --- /dev/null +++ b/Python/02 - Data_Structures/02 - Stack/Medium/Reverse_string_using_stack.ipynb @@ -0,0 +1,75 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Problem: Reverse a String Using Stack\n", + "\n", + "- **Problem**: Given a string, write a function to reverse the string using a stack.\n", + " \n", + "- **Example**:\n", + " ```python\n", + " input_str = \"hello\"\n", + " # Output: \"olleh\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "olleh\n" + ] + } + ], + "source": [ + "def reverse_string(s):\n", + " # Initialize an empty stack\n", + " stack = []\n", + " \n", + " # Push each character of the string onto the stack\n", + " for char in s:\n", + " stack.append(char)\n", + " \n", + " # Initialize an empty result string\n", + " reversed_str = ''\n", + " \n", + " # Pop characters from the stack and append them to the result string\n", + " while stack:\n", + " reversed_str += stack.pop()\n", + " \n", + " return reversed_str\n", + "\n", + "# Example\n", + "input_str = \"hello\"\n", + "print(reverse_string(input_str)) # Output: \"olleh\"\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}