-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from DhayananthB/python_dsa
created a folder for python language and added few questions in array(list) and stack.
- Loading branch information
Showing
5 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
Python/02 - Data_Structures/01 - Arrays/Easy/Maximum_in_Array.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Find the Maximum and Minimum in an Array\n", | ||
"\n", | ||
"**Problem:** *Given an array, write a Python function to find the maximum and minimum elements.*\n", | ||
"\n", | ||
"**Example:**\n", | ||
"\n", | ||
"arr = [3, 5, 7, 2, 8]\n", | ||
"\n", | ||
"**Output:** Max = 8, Min = 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Max = 8,Min = 2\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def find_max_min(arr):\n", | ||
" if len(arr) == 0: # Check for an empty array\n", | ||
" return None, None\n", | ||
"\n", | ||
" max_val = arr[0] # Initialize max and min with the first element\n", | ||
" min_val = arr[0]\n", | ||
"\n", | ||
" for num in arr:\n", | ||
" if num > max_val:\n", | ||
" max_val = num\n", | ||
" if num < min_val:\n", | ||
" min_val = num\n", | ||
"\n", | ||
" return max_val, min_val\n", | ||
"\n", | ||
"# Example\n", | ||
"arr = [3, 5, 7, 2, 8]\n", | ||
"Max , Min = find_max_min(arr)\n", | ||
"\n", | ||
"print(f\"Max = {Max},Min = {Min}\")\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 | ||
} |
81 changes: 81 additions & 0 deletions
81
Python/02 - Data_Structures/01 - Arrays/Medium/Remove_duplicates.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
100
Python/02 - Data_Structures/02 - Stack/Easy/Stack_operations.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
75 changes: 75 additions & 0 deletions
75
Python/02 - Data_Structures/02 - Stack/Medium/Reverse_string_using_stack.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.