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")
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)
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')
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)
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")))
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))
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)), }
def inc(n: pygg.Value) -> pygg.Value: i = int(n.as_str()) return gg.str_value(str(i + 1))