Ejemplo n.º 1
0
def add_str(a: pygg.Value, b: pygg.Value) -> pygg.Output:
    ap = a.path()
    bp = b.path()
    assert ap is not None
    assert bp is not None
    sub.check_call([gg.bin("add_str").path(), ap, bp])
    return gg.file_value("out")
Ejemplo n.º 2
0
def add_str(a: pygg.Value, b: pygg.Value) -> pygg.Output:
    ai = int(a.as_str())
    bi = int(b.as_str())
    path = 'random'
    with open(path, 'w') as f:
        f.write(str(ai + bi))
    return gg.file_value(path)
Ejemplo n.º 3
0
def merge(a: pygg.Value, b: pygg.Value) -> pygg.Value:
    al = [int(l.strip()) for l in open(a.path(), 'r').readlines()]
    bl = [int(l.strip()) for l in open(b.path(), 'r').readlines()]
    ai = 0
    bi = 0
    with open('c', 'w') as f:
        while ai < len(al) and bi < len(bl):
            if al[ai] < bl[bi]:
                f.writelines([str(al[ai]), "\n"])
                ai += 1
            else:
                f.writelines([str(bl[bi]), "\n"])
                bi += 1
        f.writelines(str(x) + "\n" for x in al[ai:])
        f.writelines(str(x) + "\n" for x in bl[bi:])
    return gg.file_value('c')
Ejemplo n.º 4
0
def fib_(n: pygg.Value) -> pygg.Output:
    i = int(n.as_str())
    if i < 2:
        return gg.str_value(str(i))
    else:
        s = gg.thunk(split, n)
        a = gg.thunk(fib_, s)
        b = gg.thunk(fib_, s["n2"])
        return gg.thunk(add_str, a, b)
Ejemplo n.º 5
0
def sort(a: pygg.Value) -> pygg.Output:
    al = open(a.path(), 'r').readlines()
    if len(al) < 2:
        with open("out", "w") as f:
            f.writelines(al)
        return gg.file_value("out")
    else:
        with open("1", "w") as f:
            f.writelines(al[:len(al) // 2])
        with open("2", "w") as f:
            f.writelines(al[len(al) // 2:])
        return gg.thunk(merge, gg.thunk(sort, gg.file_value("1")),
                        gg.thunk(sort, gg.file_value("2")))
Ejemplo n.º 6
0
def add_str(a: pygg.Value, b: pygg.Value) -> pygg.Output:
    ai = int(a.as_str())
    bi = int(b.as_str())
    return gg.str_value(str(ai + bi))
Ejemplo n.º 7
0
def split(n: pygg.Value) -> pygg.OutputDict:
    i = int(n.as_str())
    return {
        "n1": gg.str_value(str(i - 1)),
        "n2": gg.str_value(str(i - 2)),
    }
Ejemplo n.º 8
0
def inc(n: pygg.Value) -> pygg.Value:
    i = int(n.as_str())
    return gg.str_value(str(i + 1))