diff --git a/answers/40130112086/.idea/.gitignore b/answers/40130112086/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/answers/40130112086/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/answers/40130112086/.idea/40130112086.iml b/answers/40130112086/.idea/40130112086.iml new file mode 100644 index 0000000..6cb8b9a --- /dev/null +++ b/answers/40130112086/.idea/40130112086.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/answers/40130112086/.idea/inspectionProfiles/profiles_settings.xml b/answers/40130112086/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/answers/40130112086/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/answers/40130112086/.idea/misc.xml b/answers/40130112086/.idea/misc.xml new file mode 100644 index 0000000..ef52259 --- /dev/null +++ b/answers/40130112086/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/answers/40130112086/.idea/modules.xml b/answers/40130112086/.idea/modules.xml new file mode 100644 index 0000000..44d9a3d --- /dev/null +++ b/answers/40130112086/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/answers/40130112086/.idea/vcs.xml b/answers/40130112086/.idea/vcs.xml new file mode 100644 index 0000000..b2bdec2 --- /dev/null +++ b/answers/40130112086/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/answers/40130112086/Second.py b/answers/40130112086/Second.py new file mode 100644 index 0000000..7348f8f --- /dev/null +++ b/answers/40130112086/Second.py @@ -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) \ No newline at end of file diff --git a/answers/40130112086/Third.py b/answers/40130112086/Third.py new file mode 100644 index 0000000..254dcfe --- /dev/null +++ b/answers/40130112086/Third.py @@ -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 diff --git a/answers/40130112086/WhichOne.py b/answers/40130112086/WhichOne.py new file mode 100644 index 0000000..b2e00b4 --- /dev/null +++ b/answers/40130112086/WhichOne.py @@ -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 \ No newline at end of file diff --git a/answers/40130112086/main.py b/answers/40130112086/main.py new file mode 100644 index 0000000..fff8407 --- /dev/null +++ b/answers/40130112086/main.py @@ -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() \ No newline at end of file diff --git a/answers/How To Do.txt b/answers/How To Do.txt deleted file mode 100644 index d76cc33..0000000 --- a/answers/How To Do.txt +++ /dev/null @@ -1,6 +0,0 @@ -1. copy project structure folder and paste it to the current folder -2. change the folder that you have pasted here to your student number -3. complete functions and commit changes -4. after you completed all functions make a pull request to main repository - -good luck xD \ No newline at end of file