tot = 0 with open('pe13.txt') as file: lines = file.readlines() lines = [x.strip() for x in lines] for x in range(0, len(lines)): line = lines[x] tot += int(line) tot = str(tot) tot = tot[:10] print(tot)
Above is what I first did to solve the problem. Below is how I refined my solution:
tot = 0lines = [] with open('pe13.txt') as file: lines = file.readlines() lines = [x.strip() for x in lines] lines = list(map(int, lines)) tot = str(sum(lines)) tot = tot[:10] print(tot)
and even more:
with open('pe13.txt') as file: lines = file.readlines() lines = [x.strip() for x in lines] lines = list(map(int, lines)) print(str(sum(lines))[:10])
Quite compact if I may say so.
/Ludvig
No comments:
Post a Comment