def test_switch_pairs(self): # Test case: even number of values in stack # bottom [3, 8, 17, 9, 1, 10] top self.assertEqual([8, 3, 9, 17, 10, 1], first_switch_pairs([3, 8, 17, 9, 1, 10])) self.assertEqual([8, 3, 9, 17, 10, 1], second_switch_pairs([3, 8, 17, 9, 1, 10])) # Test case: odd number of values in stack # bottom [3, 8, 17, 9, 1] top self.assertEqual([8, 3, 9, 17, 1], first_switch_pairs([3, 8, 17, 9, 1])) self.assertEqual([8, 3, 9, 17, 1], second_switch_pairs([3, 8, 17, 9, 1]))
""" Given a stack, switch_pairs function takes a stack as a parameter and that switches successive pairs of numbers starting at the bottom of the stack. For example, if the stack initially stores these values: bottom [3, 8, 17, 9, 1, 10] top Your function should switch the first pair (3, 8), the second pair (17, 9), ...: bottom [8, 3, 9, 17, 10, 1] top if there are an odd number of values in the stack, the value at the top of the stack is not moved: For example: bottom [3, 8, 17, 9, 1] top It would again switch pairs of values, but the value at the top of the stack (1) would not be moved bottom [8, 3, 9, 17, 1] top Note: There are 2 solutions: first_switch_pairs: it uses a single stack as auxiliary storage second_switch_pairs: it uses a single queue as auxiliary storage """ from algorithms.stack import first_switch_pairs, second_switch_pairs a = [3, 8, 17, 9, 1, 10] print(first_switch_pairs(a)) print(second_switch_pairs(a))