Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 54 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
# Data Structures and Algorithms Practice

This repository contains your solutions to three simple data structures and algorithms problems. Please follow the instructions below to complete the tasks and submit your work.

## Questions

### 1. Reverse a String Using a Stack
- **Task:** Implement a stack data structure to reverse a string.
- **Function:** `reverse_string(s: str) -> str`
- **Example:**
- Input: `"hello"`
- Output: `"olleh"`

### 2. Implement a Queue Using Two Stacks
- **Task:** Implement a queue using two stacks.
- **Class:** `QueueWithStacks`
- **Methods:**
- `enqueue(x: int)`: Adds an element to the queue.
- `dequeue() -> int`: Removes and returns the front element of the queue.
- **Example:**
```python
q = QueueWithStacks()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1
print(q.dequeue()) # Output: 2


### 3. Find the Maximum Element in a List Using a Linked List
- **Task:** Implement a singly linked list and find the maximum element in the list.
- **Class:** LinkedList
- **Method:** find_max() -> int
- **Example**
```python
ll = LinkedList()
ll.append(3)
ll.append(1)
ll.append(4)
ll.append(2)
print(ll.find_max()) # Output: 4


### Submission Instructions
- Fork this repository.
- Clone the forked repository to your local machine.
- Create a separate branch for your solutions.
- Implement the solutions to the above questions in Python.
- Commit your changes with clear and descriptive messages.
- Push your changes to your forked repository.
- Create a pull request (PR) to the original repository with your solutions.
- Submit the URL of your GitHub repository as your final submission.

### Submission form
https://forms.gle/VUTFyWTXKUPq4CMQA
# Data Structures and Algorithms Practice
This repository contains your solutions to three simple data structures and algorithms problems. Please follow the instructions below to complete the tasks and submit your work.
## Questions
### 1. Reverse a String Using a Stack
- **Task:** Implement a stack data structure to reverse a string.
- **Function:** `reverse_string(s: str) -> str`
- **Example:**
- Input: `"hello"`
- Output: `"olleh"`
### 2. Implement a Queue Using Two Stacks
- **Task:** Implement a queue using two stacks.
- **Class:** `QueueWithStacks`
- **Methods:**
- `enqueue(x: int)`: Adds an element to the queue.
- `dequeue() -> int`: Removes and returns the front element of the queue.
- **Example:**
```python
q = QueueWithStacks()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1
print(q.dequeue()) # Output: 2
### 3. Find the Maximum Element in a List Using a Linked List
- **Task:** Implement a singly linked list and find the maximum element in the list.
- **Class:** LinkedList
- **Method:** find_max() -> int
- **Example**
```python
ll = LinkedList()
ll.append(3)
ll.append(1)
ll.append(4)
ll.append(2)
print(ll.find_max()) # Output: 4
### Submission Instructions
- Fork this repository.
- Clone the forked repository to your local machine.
- Create a separate branch for your solutions.
- Implement the solutions to the above questions in Python.
- Commit your changes with clear and descriptive messages.
- Push your changes to your forked repository.
- Create a pull request (PR) to the original repository with your solutions.
- Submit the URL of your GitHub repository as your final submission.
### Submission form
https://forms.gle/VUTFyWTXKUPq4CMQA
31 changes: 31 additions & 0 deletions Reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Stack:
def __init__(self):
# Initializes an empty list
self.items = []

def push(self, item):
# Adds an item
self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()
raise IndexError("pop from empty stack")

def is_empty(self):
return len(self.items) == 0

def reverse_string(s: str) -> str:
# Create a stack instance
stack = Stack()

for char in s:
stack.push(char)

reversed_str = ''
while not stack.is_empty():
reversed_str += stack.pop()

return reversed_str

print(reverse_string("hello"))
46 changes: 46 additions & 0 deletions append.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Node:
def __init__(self, data):
# Initialize a node with data and a pointer to the next node
self.data = data
self.next = None

class LinkedList:
def __init__(self):
# Initialize the head of the linked list
self.head = None

def append(self, data):
# Add a new node with the given data to the end of the list
new_node = Node(data)
if not self.head:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node

def find_max(self) -> int:
# Find the maximum element in the linked list
if not self.head:
raise ValueError("The linked list is empty")

max_value = self.head.data
current_node = self.head

while current_node:
if current_node.data > max_value:
max_value = current_node.data
current_node = current_node.next

return max_value


ll = LinkedList()
ll.append(3)
ll.append(5)
ll.append(2)
ll.append(8)
ll.append(1)

print(ll.find_max())
27 changes: 27 additions & 0 deletions queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class QueueWithStacks:
def __init__(self):
# Initialize two stacks
self.stack_in = [] # Stack for enqueue operations
self.stack_out = [] # Stack for dequeue operations

def enqueue(self, x: int):
# Push the new element onto the input stack
self.stack_in.append(x)

def dequeue(self) -> int:
# If the output stack is empty, transfer elements from input stack
if not self.stack_out:
while self.stack_in:
self.stack_out.append(self.stack_in.pop())

# If the output stack is still empty, raise an error
if not self.stack_out:
raise IndexError("dequeue from empty queue")

return self.stack_out.pop()

q = QueueWithStacks()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue())
print(q.dequeue())