def merge_stack(stack1, stack2): #Remove pass and write your logic here lst = [] while not stack1.is_empty(): lst.append(stack1.pop()) while not stack2.is_empty(): lst.append(stack2.pop()) lst.sort() new_stack = Stack(len(lst)) for i in lst: new_stack.push(i) return new_stack
def merge_stack(stack1, stack2): output = Stack(stack1.get_max_size() + stack2.get_max_size()) l1 = [] while not (stack1.is_empty()): l1.append(stack1.pop()) while not (stack2.is_empty()): l1.append(stack2.pop()) l1.sort() for x in l1: output.push(x) return output #Remove pass and write your logic here pass
def regenerate_stack(input_stack): output_stack = Stack( input_stack.get_max_size()) #Do Not Change the size of the ouput_stack count = 0 lst = [] while not input_stack.is_empty(): val = input_stack.pop() if (val == '@'): count += 1 if (count == 1): if (val == '@'): val = '.' val = val.upper() lst.append(val) else: if (val == '@'): val = '.' lst.append(val) for i in lst[::-1]: output_stack.push(i) return output_stack
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(self.ball_container.get_max_size()): ball = self.ball_container.pop() if ball.get_color() == "Red": self.red_balls_container.push(ball) elif ball.get_color() == "Green": self.green_balls_container.push(ball) elif ball.get_color() == "Yellow": self.yellow_balls_container.push(ball) elif ball.get_color() == "Blue": self.blue_balls_container.push(ball) 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
def display_ball_details(self, color): pass # Implement Game class here # 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) g1 = Game(ball_list) g1.grouping_based_on_color() # Create objects of Game class, invoke the methods and test the program
while not (box_stack.is_empty()): poped = box_stack.pop() if poped in ["Red", "Blue", "Green"]: lst.append(poped) else: que.enqueue(poped) while len(lst) != 0: box_stack.push(lst.pop()) return que #Remove pass and write your logic here pass #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:")
count = tmp.count(mini) for i in range (count): tmp.remove(mini) number_stack.push(mini) for x in tmp[::-1]: number_stack.push(x) return number_stack number_stack=Stack(8) number_stack.push(4) number_stack.push(7) number_stack.push(3) number_stack.push(3) number_stack.push(90) # number_stack.push(4) # number_stack.push(14) # number_stack.push(7) print("Initial Stack:") number_stack.display() change_smallest_value(number_stack) print("After the change:") number_stack.display()
lst = [] while not stack1.is_empty(): lst.append(stack1.pop()) while not stack2.is_empty(): lst.append(stack2.pop()) lst.sort() new_stack = Stack(len(lst)) for i in lst: new_stack.push(i) return new_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()
count += 1 if (count == 1): if (val == '@'): val = '.' val = val.upper() lst.append(val) else: if (val == '@'): val = '.' lst.append(val) for i in lst[::-1]: output_stack.push(i) return output_stack #You can modify the input to test your code input_stack = Stack(10) input_stack.push('Hello') input_stack.push('How') input_stack.push('are') input_stack.push('@') input_stack.push('@') input_stack.push('you') # input_stack.push('you') # input_stack.push('@') # input_stack.push('WelCome') # input_stack.push('hi') output_stack = regenerate_stack(input_stack) output_stack.display()
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): while not self.ball_container.is_empty(): ele = self.ball_container.pop() if (ele.get_color() == 'Red'): self.red_balls_container.push(ele) elif (ele.get_color() == 'Blue'): self.blue_balls_container.push(ele) elif (ele.get_color() == 'Green'): self.green_balls_container.push(ele) elif (ele.get_color() == 'Yellow'): self.yellow_balls_container.push(ele) def rearrange_balls(self, color): if (color == 'Red'): red_list = [] while not self.red_balls_container.is_empty(): red_list.append(self.red_balls_container.pop()) red_list.sort(key=lambda nam: nam.get_name(), reverse=False) for i in red_list[::-1]: self.red_balls_container.push(i) elif (color == 'Blue'): blue_list = [] while not self.blue_balls_container.is_empty(): blue_list.append(self.blue_balls_container.pop()) blue_list.sort(key=lambda nam: nam.get_name(), reverse=False) for i in blue_list[::-1]: self.blue_balls_container.push(i) elif (color == 'Green'): green_list = [] while not self.green_balls_container.is_empty(): green_list.append(self.green_balls_container.pop()) green_list.sort(key=lambda nam: nam.get_name(), reverse=False) for i in green_list[::-1]: self.green_balls_container.push(i) elif (color == 'Yellow'): yellow_list = [] while not self.yellow_balls_container.is_empty(): yellow_list.append(self.yellow_balls_container.pop()) yellow_list.sort(key=lambda nam: nam.get_name(), reverse=False) for i in yellow_list[::-1]: self.yellow_balls_container.push(i) def display_ball_details(self, color): pass