def eliminated(people, skip): """Return a list of the positions of people as they are eliminated. The position of the last person standing is at the end of the list. Assume people is a positive integer. Assume skip is a non-negative integer. For example: >>> eliminated(3, 0) [1, 2, 3] >>> eliminated(3, 1) [2, 1, 3] >>> eliminated(3, 4) [2, 3, 1] """ assert people > 0 assert skip >= 0 circle = Queue() for position in range(1, people + 1): circle.enqueue(position) positions = [] while circle: for _skipped in range(skip): circle.enqueue(circle.dequeue()) positions.append(circle.dequeue()) return positions
def hot_potato(name_list, num): q = Queue() for i in range(0, len(name_list)): q.enqueue(name_list[i]) for j in range(0, len(name_list) - 1): for k in range(0, num): q.enqueue(q.dequeue()) q.dequeue() remaining_person = q.dequeue() return remaining_person
def hot_potato(namelist, num): q = Queue() for name in namelist: q.enqueue(name) while q.size() > 1: for i in range(num): q.enqueue(q.dequeue()) q.dequeue() return q.dequeue()
def test_len(self): q = Queue() assert len(q) == 0 q.enqueue('A') assert len(q) == 1 q.enqueue('B') assert len(q) == 2 q.dequeue() assert len(q) == 1 q.dequeue() assert len(q) == 0
def hot_potato(name_list, num): # Finish the function q = Queue() for i in name_list: q.enqueue(i) while q.size() > 1: for _ in range(num): tmp = q.dequeue() q.enqueue(tmp) q.dequeue() remaining_person = q.dequeue() return remaining_person
def hot_potato(name_list, num): q = Queue() for name in name_list: q.enqueue(name) while q.size() > 1: for j in range(num): person = q.dequeue() q.enqueue(person) q.dequeue() remaining_person = q.dequeue() return remaining_person
def test_dequeue(self): q = Queue() q.enqueue('A') assert len(q) == 1 assert q.head.data == 'A' assert q.tail.data == 'A' q.enqueue('B') assert len(q) == 2 assert q.head.data == 'A' assert q.tail.data == 'B' q.dequeue() assert len(q) == 1 assert q.head.data == 'B' assert q.tail.data == 'B'
def test_dequeuing_from_queue_removes_item(): queue = Queue() queue.enqueue(1) assert_equal(queue.empty(), False) assert_equal(queue.dequeue(), 1) assert_equal(queue.empty(), True)
def test_dequeuing_from_queue_returns_first_item(): queue = Queue() queue.enqueue(1) queue.enqueue(2) queue.enqueue(3) assert_equal(queue.dequeue(), 1)
def test_enqueuing_to_queue_adds_item_to_queue(): queue = Queue() assert_equal(queue.empty(), True) queue.enqueue(1) assert_equal(queue.empty(), False) assert_equal(queue.dequeue(), 1)
def test_dequeue_different_sizes(self, lst: list) -> None: """ Tests the Queue.dequeue method on queues with different amounts of items. """ q = Queue(lst) while not q.is_empty(): self.assertEqual(lst.pop(0), q.dequeue())
def hot_potato(name_list, num): number=len(name_list) peoplelist=Queue() for i in range(number): peoplelist.enqueue(name_list[i]) while number>1: for i in range(num): front=peoplelist.dequeue() peoplelist.enqueue(front) peoplelist.dequeue() number-=1 remaining_person=peoplelist.dequeue() return remaining_person
def hot_potato(name_list, num): q = Queue() for i in range(len(name_list)): q.enqueue(name_list[i]) while num > 0 : if q.size() > 1: for i in range(num): q.enqueue(q.dequeue()) q.dequeue() if q.size() == 1: remaining_person = q.dequeue() break return remaining_person
def del_min(self): q = Queue() del self.tree[0][0] q.enqueue(self.tree[self.power][len(self.tree[self.power]) - 1]) del self.tree[self.power][len(self.tree[self.power]) - 1] self.tree[0].insert(0, q.dequeue()) return self.tree
def find_bfs(self, data): queue = Queue() queue.enqueue(self.root) while not queue.empty: next_ = queue.dequeue() if next_: queue.enqueue(next_.left) queue.enqueue(next_.right) if next_.data == data: return next_
def bfs(g, start): start.setDistance(0) start.setPred(None) vertQueue = Queue() vertQueue.enqueue(start) while (vertQueue.size() > 0): currentVert = vertQueue.dequeue() for nbr in currentVert.getConnections(): if (nbr.getColor() == 'white'): nbr.setColor('gray') nbr.setDistance(currentVert.getDistance() + 1) nbr.setPred(currentVert) vertQueue.enqueue(nbr) currentVert.setColor('black')
class TestQueue(unittest.TestCase): def setUp(self): # Create queues for the tests to use self.new = Queue() self.empty = Queue() self.empty.enqueue('hi') self.empty.dequeue() # Don't add in ascending or descending order self.non_empty = Queue() self.non_empty.enqueue(5) self.non_empty.enqueue(2) self.non_empty.enqueue(7) self.non_empty.enqueue(2) def test_length(self): self.assertEqual(len(self.new), 0) self.assertEqual(len(self.empty), 0) self.assertEqual(len(self.non_empty), 4) def test_is_empty(self): self.assertTrue(self.new.is_empty()) self.assertTrue(self.empty.is_empty()) self.assertFalse(self.non_empty.is_empty()) def test_fifo_order(self): self.assertEqual(self.non_empty.dequeue(), 5) self.assertEqual(self.non_empty.dequeue(), 2) self.assertEqual(self.non_empty.dequeue(), 7) self.assertEqual(self.non_empty.dequeue(), 2) def test_access_to_empty(self): with self.assertRaises(AssertionError): self.new.front() with self.assertRaises(AssertionError): self.empty.front() with self.assertRaises(AssertionError): self.new.dequeue() with self.assertRaises(AssertionError): self.empty.dequeue() def test_membership(self): self.assertFalse(2 in self.new) self.assertFalse(2 in self.empty) self.assertTrue(2 in self.non_empty) self.assertTrue(5 in self.non_empty) self.assertTrue(7 in self.non_empty)
def base_converter(dec, base): digits = "0123456789ABCDEF" i = 0 for j in range(10000): if(dec // (base**j) == 0): i = j break q = Queue() for k in range(i): k = i - k temp = dec % (base ** k) // (base ** (k-1)) q.enqueue(digits[temp]) str_temp = "" for l in range(i): str_temp += str(q.dequeue()) return str_temp
def initialize(self): q = Queue() for i in range(self.power, 0, -1): for j in range(0, 2**(i - 1)): try: self.tree[i][2 * j + 1] self.tree[i - 1][j] except IndexError: try: self.tree[i][2 * j] self.tree[i - 1][j] except IndexError: continue else: if self.tree[i][2 * j] < self.tree[i - 1][j]: q.enqueue(self.tree[i][2 * j]) del self.tree[i][2 * j] q.enqueue(self.tree[i - 1][j]) del self.tree[i - 1][j] self.tree[i - 1].insert(j, q.dequeue()) self.tree[i].insert(2 * j, q.dequeue()) else: for k in range(0, self.power - i + 1): try: self.tree[i + k][2 * j + 1] except IndexError: try: self.tree[i + k][2 * j] except IndexError: break else: if self.tree[i + k][2 * j] < self.tree[i + k - 1][j]: q.enqueue(self.tree[i + k][2 * j]) del self.tree[i + k][2 * j] q.enqueue(self.tree[i + k - 1][j]) del self.tree[i + k - 1][j] self.tree[i + k - 1].insert(j, q.dequeue()) self.tree[i + k].insert(2 * j, q.dequeue()) else: if self.tree[i + k][2 * j + 1] < self.tree[ i + k - 1][j] and self.tree[i][ 2 * j + 1] < self.tree[i][2 * j]: q.enqueue(self.tree[i + k][2 * j + 1]) del self.tree[i + k][2 * j + 1] q.enqueue(self.tree[i + k - 1][j]) del self.tree[i + k - 1][j] self.tree[i + k - 1].insert(j, q.dequeue()) self.tree[i + k].insert(2 * j + 1, q.dequeue()) j = 2 * j + 1 elif self.tree[i + k][2 * j] < self.tree[i + k - 1][j]: q.enqueue(self.tree[i + k][2 * j]) del self.tree[i + k][2 * j] q.enqueue(self.tree[i + k - 1][j]) del self.tree[i + k - 1][j] self.tree[i + k - 1].insert(j, q.dequeue()) self.tree[i + k].insert(2 * j, q.dequeue()) j = 2 * j return self.tree
def test_dequeue_empty(self) -> None: """ Tests the Queue.dequeue method on an empty queue. """ q = Queue() self.assertIsNone(q.dequeue())
def test_dequeuing_from_empty_queue_raises_custom_error_message(): queue = Queue() assert_equal(queue.dequeue(), "Error: Queue is empty")
def test_dequeue_size_one(self) -> None: """ Tests the Queue.dequeue method on an empty queue. """ q = Queue([14]) self.assertEqual(14, q.dequeue())