b.py (558B)
1 import fileinput 2 from functools import reduce 3 from operator import mul 4 5 with fileinput.input() as lines: 6 all = list(lines) 7 a = [line[:-1] for line in all[:-1]] 8 op = all[-1][:-1] 9 10 def sol(a, j, k): 11 nums = [] 12 i = k-2 13 while i >= j: 14 n = 0 15 for r in a: 16 if r[i] != ' ': 17 n = 10*n + int(r[i]) 18 nums.append(n) 19 i -= 1 20 return sum(nums) if op[j] == '+' else reduce(mul, nums, 1) 21 22 j = 0 23 s = 0 24 while j < len(op): 25 k = j+1 26 while k < len(op) and op[k] == ' ': 27 k += 1 28 if k >= len(op): 29 k = max(len(r) for r in a)+1 30 s += sol(a, j, k) 31 j = k 32 print(s)