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
Binary file added __pycache__/linked_list.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/queue_with_stacks.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/reverse_string.cpython-311.pyc
Binary file not shown.
39 changes: 39 additions & 0 deletions linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Node:
def _init_(self, data=None):
self.data = data
self.next = None

class LinkedList:
def _init_(self):
self.head = None

def append(self, data):
if not self.head:
self.head = Node(data)
else:
current = self.head
while current.next:
current = current.next
current.next = Node(data)

def find_max(self) -> int:
if not self.head:
raise ValueError("List is empty")

max_element = self.head.data
current = self.head
while current:
if current.data > max_element:
max_element = current.data
current = current.next

return max_element

# Example usage
if __name__ == "_main_":
ll = LinkedList()
ll.append(3)
ll.append(1)
ll.append(4)
ll.append(2)
print(ll.find_max()) # Output: 4
27 changes: 27 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from reverse_string import reverse_string
from queue_with_stacks import QueueWithStacks
from linked_list import LinkedList

def main():
# Task 1: Reverse a String Using a Stack
input_str = "hello"
output_str = reverse_string(input_str)
print(output_str) # Output: "olleh"

# Task 2: Implement a Queue Using Two Stacks
q = QueueWithStacks()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1
print(q.dequeue()) # Output: 2

# Task 3: Find the Maximum Element in a List Using a Linked List
ll = LinkedList()
ll.append(3)
ll.append(1)
ll.append(4)
ll.append(2)
print(ll.find_max()) # Output: 4

if __name__ == "_main_":
main()
22 changes: 22 additions & 0 deletions queue_with_stacks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class QueueWithStacks:
def _init_(self):
self.stack1 = []
self.stack2 = []

def enqueue(self, x: int):
self.stack1.push(x)

def dequeue(self) -> int:
if self.stack2.is_empty():
while not self.stack1.is_empty():
self.stack2.push(self.stack1.pop())

return self.stack2.pop()

# Example usage
if __name__ == "_main_":
q = QueueWithStacks()
q.enqueue(1)
q.enqueue(2)
print(q.dequeue()) # Output: 1
print(q.dequeue()) # Output: 2
32 changes: 32 additions & 0 deletions reverse_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Stack:
def _init_(self):
self.items = []

def push(self, item):
self.items.append(item)

def pop(self):
if not self.is_empty():
return self.items.pop()
else:
return None

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

def reverse_string(s: str) -> str:
stack = Stack()
for char in s:
stack.push(char)

reversed_s = ""
while not stack.is_empty():
reversed_s += stack.pop()

return reversed_s

# Example usage
if __name__ == "_main_":
input_str = "hello"
output_str = reverse_string(input_str)
print(output_str) # Output: "olleh"