def merge_stack(stack1, stack2): #Remove pass and write your logic here list1 = [] output_stack = Stack(stack1.get_max_size() + stack2.get_max_size()) while (not (stack1.is_empty())): list1.append(stack1.pop()) while (not (stack2.is_empty())): list1.append(stack2.pop()) list1.sort() for i in list1: output_stack.push(i) return output_stack
def merge_stack(stack1, stack2): new_stack = Stack(stack1.get_max_size() + stack2.get_max_size()) list1 = [] while not stack1.is_empty(): list1.append(stack1.pop()) while not stack2.is_empty(): list1.append(stack2.pop()) list1.sort() while not new_stack.is_full(): new_stack.push(list1.pop(0)) return new_stack
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)
#Remove pass and write your logic here list1 = [] output_stack = Stack(stack1.get_max_size() + stack2.get_max_size()) while (not (stack1.is_empty())): list1.append(stack1.pop()) while (not (stack2.is_empty())): list1.append(stack2.pop()) list1.sort() for i in list1: output_stack.push(i) return output_stack #Pass different values to the function and test your program stack2 = Stack(3) stack2.push(9) stack2.push(11) stack2.push(15) stack1 = Stack(4) stack1.push(3) stack1.push(7) stack1.push(10) stack1.push(21) print("The elements in stack1 are:") stack1.display() print("The elements in stack2 are:") stack2.display() print()
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()
return new_queue # print(list1) # list2=[] # for i in range(0,len(list1)): # if list1[i]=="Red" or list1[i]=="Green" or list1[i]=="Blue": # list2.append(list1.pop(i)) # # print(list2) # print(list1) # box_stack.push() #Remove pass and write your logic here #Use different values for stack and test your program box_stack = Stack(8) box_stack.push("Red") box_stack.push("Magenta") box_stack.push("Yellow") box_stack.push("Red") box_stack.push("Orange") box_stack.push("Green") box_stack.push("White") box_stack.push("Purple") print("Boxes in the stack:") box_stack.display() result = separate_boxes(box_stack) print() print("Boxes in the stack after modification:") box_stack.display() print("Boxes in the queue:")
l2.append(i) # print(l1) # print(l2) l2.reverse() m = [] m = l1 + l2 for i in m: number_stack.push(i) #write your logic here return number_stack #Add different values to the stack and test your program number_stack = Stack(8) number_stack.push(7) number_stack.push(8) number_stack.push(5) number_stack.push(66) number_stack.push(5) print("Initial Stack:") number_stack.display() change_smallest_value(number_stack) print("After the change:") number_stack.display() # from src.DS_stack import Stack # # def change_smallest_value(number_stack): # list1=[]
def __init__(self,ball_stack): self.ball_container=ball_stack self.red_balls_container=Stack(2) self.green_balls_container = Stack(2) self.blue_balls_container = Stack(2) self.yellow_balls_container = Stack(2)
class Game: def __init__(self,ball_stack): self.ball_container=ball_stack self.red_balls_container=Stack(2) self.green_balls_container = Stack(2) self.blue_balls_container = Stack(2) self.yellow_balls_container = Stack(2) def grouping_based_on_color(self): for i in range(0,self.ball_container.get_max_size()): a = self.ball_container.pop() if(a.get_color()=="Red"): self.red_balls_container.push(a) elif(a.get_color()=="Blue"): self.blue_balls_container.push(a) elif(a.get_color()=="Yellow"): self.yellow_balls_container.push(a) elif(a.get_color()=="Green"): self.green_balls_container.push(a) def rearrange_balls(self,color): if(color=="Red"): for x in range (2): ball =self.red_balls_container.pop() if(ball.get_name()=="A"): a = ball else: b = ball self.red_balls_container.push(b) self.red_balls_container.push(a) elif(color == "Blue"): for x in range (2): ball =self.blue_balls_container.pop() if(ball.get_name()=="A"): a = ball else: b = ball self.blue_balls_container.push(b) self.blue_balls_container.push(a) def display_ball_details(self,color): pass
a = ball else: b = ball self.blue_balls_container.push(b) self.blue_balls_container.push(a) def display_ball_details(self,color): pass #Use different values to test your program ball1=Ball("Red","A") ball2=Ball("Blue","B") ball3=Ball("Yellow","B") ball4=Ball("Blue","A") ball5=Ball("Yellow","A") ball6=Ball("Green","B") ball7=Ball("Green","A") ball8=Ball("Red","B") ball_list=Stack(8) ball_list.push(ball1) ball_list.push(ball2) ball_list.push(ball3) ball_list.push(ball4) ball_list.push(ball5) ball_list.push(ball6) ball_list.push(ball7) ball_list.push(ball8) #Create objects of Game class, invoke the methods and test the program g1 = Game(ball_list) g1.grouping_based_on_color() g1.display_ball_details("Green")
class Game: def __init__(self, ball_stack): self.ball_container = ball_stack self.red_balls_container = Stack(4) self.green_balls_container = Stack(4) self.blue_balls_container = Stack(4) self.yellow_balls_container = Stack(4) def grouping_based_on_color(self): while (not (self.ball_container.is_empty())): a = self.ball_container.pop() if (a.get_color() == "Red"): self.red_balls_container.push(a) elif (a.get_color() == "Green"): self.green_balls_container.push(a) elif (a.get_color() == "Yellow"): self.yellow_balls_container.push(a) elif (a.get_color() == "Blue"): self.blue_balls_container.push(a) def rearrange_balls(self, color): if (color == "Red"): list1 = [] list2 = [] while (not (self.red_balls_container.is_empty())): a = self.red_balls_container.pop() if (a.get_name() == "B"): list1.append(a) else: list2.append(a) for i in list1: self.red_balls_container.push(i) for j in list2: self.red_balls_container.push(j) elif (color == "Green"): list1 = [] list2 = [] while (not (self.green_balls_container.is_empty())): a = self.green_balls_container.pop() if (a.get_name() == "B"): list1.append(a) else: list2.append(a) for i in list1: self.green_balls_container.push(i) for j in list2: self.green_balls_container.push(j) elif (color == "Blue"): list1 = [] list2 = [] while (not (self.blue_balls_container.is_empty())): a = self.blue_balls_container.pop() if (a.get_name() == "B"): list1.append(a) else: list2.append(a) for i in list1: self.blue_balls_container.push(i) for j in list2: self.blue_balls_container.push(j) elif (color == "Yellow"): list1 = [] list2 = [] while (not (self.yellow_balls_container.is_empty())): a = self.yellow_balls_container.pop() if (a.get_name() == "B"): list1.append(a) else: list2.append(a) for i in list1: self.yellow_balls_container.push(i) for j in list2: self.yellow_balls_container.push(j) def display_ball_details(self, color): pass