diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..f25c1ce Binary files /dev/null and b/.DS_Store differ diff --git a/answers/.DS_Store b/answers/.DS_Store new file mode 100644 index 0000000..a6de916 Binary files /dev/null and b/answers/.DS_Store differ diff --git a/answers/40130212009/CircularQueue.py b/answers/40130212009/CircularQueue.py new file mode 100644 index 0000000..7f10fd8 --- /dev/null +++ b/answers/40130212009/CircularQueue.py @@ -0,0 +1,35 @@ +class CircularQueue: + def __init__(self, max_size): + self.max_size = max_size # Maximum size of the queue + self.CircleQ = [None] * max_size # Initialize the queue with None values + self.rear = 0 # Index for the rear end of the queue + self.front = 0 # Index for the front end of the queue + self.size = 0 # Current number of elements in the queue + + def enqueue(self, item): + # Add an item to the queue + if self.size == self.max_size: + return "Overflow" # Queue is full + else: + self.CircleQ[self.rear] = item # Place the item at the rear end + self.rear = (self.rear + 1) % self.max_size # Move rear to the next position + self.size += 1 # Increment the size of the queue + + def dequeue(self): + # Remove an item from the queue + if self.isEmpty(): + return "Queue is empty" # Queue is empty + else: + item = self.CircleQ[self.front] # Get the item at the front end + self.CircleQ[self.front] = None # Clear the spot + self.front = (self.front + 1) % self.max_size # Move front to the next position + self.size -= 1 # Decrement the size of the queue + return item # Return the dequeued item + + def isEmpty(self): + # Check if the queue is empty + return self.size == 0 # True if size is 0, else False + + def SizeOfQueue(self): + # Get the current size of the queue + return self.size # Return the number of elements in the queue diff --git a/answers/40130212009/PriorityQueue.py b/answers/40130212009/PriorityQueue.py new file mode 100644 index 0000000..edc1ad0 --- /dev/null +++ b/answers/40130212009/PriorityQueue.py @@ -0,0 +1,42 @@ +class Data: + def __init__(self, priority, item): + self.priority = priority # Set the priority of the item + self.item = item # Set the item + +class PriorityQueue: + def __init__(self): + self.pQueue = [] # Initialize the priority queue as an empty list + self.rear = 0 # Rear pointer starts at 0 + self.front = 0 # Front pointer starts at 0 + + def enqueue(self, item, priority): + # Add an item to the priority queue + new_data = Data(priority, item) # Create a new Data object + if self.isEmpty(): + self.pQueue.append(new_data) # Add the item to the queue + else: + inserted = False + for i in range(len(self.pQueue)): + if self.pQueue[i].priority <= new_data.priority: + self.pQueue.insert(i, new_data) # Insert the item based on priority + inserted = True + break + if not inserted: + self.pQueue.append(new_data) # Append if it's the highest priority + self.rear += 1 # Increment the rear pointer + + def dequeue(self): + # Remove an item from the priority queue + if self.isEmpty(): + return "Underflow" # The queue is empty + else: + self.front += 1 # Increment the front pointer + return self.pQueue.pop(0).item # Return and remove the item with the highest priority + + def isEmpty(self): + # Check if the queue is empty + return self.rear == self.front # True if rear and front are equal, else False + + def sizeOfQueue(self): + # Get the current size of the queue + return self.rear - self.front # Return the number of elements in the queue diff --git a/answers/40130212009/Q1.py b/answers/40130212009/Q1.py new file mode 100644 index 0000000..3e5c996 --- /dev/null +++ b/answers/40130212009/Q1.py @@ -0,0 +1,53 @@ +class Queue: + def __init__(self): + self.items = [] # Initialize the list to hold the queue items + self.rear = 0 # Rear pointer to track the end of the queue + self.front = 0 # Front pointer to track the start of the queue + + def enqueue(self, item): + # Add an item to the rear of the queue + self.items.append(item) # Append item to the list + self.rear += 1 # Increment the rear pointer + + def dequeue(self): + # Remove an item from the front of the queue + if self.isEmpty(): + return "Underflow" # Return underflow if the queue is empty + else: + self.front += 1 # Increment the front pointer + return self.items.pop(0) # Pop the first item from the list + + def isEmpty(self): + # Check if the queue is empty + return self.rear == self.front # True if rear equals front, else False + + def sizeOfQueue(self): + # Return the current size of the queue + return self.rear - self.front # Calculate the size by subtracting front from rear + +def shortest_path(maze, start, end): + # Find the shortest path in a maze from start to end + directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] # Possible movement directions + rows, cols = len(maze), len(maze[0]) # Dimensions of the maze + visited = set() # Set to track visited positions + queue = Queue() # Initialize the queue + + queue.enqueue((start, 0)) # Enqueue the start position with distance 0 + + while not queue.isEmpty(): + current_pos, dist = queue.dequeue() # Dequeue the current position and distance + + if current_pos == end: + return dist # Return the distance if end is reached + + visited.add(current_pos) # Mark the current position as visited + + for dx, dy in directions: + # Calculate the new position + new_x, new_y = current_pos[0] + dx, current_pos[1] + dy + + # Check if the new position is valid and not visited + if 0 <= new_x < rows and 0 <= new_y < cols and maze[new_x][new_y] == 0 and (new_x, new_y) not in visited: + queue.enqueue(((new_x, new_y), dist + 1)) # Enqueue the new position with updated distance + + return -1 # Return -1 if no path is found diff --git a/answers/40130212009/Queue.py b/answers/40130212009/Queue.py new file mode 100644 index 0000000..68c2f36 --- /dev/null +++ b/answers/40130212009/Queue.py @@ -0,0 +1,26 @@ +class Queue: + def __init__(self): + self.items = [] # Initialize an empty list to hold queue items + self.rear = 0 # Initialize rear pointer to track the end of the queue + self.front = 0 # Initialize front pointer to track the start of the queue + + def enqueue(self, item): + # Add an item to the rear of the queue + self.items.append(item) # Append the item to the list + self.rear += 1 # Increment the rear pointer + + def dequeue(self): + # Remove an item from the front of the queue + if self.isEmpty(): + return "Underflow" # Return "Underflow" if the queue is empty + else: + self.front += 1 # Increment the front pointer + return self.items.pop(0) # Remove and return the first item from the list + + def isEmpty(self): + # Check if the queue is empty + return self.rear == self.front # True if rear equals front, else False + + def sizeOfQueue(self): + # Return the current size of the queue + return self.rear - self.front # Calculate size by subtracting front from rear