示例#1
0
    def shortestPath(self, gameState, isEnd):
        """
        find the shortest path from current position to destination defined by the function isEnd
        """
        pos = tuple(gameState.getPacmanPosition())

        # if there is not food left

        visited = []
        q = Queue()
        q.put((pos,0))
        visited.append(pos)
        while not q.empty():
            (pos,dis) =q.get()
            visited.append(pos)
            for (dx,dy) in [(0,1),(0,-1),(1,0),(-1,0)]:
                x = pos[0] + dx
                y = pos[1] + dy
                if (isEnd(x,y)):
                    return dis + 1

                if (not gameState.hasWall(x,y)) and (not (x,y) in visited):
                    q.put(((x,y),dis+1))
        
        raise Exception("should not reach here")
示例#2
0
def solution(s):
    q = Queue(len(s))
    for token in s:
        if token in '{[(':
            q.put(token)
        else:
            if q.empty():
                return 0
            if token == ')':
                if q.get() != '(':
                    return 0
            elif token == '}':
                if q.get() != '{':
                    return 0
            else:
                if q.get() != '[':
                    return 0

    if q.empty():
        return 1
    else:
        return 0
示例#3
0
def solution(S, D):
    assert (len(S) == len(D))
    q = Queue(len(S))

    total = 0
    for size, direction in zip(S, D):
        # print size, direction
        if direction == 1:
            q.put(size)
        else:
            if q.empty():
                total += 1
            else:
                while not q.empty():
                    upstream_size = q.get()
                    if upstream_size > size:
                        q.put(upstream_size)
                        break
                if q.empty():
                    total += 1

    # return total + q.size()
    return total + q.size()
示例#4
0
def solution(s):
    q = Queue(len(s))
    total = 0
    for token in s:
        if q.empty():
            q.put(token)
            total += 1
        else:
            if token > q.peek():
                q.put(token)
                total += 1
            else:
                while not q.empty() and token < q.peek():
                    q.get()
                if q.empty() or token != q.peek():
                    q.put(token)
                    total += 1
    return total
示例#5
0
class DebuggerInterface:
    """
    Provides a simple interface to capture and send 
    messages from/to the debugger vis stdin/stdout.
    """
    def __init__(self, on_receive=None):
        self.send_queue = Queue()
        self.running = False
        self.callback = on_receive

    def start(self):
        if not self.running:
            self.running = True
            run(self._debugger_send_loop)
            self._read_debugger_input()

    def start_nonblocking(self):
        if not self.running:
            self.running = True
            run(self._debugger_send_loop)
            run(self._read_debugger_input)

    def stop(self):
        if self.running:
            self.running = False

    def send(self, message):
        self.send_queue.put(message)

    def _read_debugger_input(self):
        """
        Reads DAP messages sent from the debugger through stdin and calls the
        function passed in as the callback with the message recieved.
        """

        while self.running:
            try:
                content_length = 0
                while self.running:
                    header = stdin.readline()
                    if header:
                        header = header.strip()
                    if not header:
                        break
                    if header.startswith(CONTENT_HEADER):
                        content_length = int(header[len(CONTENT_HEADER):])

                if content_length > 0:
                    total_content = ""
                    while content_length > 0:
                        content = stdin.read(content_length)
                        content_length -= len(content)
                        total_content += content

                    if content_length == 0:
                        message = total_content
                        if self.callback:
                            self.callback(message)

            except Exception as e:
                log("Failure reading stdin: " + str(e))
                log(header)
                log(message)
                log(total_content)
                raise e

    def _debugger_send_loop(self):
        """
        Waits for items to show in the send queue and prints them.
        Blocks until an item is present
        """

        while self.running:
            msg = self.send_queue.get()
            if msg is None:
                return
            else:
                try:
                    stdout.write('Content-Length: {}\r\n\r\n'.format(len(msg)))
                    stdout.write(msg)
                    stdout.flush()
                    log('Sent to Debugger:', msg)
                except Exception as e:
                    log("Failure writing to stdout (normal on exit):" + str(e))