1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #!/usr/bin/env python
txt = """2000
Monaco
Lyon
2001
Lyon
Marseille
2002
PSG
Monaco
Turin
Lyon
Marseille"""
def parse(txt):
lst = txt.splitlines()
res = []
cur = []
for elmt in lst:
if elmt.isdigit():
if (cur):
res.append(cur)
cur = []
cur.append(int(elmt))
else:
cur.append(elmt)
if (cur):
res.append(cur)
return res
def getPoints(name, lst):
total = 0
for elmt in lst:
if name in elmt:
total += 21 - elmt.index(name)
return total
def getPointsYear(name, year, lst):
for elmt in lst:
if (elmt and elmt[0]==year and (name in elmt)):
return 21 - elmt.index(name)
return 0
def main():
lst = parse(txt)
print lst
print getPoints("Marseille", lst), " pts pour Marseille"
print getPoints("Lyon", lst), " pts pour Lyon"
print getPointsYear("PSG",2002, lst), "pts pour le PSG en 2002"
if __name__ == '__main__':
main() |
Partager