23 lines
1014 B
Python
23 lines
1014 B
Python
|
def col_all_zeros(ls, i, al=0):
|
||
|
return all(x[i] == al for x in ls)
|
||
|
def row_all_zeros(ls, i, al=0):
|
||
|
return all(x == al for x in ls[i])
|
||
|
def matrix(name, ls, default=0):
|
||
|
nonzero_cols = [i+1 for i in range(len(ls[0])) if not col_all_zeros(ls, i, default)]
|
||
|
nonzero_rows = [i+1 for i in range(len(ls)) if not row_all_zeros(ls, i, default)]
|
||
|
res = ""
|
||
|
for r in nonzero_rows:
|
||
|
res += "\n{:2d}".format(r)
|
||
|
for c in nonzero_cols:
|
||
|
res += " {:2d}".format(ls[r-1][c-1])
|
||
|
return param(name, res, " : " + " ".join("{:2d}".format(x) for x in nonzero_cols))
|
||
|
|
||
|
def dict(name, thing, default=None):
|
||
|
fmt_key = lambda k: " ".join((str(x+1) for x in k)) if type(k) == tuple else k+1
|
||
|
return param(name, ", ".join(["{} {}".format(fmt_key(k), v) for k,v in thing.items() if v != default]))
|
||
|
def param(name, val, middle=""):
|
||
|
val = str(val)
|
||
|
if "\n" in val:
|
||
|
val = val.replace("\n", "\n" + " " * (len(name) + 6))
|
||
|
return "param {}{} := {};".format(name, middle, val)
|