From ed750a67473069314deb50e65ec2dd55f6d02f74 Mon Sep 17 00:00:00 2001 From: Amir-c-137 Date: Mon, 27 May 2024 00:30:02 +0330 Subject: [PATCH 1/2] add 3 func --- .DS_Store | Bin 0 -> 6148 bytes answers/.DS_Store | Bin 0 -> 6148 bytes answers/40130212009/CircularQueue.py | 35 ++++++++++++++++++ answers/40130212009/PriorityQueue.py | 42 +++++++++++++++++++++ answers/40130212009/Q1.py | 53 +++++++++++++++++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 .DS_Store create mode 100644 answers/.DS_Store create mode 100644 answers/40130212009/CircularQueue.py create mode 100644 answers/40130212009/PriorityQueue.py create mode 100644 answers/40130212009/Q1.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f25c1ce0ddd5d1c4e70340ad4275f814f5343636 GIT binary patch literal 6148 zcmeHK!A{#i5S2%CmCZ z>0GPEQgiuD%WHWn-dXfrSJ5~gPs(0AI^o`-)_HPk$H`GPtOo73d%76MSuq@$ge)B* zCR@p_ilab)4||Jep%>W zX4a&zLXE6CyoKK|{$t>0G%9jke8UK5`E)@0;Gs`nftz$msa@lD9Q{GmeU8u-5e9?- zVPJt7a66*6vcPkcsR;wZz{@be`$GU_3?0@M&DMd&lmI}!MjC<5mqyMB9EJ{Six`0@ zn+mk4%9R+(ro$h&xX@v3(WaAf#fNflR<1%(=IwZXV8Tg-7Nr#ign@?)EZJ_G&;R|W z>;H#I(h~-Rfmg+VYWAak51-`D*13=4vo?U9Ls>Ykws@BU!yLtk<)ipL)Cl|m8^F+E SZ4n-bTm%da(g*|3%D_K(ie;q$ literal 0 HcmV?d00001 diff --git a/answers/.DS_Store b/answers/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a6de91631da0eb04136f2df40e321c8c214f71cc GIT binary patch literal 6148 zcmeHK!EO^V5FNLHIz*Ln0L0M}N2; zU+@)t0-wN7z?B0hcw;+2DM4^SsPaTJZ*0%3?YFVLCL+2&<=H=0e)UQ1D=8ZodI6EE!xF;?a&R_@9TH(Uak+5TqlU&?cojd+7~16 zR%RvDbnLR;CgVJtmO=1dtgScBo!^ifvL&C!$GVEkK{=cD29w8JI?y^#mi{1lm<_9b z`{Hd~l!L4oPE10U4iWO?QC6h7>gidLPEBrX8)Q>9`|Zo~d9bB+x_gT)HSc!e-`wpk z7EO8S>h=8x{pb0p(67wsNnpn^vgvRMui>m8MvOE_7I1wCN<=tE8OgM?qqPLy_&%j9r)@`xP=l`A6_y3bg zzT_G344f$jL?eo$9;W2Z)>D(?vsOW$L0LGiwrEmd+;MCiK8mkEji61j0}LJ37EuH7 O9|DF3Z#)D4l!31nr-7mX literal 0 HcmV?d00001 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 From ca21a040e3aecd725af712bac0fc9c78185cc5e5 Mon Sep 17 00:00:00 2001 From: Amir-c-137 Date: Mon, 27 May 2024 00:31:27 +0330 Subject: [PATCH 2/2] add another func --- answers/40130212009/Queue.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 answers/40130212009/Queue.py 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