#31811: Python reduce解(超簡單)


seer852741@gmail.com (St418)

學校 : 國立中央大學
編號 : 170226
來源 : [114.24.161.39]
最後登入時間 :
2024-01-05 19:57:56
c290. APCS 2017-0304-1秘密差 -- 2017年3月APCS | From: [220.129.81.29] | 發表日期 : 2022-08-21 00:12

from functools import reduce
print(abs(reduce(lambda x, y: int(y) - x, input(), 0)))
翻譯一下
diff = 0
for n in input():
    diff = int(n) - diff
print(abs(diff))
例:1234
|1 - 2 + 3 - 4| = 2
可以理解成
|4 - (3 - (2 - 1)))| = 2
符合reduce的定義
 
ZeroJudge Forum