view optim.py @ 2:3632502b8af1 draft

parseinput: replace lists with namedtuplese It improves readability of the code.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 10 Mar 2015 19:51:14 -0400
parents 15944d95f399
children c683f80bc858
line wrap: on
line source

#!/usr/bin/env python

from collections import namedtuple

Machine = namedtuple("Machine", ["day", "buy", "sell", "profit"])
Case = namedtuple("Case", ["machines", "days", "capital"])

def parseinput(fname):
    """
    Parse the input file, forget about input validation
    """
    cases = []
    with open(fname) as f:
        while True:
            header = [int(x) for x in f.readline().split()]
            if header == [0, 0, 0]:
                return cases
            N = header[0]
            case = Case([], header[1], header[2])
            for i in range(0, N):
                machine = Machine(*[int(x) for x in f.readline().split()])
                case.machines.append(machine)
            cases.append(case)

def main():
    cases = parseinput("input.txt")
    for case in cases:
        print "Next case:", case["header"]
        for machine in case["machines"]:
            print machine

if __name__ == "__main__":
    main()