Ejemplo n.º 1
0
def queue_first():
    q = Queue()
    q.print()
    print("Is empty?", q.is_empty())
    for i in range(1, 9, 2):
        print("Enq   ", str(i) + ":  ", end="")
        q.enq(i)
        q.print()
    print("Front:", q.front())
    for i in range(4):
        print("Deq:  ", q.deq(), "- ", end="")
        q.print()
    print("Front:", q.front())
    print("Is empty?", q.is_empty())
Ejemplo n.º 2
0
    def BFS(self):
        # keep a queue for BFS
        queue = Queue()

        # enqueue the start state to the queue as a tuple (state, parent_state)
        self.start_state.set_parent(State(None))
        queue.enqueue(self.start_state)
        self.expanded.append((self.start_state.get_state(), None))

        # keep looping whilst there's still states in the queue
        while queue.is_empty() != True and len(self.visited) <= 1000:
            # dequeue next state and add it to visited
            current_state = queue.front()
            parent_state = queue.dequeue().get_parent()
            children_states = self.get_children_states(current_state,
                                                       parent_state)
            self.visited.append(current_state.get_state())

            # add current states children states to the queue
            for child_state in children_states:
                queue.enqueue(child_state)
                self.expanded.append((child_state.get_state(),
                                      self.last_update(child_state,
                                                       current_state)))

            # check if end state is found
            if current_state.get_state() == self.goal_state.get_state():
                # end state found
                self.path(current_state)
                break
Ejemplo n.º 3
0
 def test_front(self):
     q = Queue()
     q.enqueue(1)
     q.enqueue(2)
     q.enqueue(3)
     r = q.dequeue()
     self.assertEqual(2, q.front())
Ejemplo n.º 4
0
 def show(self):
     current = Queue()
     current.push(self)
     num = 1
     while not current.empty():
         line = []
         next_num = 0
         for i in range(num):
             node = current.front()
             current.pop()
             if node is None:
                 line.append('#')
                 continue
             else:
                 line.append(node.val)
             if node.left is not None:
                 current.push(node.left)
             else:
                 current.push(None)
             next_num += 1
             if node.right is not None:
                 current.push(node.right)
             else:
                 current.push(None)
             next_num += 1
         num = next_num
         print line
Ejemplo n.º 5
0
def enCoder(text, series):
    q = Queue()
    for i in series:
        q.push(i)
    returnString = ""
    for i in text:
        if 'a' <= i <= 'z':
            returnString += chr((ord(i) - ord('a') + q.front()) % 26 +
                                ord('a'))
            q.push(q.pop())
        elif 'A' <= i <= 'Z':
            returnString += chr((ord(i) - ord('A') + q.front()) % 26 +
                                ord('A'))
            q.push(q.pop())
        else:
            returnString += i
    return returnString
Ejemplo n.º 6
0
def queue_test(q_str, op_str):
    q = Queue([])
    i = 0
    for op in op_str:
        if op == "e":
            if i < len(q_str):
                print("Enq   ", q_str[i] + ":  ", end="")
                q.enq(q_str[i])
                q.print()
                i += 1
        elif op == "f":
            print("Front:", q.front())
        elif op == "d":
            print("Deq:  ", q.deq(), "- ", end="")
            q.print()
        elif op == "i":
            print("Is empty?", q.is_empty())
        else:
            print("ERROR: unknown operation", op)
Ejemplo n.º 7
0
#front at index 0
#back at index n - 1
my_queue = []

#UPDATE, ADD
#enqueue
my_queue.append("A")
my_queue.append("B")
my_queue.append("C")

#DELETE
#dequeue
my_queue.pop(0)

#READ 
#front
print(my_queue[0])'''

#CREATE
my_queue = Queue()

my_queue.enqueue("A")
#["A"]
my_queue.enqueue("B")
#["A", "B"]
my_queue.enqueue("C")
#["A", "B", "C"]

print(my_queue.front())

Ejemplo n.º 8
0
print "Checking is_empty and size..."

# TODO: tests here

queue.is_empty()
queue.print_queue()
print "done\n"

###################################

print "Checking front, removeFront..."

# TODO: tests here
queue.removeFront()
queue.front()
queue.print_queue()
print "done\n"

###################################

print "Checking back, addBack..."

# TODO: tests here
queue.removeFront()
queue.addBack()
queue.print_queue()
print "done\n"

###################################
Ejemplo n.º 9
0
 def test_front(self):
     s = Queue(1, 2, 3, "Hello", "World")
     self.assertEqual(s.front(), 1)
Ejemplo n.º 10
0
from Queue import Queue

queue = Queue()

#Insert elements
queue.push(1)
queue.push(3)
queue.push("a")
queue.push(True)

#Get front data
print(queue.front())

#Get back data
print(queue.back())

#Check if is empty
print(queue.empty())

#Print queue
print(queue)

#Remove elements
print(queue.pop())
print(queue.pop())
print(queue.pop())
print(queue.pop())


#Check again if queue is empty
print(queue.empty())
Ejemplo n.º 11
0
assert queue.size() == 0

queue.print_queue()
print "done\n"

###################################

print "Checking back, addBack..."

queue.addBack(0)
queue.addBack(1)
assert queue.back() == 1

queue.print_queue()
print "done\n"

###################################

print "Checking front, removeFront..."

assert queue.front() == 0
queue.removeFront()
assert queue.front() == 1

queue.print_queue()
print "done\n"

###################################

queue.print_queue()
print "All checks passed"