Exemplo n.º 1
0
def hot_potato(names_lst, num):
    """     
     queue data structure implementation
     returns the last person surviving in the queue
     """
    
    names = Queue()
    
    for i in names_lst:
        # add the names to the queue
        names.add(i)
    
    turns = 1
    while names.get_length() > 1:
        
        if turns < num:
            # get and return items to queue
            potato = names.get()
            names.add(potato)
            turns += 1
            
        else:
            # when turns == num remove item
            names.get()
            turns = 1
    
    # get the survivor off of the queue
    return names.get()       
Exemplo n.º 2
0
class TestQueue(TestStack):

    # inherit from testStack since similar tests will be run with Queue
    def setUp(self):
        self.structure = Queue()

    def test_structure_peek(self):
        structure = self.add_to_structure(
            self, iter(['people', 'potatoes', 'pansies']))
        # first word to the queue appears with peak
        self.assertEqual(self.structure.peek(), 'people')

    def test_structure_get(self):
        structure = self.add_to_structure(self, range(3))

        # test that last in are first out
        for i in range(3):
            self.assertEqual(self.structure.get(), i)