Skip to content

Commit

Permalink
added stack problem
Browse files Browse the repository at this point in the history
  • Loading branch information
DhayananthB committed Oct 11, 2024
1 parent 3dd586e commit 2d9c198
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
100 changes: 100 additions & 0 deletions Python/02 - Data_Structures/02 - Stack/Easy/Stack_operations.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2d9c198

Please sign in to comment.