From 3dd586e63f3fcca40202e94bbae02e1cf3855aab Mon Sep 17 00:00:00 2001 From: Dhayananth B Date: Fri, 11 Oct 2024 16:41:41 +0530 Subject: [PATCH 1/2] added python structure --- .../01 - Arrays/Easy/Maximum_in_Array.ipynb | 76 +++++++++++++++++++ Python/README.md | 76 +++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 Python/02 - Data_Structures/01 - Arrays/Easy/Maximum_in_Array.ipynb create mode 100644 Python/README.md diff --git a/Python/02 - Data_Structures/01 - Arrays/Easy/Maximum_in_Array.ipynb b/Python/02 - Data_Structures/01 - Arrays/Easy/Maximum_in_Array.ipynb new file mode 100644 index 0000000..ec667cd --- /dev/null +++ b/Python/02 - Data_Structures/01 - Arrays/Easy/Maximum_in_Array.ipynb @@ -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 +} diff --git a/Python/README.md b/Python/README.md new file mode 100644 index 0000000..315ed4a --- /dev/null +++ b/Python/README.md @@ -0,0 +1,76 @@ +# Mastering Data Structures and Algorithms in Python + +Python has rapidly become one of the most popular programming languages, with over 10 million developers utilizing its versatility and simplicity across a wide range of applications. In addition to its growing popularity, Python offers lucrative career opportunities, with an average base pay reaching $110,000 per year. Known for its readability, flexibility, and vast libraries, Python finds use across multiple domains, including web development, artificial intelligence, machine learning, scientific computing, and beyond. + +## Unlocking the Power of Data Structures and Algorithms (DSA) in Python + +Data Structures and Algorithms (DSA) are foundational to programming, enabling developers to solve complex problems efficiently. This article will guide you through mastering DSA using Python, one of the most beginner-friendly yet powerful programming languages in the world. + +### Introduction to Python DSA + +Python is widely regarded for its clean syntax and rapid prototyping capabilities, making it an excellent language for solving computational problems using Data Structures and Algorithms. Its rich ecosystem of libraries like NumPy, Pandas, and collections offer optimized implementations of fundamental data structures and algorithms, simplifying development tasks. + +### Understanding Data Structures and Algorithms + +**Data Structures**: Data structures are fundamental to organizing, storing, and retrieving data efficiently. Python supports various built-in data structures like lists, tuples, sets, and dictionaries, each suited for specific data management needs. + +**Algorithms**: Algorithms provide systematic steps to solve computational problems. In Python, you can implement a wide range of algorithms for tasks like searching, sorting, and optimizing, with simple and readable code. + +#### Data Structures Concepts: +• **Lists in Python** +• **Dictionaries** +• **Collections** + +### Importance of Data Structures and Algorithms + +Data Structures and Algorithms are essential for efficient code, enhancing performance by reducing time complexity and optimizing resource utilization. Efficient DSA usage allows Python developers to solve real-world problems with solutions that scale, such as searching through large datasets using optimized algorithms like Binary Search, or handling data collisions in Hash Tables. + +### Core Data Structures in Python + +**Lists**: Lists in Python are mutable and can hold elements of different data types. They offer the flexibility to implement sorting and searching algorithms efficiently. + +**Dictionaries**: Similar to Hash Tables, dictionaries map keys to values, providing an efficient way to store and retrieve data with average O(1) time complexity. + +**Tuples and Sets**: While tuples are immutable sequences, sets are unordered collections of unique elements. Both data structures can be useful in specific scenarios, like ensuring data uniqueness or handling immutable objects. + +**Stacks and Queues**: Stacks operate on the Last In, First Out (LIFO) principle, while queues follow the First In, First Out (FIFO) rule. Python’s `collections.deque` is an efficient way to implement these data structures. + +**Trees and Graphs**: Although Python doesn’t have a built-in Tree or Graph data structure, libraries like `networkx` make it easy to implement them and solve complex problems in hierarchical or networked data. + +### Common Algorithms in Python + +**Sorting Algorithms**: Python’s built-in `sort()` method uses Timsort, an adaptive sorting algorithm with O(n log n) complexity. Other popular algorithms like Quick Sort, Merge Sort, and Bubble Sort can also be easily implemented in Python. + +**Searching Algorithms**: From Linear Search to Binary Search, Python allows you to perform efficient element searches within datasets using a few lines of code. + +**Graph Algorithms**: Python libraries such as `networkx` facilitate the implementation of BFS, DFS, and Shortest-Path algorithms like Dijkstra’s Algorithm to traverse and manipulate graph data efficiently. + +**Dynamic Programming**: Python’s flexibility makes it an ideal choice for implementing dynamic programming algorithms like the Knapsack Problem, Longest Common Subsequence, and Fibonacci sequence optimizations. + +### Applications of Python DSA + +The practical applications of Python DSA extend across numerous industries: + +- **Web Development**: Python’s data structures and algorithms play a critical role in backend development, enabling efficient data handling in web applications. + +- **Artificial Intelligence**: In AI and machine learning, Python is the go-to language. DSA is pivotal in optimizing models, handling large datasets, and enhancing performance. + +- **Finance**: Python DSA is used for algorithmic trading, risk management, and complex financial modeling, delivering efficient and scalable solutions. + +- **Data Science**: Python's powerful data structures and optimized algorithms are essential for data manipulation, statistical analysis, and machine learning tasks in data science. + +### Conclusion: Empowering Solutions with Python DSA + +In conclusion, mastering Data Structures and Algorithms in Python is crucial for any developer aiming to tackle real-world challenges effectively. Python’s simplicity, combined with its powerful libraries, provides a seamless way to implement and optimize DSA, making it the language of choice for industries ranging from finance to AI. Whether you are a beginner or an experienced developer, Python DSA equips you with the tools to solve problems efficiently and optimize your applications. + +
+ +--- + + *Authored by Dhayananth B* + +

+ Copyright © 2023 - 2024 SERVER-X-101 All rights reserved.
+

+ +
From 2d9c1988ddb39baae326ba55f532e674ace47724 Mon Sep 17 00:00:00 2001 From: Dhayananth B Date: Fri, 11 Oct 2024 16:51:51 +0530 Subject: [PATCH 2/2] added stack problem --- .../Medium/Remove_duplicates.ipynb | 81 ++++++++++++++ .../02 - Stack/Easy/Stack_operations.ipynb | 100 ++++++++++++++++++ .../Medium/Reverse_string_using_stack.ipynb | 75 +++++++++++++ 3 files changed, 256 insertions(+) create mode 100644 Python/02 - Data_Structures/01 - Arrays/Medium/Remove_duplicates.ipynb create mode 100644 Python/02 - Data_Structures/02 - Stack/Easy/Stack_operations.ipynb create mode 100644 Python/02 - Data_Structures/02 - Stack/Medium/Reverse_string_using_stack.ipynb 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 +}