def ac_3(node, graph, available_colors, neighbours_assigned_colors): test_available_colors = {} need_to_backtrack = False for color in available_colors[node]: test_available_colors = {} for k, v in available_colors.items(): test_available_colors[k] = v test_available_colors[node] = color arc_already_queued = {} queue = Queue() for neighbour in graph.get(node): arc = (neighbour, node) queue.enque(arc) arc_already_queued[arc] = True print arc_already_queued while not queue.is_empty(): arc = queue.deque() arc_already_queued[arc] = False state = remove_inconsistent_values(arc, test_available_colors) removed = state[0] need_to_backtrack = state[1] if removed: for neighbour in graph.get(arc[0]): adj_arc = (neighbour, arc[0]) if not arc_already_queued.get(adj_arc): queue.enque(adj_arc) arc_already_queued[adj_arc] = True if need_to_backtrack: break print 'need to backtrack: ', need_to_backtrack print 'test available colors: ', test_available_colors if not need_to_backtrack: break available_colors = {} for k, v in test_available_colors.items(): available_colors[k] = v neighbours_assigned_colors[node] = True for neighbour in graph.get(node): if not neighbours_assigned_colors.get(neighbour): node = neighbour break counter = 0 for k, v in test_available_colors.items(): if len(test_available_colors.get(k)) == 1: counter += 1 print 'next node ', node print 'available colors: ', available_colors if counter == len(available_colors): return True print 'counter: ', counter ac_3(node, graph, available_colors, neighbours_assigned_colors)
def main(): a = Queue() a.enque(1) a.enque(2) a.enque(3) a.enque(4) print(a) a.deque() print(a) a.deque() print(a)
class TestStringMethods(unittest.TestCase): def setUp(self): self.q = Queue() def test_str(self): self.assertEqual(str(self.q), '') self.q.enque(4) self.assertEqual(str(self.q), '4') def test_enque(self): self.q.enque(4) self.assertEqual(self.q.front.data, 4) self.assertEqual(self.q.back.data, 4) self.q.enque(5) self.assertEqual(self.q.front.data, 4) self.assertEqual(self.q.back.data, 5) def test_deque(self): self.assertEqual(self.q.dequeue(), None) self.q.enque(4) self.assertEqual(self.q.dequeue(), 4) self.assertEqual(self.q.dequeue(), None) self.q.enque(5) self.q.enque(6) self.assertEqual(self.q.dequeue(), 5) self.q.enque(4) self.assertEqual(self.q.dequeue(), 6) self.assertEqual(self.q.dequeue(), 4) def test_len(self): self.q.enque(4) self.assertEqual(len(self.q), 1) self.q.enque('5') self.assertEqual(len(self.q), 2) self.q.enque(None) self.assertEqual(len(self.q), 3)
#coding: UTF-8 from Queue import Queue q = Queue() while True: a = input('please input >') if a == 'end': print("end") break if a == 'push': b = input('please number input >') q.enque(b) if a == 'pop': try: print(q.deque()) except: print("empty") if a == 'head': try: print(q.head()) except: print("empty") if a == 'size': print(q.size())