以一个字符串作为输入,执行字符串中所有数字的加/减,并输出这些操作的和。
Input: r5e6o9mm!/3708dvc
Process: (5+6) + (6-9) + (9+3) + (3+7) + (7+0) + (0-8) + (8-5)
Output: 32发布于 2018-07-06 11:23:10
发布于 2018-07-06 08:45:25
将输入作为字符数组。
s=>s.map(c=>1/c?r+=p*(p=c*2&2,n=n||c,c):0,n=p=r=0)|r+p*ns => // given the input array s[]
s.map(c => // for each character c in s[]:
1 / c ? // if c is a digit:
r += // update r:
p * ( // p = either 0 or 2 (always 0 on the 1st iteration)
p = c * 2 & 2, // p = 0 if c is even, 2 if c is odd
n = n || c, // if n is still equal to 0 (as an integer), set it to c
c // compute p * c
) // add the result to r
: // else:
0, // do nothing
n = p = r = 0 // n = first digit, p = previous digit, r = result
) // end of map()
| r + p * n // compute the last operation with the 1st digit and add it to rhttps://codegolf.stackexchange.com/questions/168039
复制相似问题