def merge_queue(queue1,queue2): list1=[] list2=[] merged_queue=Queue(9) while not queue1.is_empty(): list1.append(queue1.dequeue()) while not queue2.is_empty(): list2.append(queue2.dequeue()) if len(list1)<len(list2): for i in range(len(list1)): merged_queue.enqueue(list1.pop(0)) merged_queue.enqueue(list2.pop(0)) for x in list2: merged_queue.enqueue(x) #write your logic here return merged_queue elif len(list1)>len(list2): for i in range(len(list2)): merged_queue.enqueue(list1.pop(0)) merged_queue.enqueue(list2.pop(0)) for x in list1: merged_queue.enqueue(x) return merged_queue else: for i in range(len(list2)): merged_queue.enqueue(list1.pop(0)) merged_queue.enqueue(list2.pop(0)) return merged_queue
def check_numbers(number_queue): solution_queue1 = Queue(number_queue.get_max_size()) list1 = [] while not number_queue.is_empty(): list1.append(number_queue.dequeue()) for i in list1: if i % 1 == 0 and i % 2 == 0 and i % 3 == 0 and i % 4 == 0 and i % 5 == 0 and i % 6 == 0 and i % 7 == 0 and i % 8 == 0 and i % 9 == 0 and i % 10 == 0: solution_queue1.enqueue(i) #wri te your logic here return solution_queue1
def separate_boxes(box_stack): new_queue = Queue(box_stack.get_max_size()) list1 = [] while not box_stack.is_empty(): i = box_stack.pop() if i in ["Red", "Green", "Blue"]: list1.append(i) else: new_queue.enqueue(i) list1.reverse() for i in list1: box_stack.push(i) return new_queue
def merge_queue(queue1,queue2): #write your logic here queue_size = queue1.get_max_size()+queue2.get_max_size() merged_queue = Queue(queue_size) for i in range(0,queue_size): a = queue1.dequeue() if(a is not None): merged_queue.enqueue(a) b = queue2.dequeue() if(b is not None): merged_queue.enqueue(b) return merged_queue
def check_numbers(number_queue): #write your logic here new_queue = Queue(number_queue.get_max_size()) while (not (number_queue.is_empty())): pop = number_queue.dequeue() count = 0 for x in range(1, 11): if (pop % x == 0): count += 1 if (count == 10): new_queue.enqueue(pop) return new_queue
def separate_boxes(box_stack): #Remove pass and write your logic here new_queue = Queue(box_stack.get_max_size()) mod_stack = Stack(box_stack.get_max_size()) while (not (box_stack.is_empty())): data = box_stack.pop() if (data == "Red" or data == "Green" or data == "Blue"): mod_stack.push(data) else: new_queue.enqueue(data) while (not (mod_stack.is_empty())): a = mod_stack.pop() box_stack.push(a) return (new_queue)
class Company: def __init__(self,emp_list): self.__employees = emp_list self.__pending_jobs = Queue(10) def get_employees(self): return self.__employees def get_pending_jobs(self): return self.__pending_jobs def allocate_new_job(self,job): count=0 for i in self.__employees: if(i.get_allocated_job() is None): i.set_allocated_job(job) break else: count+=1 if(count == len(self.__employees)): self.__pending_jobs.enqueue(job) def elapsed_time(self,no_of_mins): list1 = [] for i in self.__employees: job_status = i.elapsed_time(no_of_mins) if i.get_allocated_job() is None: self.allocate_new_job(self.get_pending_jobs().dequeue()) if job_status is not None: list1.append(job_status) if len(list1 )!=0: return list1 else: return None
def queue_ordering(input_queue, input_stack): #Do not modify the size of output_queue output_queue = Queue(input_queue.get_max_size()) list1 = [] list2 = [] while (not (input_queue.is_empty())): list1.append(input_queue.dequeue()) while (not (input_stack.is_empty())): list2.append(input_stack.pop()) i = 0 while (i < len(list2)): if (list2[i] == 1): list1.append(list1[0]) list1.pop(0) print(list1) elif (list2[i] == 2): list1.insert(0, list1[-1]) list1.pop(-1) i = i + 1 for i in list1: output_queue.enqueue(i) #Write your code here return output_queue
i = 0 while (i < len(list2)): if (list2[i] == 1): list1.append(list1[0]) list1.pop(0) print(list1) elif (list2[i] == 2): list1.insert(0, list1[-1]) list1.pop(-1) i = i + 1 for i in list1: output_queue.enqueue(i) #Write your code here return output_queue #You may modify the below code for testing input_stack = Stack(4) input_stack.push(2) input_stack.push(1) input_stack.push(2) input_stack.push(1) input_queue = Queue(5) input_queue.enqueue('X') input_queue.enqueue('a') input_queue.enqueue('b') input_queue.enqueue('c') output_queue = queue_ordering(input_queue, input_stack) output_queue.display()
#DSA-Assgn-14 #This assignment needs DataStructures.py file in your package, you can get it from resources page from src.DataStructures import Queue def check_numbers(number_queue): solution_queue1 = Queue(number_queue.get_max_size()) list1 = [] while not number_queue.is_empty(): list1.append(number_queue.dequeue()) for i in list1: if i % 1 == 0 and i % 2 == 0 and i % 3 == 0 and i % 4 == 0 and i % 5 == 0 and i % 6 == 0 and i % 7 == 0 and i % 8 == 0 and i % 9 == 0 and i % 10 == 0: solution_queue1.enqueue(i) #wri te your logic here return solution_queue1 #Add different values to the queue and test your program number_queue = Queue(5) number_queue.enqueue(13983) number_queue.enqueue(10080) number_queue.enqueue(7113) number_queue.enqueue(2520) number_queue.enqueue(2500) check_numbers(number_queue)
merged_queue.enqueue(list1.pop(0)) merged_queue.enqueue(list2.pop(0)) for x in list1: merged_queue.enqueue(x) return merged_queue else: for i in range(len(list2)): merged_queue.enqueue(list1.pop(0)) merged_queue.enqueue(list2.pop(0)) return merged_queue #Enqueue different values to both the queues and test your program queue1=Queue(3) queue2=Queue(6) queue1.enqueue(3) queue1.enqueue(6) queue1.enqueue(8) queue2.enqueue('b') queue2.enqueue('y') queue2.enqueue('u') queue2.enqueue('t') queue2.enqueue('r') queue2.enqueue('o') merged_queue=merge_queue(queue1, queue2) print("The elements in the merged queue are:") merged_queue.display()