Skip to content

Instantly share code, notes, and snippets.

@MarcoBuster
Last active March 15, 2024 11:17
Show Gist options
  • Save MarcoBuster/ae0b258528663804b271d16bb5c6ce24 to your computer and use it in GitHub Desktop.
Save MarcoBuster/ae0b258528663804b271d16bb5c6ce24 to your computer and use it in GitHub Desktop.
Converti le tabelle testuali del corso di Ricerca Operativa del Professore Righini in parametri MathProg
import argparse
TAB_SIZE = 1
parser = argparse.ArgumentParser(
description="Converti le tabelle testuali del corso di Ricerca Operativa del Professore Righini in parametri MathProg",
)
parser.add_argument(
"name",
metavar="name",
type=str,
help="nome del parametro da creare",
)
parser.add_argument(
'-x',
'--crop-x',
dest='x_crop',
default=0, type=int,
help='numero di colonne da saltare'
)
parser.add_argument(
'-y',
'--crop-y',
dest='y_crop',
default=0, type=int,
help='numero di righe da saltare'
)
args = parser.parse_args()
lines: list[str] = []
while True:
line: str | None = input()
if not line:
break
lines.append(line.lstrip().rstrip())
matrix: list[list[str]] = [
[col.rstrip().lstrip() for col in
list(filter(''.__ne__, line.split(" ")))]
for line in lines
]
numbers: list[list[float]] = []
for row in matrix:
number_row: list[float] = []
for col in row:
try:
n: float = float(col)
except ValueError:
continue
number_row.append(n)
if len(number_row) > 0:
numbers.append(number_row)
shape_x: int = len(numbers[0])
shape_y: int = len(numbers)
text: str = f"param {args.name}{': ' + ' '.join([str(i+1) for i in range(shape_x-args.x_crop)]) if shape_x > 0 else ''} :=\n"
for i, row in enumerate(numbers):
if i < args.y_crop:
continue
text += f"{i+1-args.y_crop}\t".expandtabs(TAB_SIZE)
for j, el in enumerate(row):
if j < args.x_crop:
continue
text += f"\t{int(el) if int(el) == el else el}".expandtabs(TAB_SIZE)
text += "\n"
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment