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
3 changes: 3 additions & 0 deletions answers/40130112086/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions answers/40130112086/.idea/40130112086.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions answers/40130112086/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions answers/40130112086/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions answers/40130112086/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions answers/40130112086/Second.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class PriorityQueue:

#Initialize an empty priority queue.
def __init__(self):
self.queue = []

#Add an item to the priority queue with the specified priority.
def enqueue(self, item, priority):
self.queue.append((priority, item))
self.queue.sort(reverse=True)

#Remove and return the item with the highest priority from the queue.
def dequeue(self):
if not self.is_empty():
return self.queue.pop()[1]
else:
return None

#Check if the priority queue is empty.
def is_empty(self):
length = self.size()
if length == 0:
return True
else:
return False

#Return the number of elements in the priority queue.
def size(self):
return len(self.queue)
44 changes: 44 additions & 0 deletions answers/40130112086/Third.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class CircularQueue:

#Initialize an empty circular queue with a maximum size.
def __init__(self, max_size):
self.max_size = max_size #size
self.queue = [None] * max_size
self.front = 0
self.rear = 0
self.num_items = 0

#Add an item to the rear of the circular queue.
def enqueue(self, item):
if self.is_full():
print("Queue is full")
self.queue[self.rear] = item
self.rear = (self.rear + 1) % self.max_size
self.num_items += 1

#Remove and return the front item from the circular queue.
def dequeue(self):
if self.is_empty():
return None
item = self.queue[self.front]
self.front = (self.front + 1) % self.max_size
self.num_items -= 1
return item

#Check if the circular queue is empty.
def is_empty(self):
if self.num_items == 0:
return True
else:
return False

#Check if the circular queue is full
def is_full(self):
if self.num_items == self.max_size:
return True
else:
return False

#Return the number of elements in the circular queue.
def size(self):
return self.num_items
35 changes: 35 additions & 0 deletions answers/40130112086/WhichOne.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#use PriorityQueue for this part
class PriorityQueue:
#Initialize an queue of your choosing
def __init__(self, string):
self.queue = sorted([char for char in string], reverse=True)

#Add an item according to its asscii value into the queue.
def enqueue(self, item):
index = self.binary_search(item)
self.queue.insert(index, item)
#Remove and return the item with the highest ascci value from the queue.
def dequeue(self):
return self.queue.pop(0)
#Print all characters in the queue.
def print(self):
print(', '.join(self.queue))
#Check if the circular queue is empty.
def is_empty(self):
return len(self.queue) == 0
#Return the number of elements in the circular queue.
def size(self):
return len(self.queue)

def binary_search(self, item):
left = 0
right = len(self.queue) - 1

while left <= right:
mid = (left + right) // 2
if ord(self.queue[mid]) < ord(item):
right = mid - 1
else:
left = mid + 1

return left
77 changes: 77 additions & 0 deletions answers/40130112086/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Queue:

#Initialize an empty queue.
def __init__(self):
self.items = []

#Add an item to the rear of the queue.
def enqueue(self, item):
self.items.append(item)

#Remove and return the front item from the queue.
def dequeue(self):
if not self.is_empty():
return self.items.pop(0)
else:
return None

#Check if the queue is empty.
def is_empty(self):
length=self.size()
if length==0:
return True
else:
return False

#Return the number of elements in the queue.
def size(self):
return len(self.items)


### Question 1: Maze Solver using Queues
def maze_solver(maze, start, end):
#Define the movements directions
#right,left,up,down
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
#determining the dimensions of the maze.
rows, cols = len(maze), len(maze[0])
#Initialize the queue and visited set
queue = Queue()
visited = set()
#Add the starting point to the queue and visited set
queue.enqueue(start)
visited.add(start)
#Keep track of the path
path = {start: []}

while not queue.is_empty():
row,col = queue.dequeue()

#Check if it is the end point
if (row, col) == end:
return path[(row, col)]

#Explore the neighboring cells
for dx, dy in directions:
new_row, new_col = row + dx, col + dy
if 0 <= new_row < rows and 0 <= new_col < cols and maze[new_row][new_col] == '0' and (
new_row, new_col) not in visited:
queue.enqueue((new_row, new_col))
visited.add((new_row, new_col))
path[(new_row, new_col)] = path[(row, col)] + [(new_row, new_col)]
return None

###Question 2: Josephus Problem
def josephus(n, k):
#Creating a queue
queue = Queue()
for i in range(1, n + 1):
queue.enqueue(i)

while queue.size() > 1:
#Skip k-1 participants
for i in range(k - 1):
queue.enqueue(queue.dequeue())
#Eliminate the one
queue.dequeue()
return queue.dequeue()
6 changes: 0 additions & 6 deletions answers/How To Do.txt

This file was deleted.