From cb5fcb4953a9b0a181479fc4b434c64123280497 Mon Sep 17 00:00:00 2001 From: somyasaxena01 <140182178+somyasaxena01@users.noreply.github.com> Date: Thu, 30 May 2024 10:56:55 +0530 Subject: [PATCH 1/4] Added stacks.md --- contrib/ds-algorithms/stacks.md | 131 ++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 contrib/ds-algorithms/stacks.md diff --git a/contrib/ds-algorithms/stacks.md b/contrib/ds-algorithms/stacks.md new file mode 100644 index 00000000..f9f662b2 --- /dev/null +++ b/contrib/ds-algorithms/stacks.md @@ -0,0 +1,131 @@ +# STACKS IN PYTHON +In Data Structures and Algorithms, a stack is a linear data structure that complies with the Last In, First Out (LIFO) rule. It works by use of two fundamental techniques: *PUSH* which inserts an element on top of the stack and *POP* which takes out the topmost element.This concept is similar to a stack of plates in a cafeteria. Stacks are usually used for handling function calls, expression evaluation, and parsing in programming. Indeed, they are efficient in managing memory as well as tracking program state. + +**POINTS TO BE REMEMBERED :-** +- A stack is a collection of data items that can be accessed at only one end, called *TOP*. +- Items can be inserted and deleted in a stack only at the *TOP*. +- The last item inserted in a stack is the first one to be deleted. +- Therefore, a stack is called a **Last-In-First-Out (LIFO)** data structure. + +## REAL LIFE EXAMPLES OF STACKS + +**PILE OF BOOKS** - Suppose a set of books are placed one over the other in a pile. When you remove books from the pile, the topmost book will be removed first. Similarly, when you have to add a book to the pile, the book will be placed at the top of the file. + +**PILE OF PLATES** - The first plate begins the pile. The second plate is placed on the top of the first plate and the third plate is placed on the top of the second plate, and so on. In general, if you want to add a plate to the pile, you can keep it on the top of the pile. Similarly, if you want to remove a plate, you can remove the plate from the top of the pile. + +**BANGLES IN A HAND** - When a person wears bangles, the last bangle worn is the first one to be removed. + +## APPLICATIONS OF STACKS + +Stacks are widely used in Computer Science: +- *Function call* management +- Maintaining the *UNDO* list for the application +- Web browser *history management* +- Evaluating expressions +- Checking the nesting of parentheses in an expression +- *Backtracking* algorithms (Recursion) + +Understanding these applications is essential for Software Development. + +## OPERATIONS ON A STACK + +Key operations on a stack include: +- **PUSH** - It is the process of inserting a new element on the top of a stack. +- **OVERFLOW** - A situation when we are pushing an item in a stack that is full. +- **POP** - It is the process of deleting an element from the top of a stack. +- **UNDERFLOW** - A situation when we are popping item from an empty stack. +- **PEEK** - It is the process of getting the most recent value of stack *(i.e. the value at the top of the stack)* +- **ISEMPTY** - It is the function which return true if stack is empty else false. +- **SHOW** -Displaying stack items. + +## IMPLEMENTING STACKS IN PYTHON + +```python +def isEmpty(S): + + if len(S) == 0: + return True + + else: + + return False + +def Push(S, item): + S.append(item) + +def Pop(S): + + if isEmpty(S): + return "Underflow" + + else: + val = S.pop() + return val + +def Peek(S): + + if isEmpty(S): + return "Underflow" + + else: + top = len(S) - 1 + return S[top] + +def Show(S): + + if isEmpty(S): + print("Sorry, No items in Stack") + + else: + print("(Top)", end=' ') + t = len(S) - 1 + while t >= 0: + print(S[t], "<", end=' ') + t -= 1 + print() +``` + +This code defines a stack data structure along with functions to manipulate it. To provide output, we would need to use these functions to interact with the stack. + +Here's an example: +```python +stack = [] + +Push(stack, 5) +Push(stack, 10) +Push(stack, 15) + +print("Stack after Push operations:") +Show(stack) + +print("Peek operation:", Peek(stack)) + +print("Pop operation:", Pop(stack)) + +print("Stack after Pop operation:") +Show(stack) +``` + +This would output: + +``` +Stack after Push operations: + +(Top) 15 < 10 < 5 < + +Peek operation: 15 + +Pop operation: 15 + +Stack after Pop operation: + +(Top) 10 < 5 < +``` + +## Complexity Analysis + +- **Worst case**: `O(n)` This occurs when the stack is full, it is dominated by the usage of Show operation. +- **Best case**: `O(1)` When the operations like isEmpty, Push, Pop and Peek are used, they have a constant time complexity of O(1). +- **Average case**: `O(n)` The average complexity is likely to be lower than O(n), as the stack is not always full. + + From 79017403f82fe8cd5e1737ff573eab8506a7fffa Mon Sep 17 00:00:00 2001 From: somyasaxena01 <140182178+somyasaxena01@users.noreply.github.com> Date: Thu, 30 May 2024 10:58:36 +0530 Subject: [PATCH 2/4] Update index.md --- contrib/ds-algorithms/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/ds-algorithms/index.md b/contrib/ds-algorithms/index.md index c61ca0b5..783f982d 100644 --- a/contrib/ds-algorithms/index.md +++ b/contrib/ds-algorithms/index.md @@ -9,3 +9,4 @@ - [Greedy Algorithms](greedy-algorithms.md) - [Dynamic Programming](dynamic-programming.md) - [Linked list](linked-list.md) +- [Stacks in Python](stacks.md) From 9a504138c2b4f1ccb412e4e05cd0d2f76f864448 Mon Sep 17 00:00:00 2001 From: somyasaxena01 <140182178+somyasaxena01@users.noreply.github.com> Date: Sat, 1 Jun 2024 10:12:12 +0530 Subject: [PATCH 3/4] Delete contrib/ds-algorithms/stacks.md --- contrib/ds-algorithms/stacks.md | 131 -------------------------------- 1 file changed, 131 deletions(-) delete mode 100644 contrib/ds-algorithms/stacks.md diff --git a/contrib/ds-algorithms/stacks.md b/contrib/ds-algorithms/stacks.md deleted file mode 100644 index f9f662b2..00000000 --- a/contrib/ds-algorithms/stacks.md +++ /dev/null @@ -1,131 +0,0 @@ -# STACKS IN PYTHON -In Data Structures and Algorithms, a stack is a linear data structure that complies with the Last In, First Out (LIFO) rule. It works by use of two fundamental techniques: *PUSH* which inserts an element on top of the stack and *POP* which takes out the topmost element.This concept is similar to a stack of plates in a cafeteria. Stacks are usually used for handling function calls, expression evaluation, and parsing in programming. Indeed, they are efficient in managing memory as well as tracking program state. - -**POINTS TO BE REMEMBERED :-** -- A stack is a collection of data items that can be accessed at only one end, called *TOP*. -- Items can be inserted and deleted in a stack only at the *TOP*. -- The last item inserted in a stack is the first one to be deleted. -- Therefore, a stack is called a **Last-In-First-Out (LIFO)** data structure. - -## REAL LIFE EXAMPLES OF STACKS - -**PILE OF BOOKS** - Suppose a set of books are placed one over the other in a pile. When you remove books from the pile, the topmost book will be removed first. Similarly, when you have to add a book to the pile, the book will be placed at the top of the file. - -**PILE OF PLATES** - The first plate begins the pile. The second plate is placed on the top of the first plate and the third plate is placed on the top of the second plate, and so on. In general, if you want to add a plate to the pile, you can keep it on the top of the pile. Similarly, if you want to remove a plate, you can remove the plate from the top of the pile. - -**BANGLES IN A HAND** - When a person wears bangles, the last bangle worn is the first one to be removed. - -## APPLICATIONS OF STACKS - -Stacks are widely used in Computer Science: -- *Function call* management -- Maintaining the *UNDO* list for the application -- Web browser *history management* -- Evaluating expressions -- Checking the nesting of parentheses in an expression -- *Backtracking* algorithms (Recursion) - -Understanding these applications is essential for Software Development. - -## OPERATIONS ON A STACK - -Key operations on a stack include: -- **PUSH** - It is the process of inserting a new element on the top of a stack. -- **OVERFLOW** - A situation when we are pushing an item in a stack that is full. -- **POP** - It is the process of deleting an element from the top of a stack. -- **UNDERFLOW** - A situation when we are popping item from an empty stack. -- **PEEK** - It is the process of getting the most recent value of stack *(i.e. the value at the top of the stack)* -- **ISEMPTY** - It is the function which return true if stack is empty else false. -- **SHOW** -Displaying stack items. - -## IMPLEMENTING STACKS IN PYTHON - -```python -def isEmpty(S): - - if len(S) == 0: - return True - - else: - - return False - -def Push(S, item): - S.append(item) - -def Pop(S): - - if isEmpty(S): - return "Underflow" - - else: - val = S.pop() - return val - -def Peek(S): - - if isEmpty(S): - return "Underflow" - - else: - top = len(S) - 1 - return S[top] - -def Show(S): - - if isEmpty(S): - print("Sorry, No items in Stack") - - else: - print("(Top)", end=' ') - t = len(S) - 1 - while t >= 0: - print(S[t], "<", end=' ') - t -= 1 - print() -``` - -This code defines a stack data structure along with functions to manipulate it. To provide output, we would need to use these functions to interact with the stack. - -Here's an example: -```python -stack = [] - -Push(stack, 5) -Push(stack, 10) -Push(stack, 15) - -print("Stack after Push operations:") -Show(stack) - -print("Peek operation:", Peek(stack)) - -print("Pop operation:", Pop(stack)) - -print("Stack after Pop operation:") -Show(stack) -``` - -This would output: - -``` -Stack after Push operations: - -(Top) 15 < 10 < 5 < - -Peek operation: 15 - -Pop operation: 15 - -Stack after Pop operation: - -(Top) 10 < 5 < -``` - -## Complexity Analysis - -- **Worst case**: `O(n)` This occurs when the stack is full, it is dominated by the usage of Show operation. -- **Best case**: `O(1)` When the operations like isEmpty, Push, Pop and Peek are used, they have a constant time complexity of O(1). -- **Average case**: `O(n)` The average complexity is likely to be lower than O(n), as the stack is not always full. - - From 1f54c5a7ccb5ba49d5f49728837521796f2b4de7 Mon Sep 17 00:00:00 2001 From: somyasaxena01 <140182178+somyasaxena01@users.noreply.github.com> Date: Sat, 1 Jun 2024 10:12:55 +0530 Subject: [PATCH 4/4] Add files via upload --- contrib/ds-algorithms/stacks.md | 116 ++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 contrib/ds-algorithms/stacks.md diff --git a/contrib/ds-algorithms/stacks.md b/contrib/ds-algorithms/stacks.md new file mode 100644 index 00000000..428a1938 --- /dev/null +++ b/contrib/ds-algorithms/stacks.md @@ -0,0 +1,116 @@ +# Stacks in Python + +In Data Structures and Algorithms, a stack is a linear data structure that complies with the Last In, First Out (LIFO) rule. It works by use of two fundamental techniques: **PUSH** which inserts an element on top of the stack and **POP** which takes out the topmost element.This concept is similar to a stack of plates in a cafeteria. Stacks are usually used for handling function calls, expression evaluation, and parsing in programming. Indeed, they are efficient in managing memory as well as tracking program state. + +## Points to be Remebered + +- A stack is a collection of data items that can be accessed at only one end, called **TOP**. +- Items can be inserted and deleted in a stack only at the TOP. +- The last item inserted in a stack is the first one to be deleted. +- Therefore, a stack is called a **Last-In-First-Out (LIFO)** data structure. + +## Real Life Examples of Stacks + +- **PILE OF BOOKS** - Suppose a set of books are placed one over the other in a pile. When you remove books from the pile, the topmost book will be removed first. Similarly, when you have to add a book to the pile, the book will be placed at the top of the file. + +- **PILE OF PLATES** - The first plate begins the pile. The second plate is placed on the top of the first plate and the third plate is placed on the top of the second plate, and so on. In general, if you want to add a plate to the pile, you can keep it on the top of the pile. Similarly, if you want to remove a plate, you can remove the plate from the top of the pile. + +- **BANGLES IN A HAND** - When a person wears bangles, the last bangle worn is the first one to be removed. + +## Applications of Stacks + +Stacks are widely used in Computer Science: + +- Function call management +- Maintaining the UNDO list for the application +- Web browser *history management* +- Evaluating expressions +- Checking the nesting of parentheses in an expression +- Backtracking algorithms (Recursion) + +Understanding these applications is essential for Software Development. + +## Operations on a Stack + +Key operations on a stack include: + +- **PUSH** - It is the process of inserting a new element on the top of a stack. +- **OVERFLOW** - A situation when we are pushing an item in a stack that is full. +- **POP** - It is the process of deleting an element from the top of a stack. +- **UNDERFLOW** - A situation when we are popping item from an empty stack. +- **PEEK** - It is the process of getting the most recent value of stack *(i.e. the value at the top of the stack)* +- **isEMPTY** - It is the function which return true if stack is empty else false. +- **SHOW** -Displaying stack items. + +## Implementing Stacks in Python + +```python +def isEmpty(S): + if len(S) == 0: + return True + else: + return False + +def Push(S, item): + S.append(item) + +def Pop(S): + if isEmpty(S): + return "Underflow" + else: + val = S.pop() + return val + +def Peek(S): + if isEmpty(S): + return "Underflow" + else: + top = len(S) - 1 + return S[top] + +def Show(S): + if isEmpty(S): + print("Sorry, No items in Stack") + else: + print("(Top)", end=' ') + t = len(S) - 1 + while t >= 0: + print(S[t], "<", end=' ') + t -= 1 + print() + +stack = [] # initially stack is empty + +Push(stack, 5) +Push(stack, 10) +Push(stack, 15) + +print("Stack after Push operations:") +Show(stack) +print("Peek operation:", Peek(stack)) +print("Pop operation:", Pop(stack)) +print("Stack after Pop operation:") +Show(stack) +``` + +## Output + +```markdown +Stack after Push operations: + +(Top) 15 < 10 < 5 < + +Peek operation: 15 + +Pop operation: 15 + +Stack after Pop operation: + +(Top) 10 < 5 < +``` + +## Complexity Analysis + +- **Worst case**: `O(n)` This occurs when the stack is full, it is dominated by the usage of Show operation. +- **Best case**: `O(1)` When the operations like isEmpty, Push, Pop and Peek are used, they have a constant time complexity of O(1). +- **Average case**: `O(n)` The average complexity is likely to be lower than O(n), as the stack is not always full.