Exemplo n.º 1
0
def write1D(a):
    """
    Write array a to sys.stdout.  First write its length. bool objects
    are written as 0 and 1, not False and True.
    """
    length = len(a)
    stdio.writeln(length)
    for i in range(length):
        # stdio.writef('%9.5f ', a[i])
        element = a[i]
        if isinstance(element, bool):
            if element == True:
                stdio.write(1)
            else:
                stdio.write(0) 
        else:
            stdio.write(element)
        stdio.write(' ')
    stdio.writeln()
Exemplo n.º 2
0
def write2D(a):
    """
    Write two-dimensional array a to sys.stdout.  First write its
    dimensions. bool objects are written as 0 and 1, not False and True.
    """
    rowCount = len(a)
    colCount = len(a[0])
    stdio.writeln(str(rowCount) + ' ' + str(colCount))
    for row in range(rowCount):
        for col in range(colCount):
            #stdio.writef('%9.5f ', a[row][col])
            element = a[row][col]
            if isinstance(element, bool):
                if element == True:
                    stdio.write(1)
                else:
                    stdio.write(0)
            else:
                stdio.write(element)
            stdio.write(' ')
        stdio.writeln()
Exemplo n.º 3
0
#!/usr/bin/env python3
from itu.algs4.sorting import merge
from itu.algs4.stdlib import stdio
"""
Reads a list of integers from standard input.
Then prints it in sorted order.
"""
L = stdio.readAllInts()

merge.sort(L)

if len(L) > 0:
    stdio.write(L[0])
for i in range(1, len(L)):
    stdio.write(" ")
    stdio.write(L[i])
stdio.writeln()
Exemplo n.º 4
0
        x = v
        while self._dist_to[x] != 0:
            path.push(x)
            x = self._edgeTo[x]
        path.push(x)
        return path


if __name__ == "__main__":
    import sys
    from itu.algs4.stdlib import stdio
    from itu.algs4.graphs.graph import Graph
    from itu.algs4.stdlib.instream import InStream

    In = InStream(sys.argv[1])
    G = Graph.from_stream(In)
    s = int(sys.argv[2])
    bfs = BreadthFirstPath(G, s)

    for v in range(G.V()):
        if bfs.has_path_to(v):
            stdio.writef("%d to %d (%d):  ", s, v, bfs.dist_to(v))
            for x in bfs.path_to(v):
                if x == s:
                    stdio.write(x)
                else:
                    stdio.writef("-%i", x)
            stdio.writeln()
        else:
            stdio.writef("%d to %d (-):  not connected\n", s, v)
Exemplo n.º 5
0
def _regressionTest():
    """Perform regression testing."""

    clear()

    setPenRadius(0.5)
    setPenColor(ORANGE)
    point(0.5, 0.5)
    show(0.0)

    setPenRadius(0.25)
    setPenColor(BLUE)
    point(0.5, 0.5)
    show(0.0)

    setPenRadius(0.02)
    setPenColor(RED)
    point(0.25, 0.25)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(GREEN)
    point(0.25, 0.25)
    show(0.0)

    setPenRadius(0)
    setPenColor(BLACK)
    point(0.25, 0.25)
    show(0.0)

    setPenRadius(0.1)
    setPenColor(RED)
    point(0.75, 0.75)
    show(0.0)

    setPenRadius(0)
    setPenColor(CYAN)
    for i in range(0, 100):
        point(i / 512.0, 0.5)
        point(0.5, i / 512.0)
    show(0.0)

    setPenRadius(0)
    setPenColor(MAGENTA)
    line(0.1, 0.1, 0.3, 0.3)
    line(0.1, 0.2, 0.3, 0.2)
    line(0.2, 0.1, 0.2, 0.3)
    show(0.0)

    setPenRadius(0.05)
    setPenColor(MAGENTA)
    line(0.7, 0.5, 0.8, 0.9)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(YELLOW)
    circle(0.75, 0.25, 0.2)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(YELLOW)
    filledCircle(0.75, 0.25, 0.1)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(PINK)
    rectangle(0.25, 0.75, 0.1, 0.2)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(PINK)
    filledRectangle(0.25, 0.75, 0.05, 0.1)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(DARK_RED)
    square(0.5, 0.5, 0.1)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(DARK_RED)
    filledSquare(0.5, 0.5, 0.05)
    show(0.0)

    setPenRadius(0.01)
    setPenColor(DARK_BLUE)
    polygon([0.4, 0.5, 0.6], [0.7, 0.8, 0.7])
    show(0.0)

    setPenRadius(0.01)
    setPenColor(DARK_GREEN)
    setFontSize(24)
    text(0.2, 0.4, "hello, world")
    show(0.0)

    # import picture as p
    # pic = p.Picture('saveIcon.png')
    # picture(pic, .5, .85)
    # show(0.0)

    # Test handling of mouse and keyboard events.
    setPenColor(BLACK)
    from itu.algs4.stdlib import stdio

    stdio.writeln("Left click with the mouse or type a key")
    while True:
        if mousePressed():
            filledCircle(mouseX(), mouseY(), 0.02)
        if hasNextKeyTyped():
            stdio.write(nextKeyTyped())
        show(0.0)

    # Never get here.
    show()
Exemplo n.º 6
0
def show(array):
    stdio.write(" ".join(array))
Exemplo n.º 7
0
#!/usr/bin/env python3
import sys

from itu.algs4.fundamentals.stack import Stack
from itu.algs4.stdlib import stdio

if len(sys.argv) > 1:
    try:
        sys.stdin = open(sys.argv[1])
    except IOError:
        print("File not found, using standard input instead")

stack: Stack[str] = Stack()
while not stdio.isEmpty():
    item = stdio.readString()
    if not item == "-":
        stack.push(item)
    elif not stack.is_empty():
        stdio.write(stack.pop() + " ")

stdio.writef("(%i left on stack)\n", stack.size())
Exemplo n.º 8
0
        return path

    def _validateVertex(self, v):
        # throw an ValueError unless 0 <= v < V
        V = len(self._marked)
        if v < 0 or v >= V:
            raise ValueError("vertex {} is not between 0 and {}".format(
                v, V - 1))


if __name__ == "__main__":
    from itu.algs4.stdlib import stdio
    from itu.algs4.graphs.graph import Graph
    from itu.algs4.stdlib.instream import InStream
    import sys

    In = InStream(sys.argv[1])
    G = Graph.from_stream(In)
    s = int(sys.argv[2])
    dfs = DepthFirstPaths(G, s)

    for v in range(G.V()):
        if dfs.has_path_to(v):
            stdio.writef("%d to %d:  ", s, v)
            for x in dfs.path_to(v):
                if x == s: stdio.write(x)
                else: stdio.writef("-%i", x)
            stdio.writeln()
        else:
            stdio.writef("%d to %d:  not connected\n", s, v)
Exemplo n.º 9
0
#!/usr/bin/env python3
from itu.algs4.stdlib import stdio

stdio.write("Hello World!\n")