Example #1
0
def solve(maze, start, end):
    pq = pQueue.PQ()
    visitedNodes = []
    startNode = next((x for x in maze if x.nPos == start), None)
    visitedNodes.append(start)
    pq.insert(startNode)

    while pq.isEmpty() != True:
        q = pq.pop()

        if q.nPos == end:
            print("Found end")
            #Lets backtrack and trace until we find the start
            pq.queue.clear()
            path = []
            current = q
            while current.nPos != start:
                path.append(current.nPos)
                current = current.parent
            path.append(start)
            return path

        #Next we iterate through every neighbour node of the current node (maze[q].items())
        for neighbour, weight in maze[q].items():
            if neighbour.nPos not in visitedNodes:
                visitedNodes.append(neighbour.nPos)
                neighbour.parent = q
                pq.insert(neighbour)
Example #2
0
    # 7
    ary = "15 31 47 50 55 75 18 49 10 80".split()
    sort.insertion_sort(ary, 6)

    # 8
    ary = "80 86 11 90 84 45 14 75 85 92".split()
    aux = "80 86 11 90 84 45 14 75 85 92".split()
    MergeSort.bottom_up_merge_sort(ary, aux)

    # 9
    ary = "38 40 93 37 77 28 36 56 89 64 46 88".split()
    QuickSort.parti(ary, 0, len(ary) - 1)
    print " ".join(ary)

    # 10
    pq = PriorityQueue.PQ()
    pq.setLi("Z W R V M E F B D G".split())

    print pq
    pq.delMax()
    print pq
    pq.delMax()
    print pq
    pq.delMax()
    print pq

    # 14
    bst = BST.BST()
    for i, k in enumerate("49 58 16 75 71 43 82 10 40 90".split()):
        bst.put(k.strip(), i)