Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

may hackathon 1 #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions task1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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:
"""
Reverses a string using a stack data structure.

Args:
s (str): The input string to be reversed.

Returns:
str: The reversed string.
"""
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:
input_str = "Hello, World!"
print(reverse_string(input_str)) # Output: "!dlroW ,olleH"
30 changes: 30 additions & 0 deletions task2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class QueueWithStacks:
def __init__(self):
# Initialize two empty stacks
self.stack1 = []
self.stack2 = []

def enqueue(self, x: int):
# Add an element to the queue by pushing it onto stack1
self.stack1.append(x)

def dequeue(self) -> int:
# If both stacks are empty, the queue is empty
if not self.stack1 and not self.stack2:
raise IndexError("Cannot dequeue from an empty queue")

# If stack2 is empty, pop all elements from stack1 and push them onto stack2
if not self.stack2:
while self.stack1:
self.stack2.append(self.stack1.pop())

# The front element of the queue is now at the top of stack2
return self.stack2.pop()

queue = QueueWithStacks()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(queue.dequeue())
print(queue.dequeue())
print(queue.dequeue())
45 changes: 45 additions & 0 deletions task3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Node:
def __init__(self, data):
self.data = data
self.next = None

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

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

def find_max(self):
"""
Finds the maximum element in the linked list.

Returns:
int: The maximum element in the list.
"""
if not self.head:
return None
max_element = self.head.data
current = self.head.next
while current:
if current.data > max_element:
max_element = current.data
current = current.next
return max_element

# Example usage:
linked_list = LinkedList()
linked_list.append(50)
linked_list.append(10)
linked_list.append(37)
linked_list.append(20)
linked_list.append(15)

print(linked_list.find_max())