上一次,当我试图想出一些简单的东西,而不是复制的时候,结果却太难了。因此,希望这一次确实是新来者可以尝试的东西。
带有整数/小数的数组/列表。(或者表示带有整数/小数的数组的字符串。)
循环遍历这些数字,并按以下顺序应用以下五个数学操作数:
+);−);*或×或·);/或÷);^或**)。(注:括号之间的符号只是作为澄清而添加的。如果编程语言在数学运算中使用了与示例完全不同的符号,那么这当然是完全可以接受的。)
一直延续到列表的末尾,然后给出总数的结果。
n ^ 0)的幂运算应该导致1(这也适用于0 ^ 0 = 1)。n / 0)的测试用例,所以您不必担心边缘用例。[1,2,3,4,5] -> 0
-> 1 + 2 = 3
-> 3 - 3 = 0
-> 0 * 4 = 0
-> 0 / 5 = 0
[5,12,23,2,4,4,2,6,7] -> 539
-> 5 + 12 = 17
-> 17 - 23 = -6
-> -6 * 2 = -12
-> -12 / 4 = -3
-> -3 ^ 4 = 81
-> 81 + 2 = 83
-> 83 - 6 = 77
-> 77 * 7 -> 539
[-8,50,3,3,-123,4,17,99,13] -> -1055.356...
-> -8 + 50 = 42
-> 42 - 3 = 39
-> 39 * 3 = 117
-> 117 / -123 = -0.9512...
-> -0.9512... ^ 4 = 0.818...
-> 0.818... + 17 = 17.818...
-> 17.818... - 99 -> -81.181...
-> -81.181... * 13 = -1055.356...
[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2] -> 256
-> 2 + 2 = 4
-> 4 - 2 = 2
-> 2 * 2 = 4
-> 4 / 2 = 2
-> 2 ^ 2 = 4
-> 4 + 2 = 6
-> 6 - 2 = 4
-> 4 * 2 = 8
-> 8 / 2 = 4
-> 4 ^ 2 = 16
-> 16 + 2 = 18
-> 18 - 2 = 16
-> 16 * 2 = 32
-> 32 / 2 = 16
-> 16 ^ 2 = 256
[1,0,1,0,1,0] -> 1
-> 1 + 0 = 1
-> 1 - 1 = 0
-> 0 * 0 = 0
-> 0 / 1 = 0
-> 0 ^ 0 = 1
[-9,-8,-1] -> -16
-> -9 + -8 = -17
-> -17 - -1 = -16
[0,-3] -> -3
-> 0 + -3 = -3
[-99] -> -99发布于 2016-06-13 15:35:19
“+_×÷*”ṁṖ⁸żFV在网上试试!或验证所有测试用例.
“+_×÷*”ṁṖ⁸żFV Main link. Argument: A (list of integers)
“+_×÷*” Yield the list of operations as a string.
Ṗ Yield A popped, i.e., with its last element removed.
ṁ Mold; reshape the string as popped A.
This repeats the characters of the string until it contains
length(A)-1 characters.
⁸ż Zipwith; pairs the integers of A with the corresponding characters.
F Flatten the result.
V Eval the resulting Jelly code.
Jelly always evaluates left-to-right (with blatant disregard towards
the order of operations), so this returns the desired result.发布于 2016-06-13 07:11:24
a=>a.reduce((c,d,e)=>[c**d,c+d,c-d,c*d,c/d][e%5])由于Dom Hastings保存了9个字节,由于Leaky又保存了6个字节
使用新的指数运算符。
发布于 2016-06-13 18:18:49
foldl(flip id)0.zipWith flip((+):cycle[(+),(-),(*),(/),(**)])在列表中创建一系列转换,就像在加1,加2,减去3。中一样,从两个添加开始,因为我们从折叠中的0开始。接下来,我们执行我所称的列表应用程序折叠,或foldl (flip id),它应用了一系列的同态。这将以零开始,添加初始值,然后执行上述所有计算的转换,以获得最终结果。
请注意,(翻转id)与(\x >y)相同,只是较短。
样本使用情况:
f = foldl(flip id)0.zipWith flip((+):cycle[(+),(-),(*),(/),(**)])
f [1,2,3,4,5] -- Is 0.0https://codegolf.stackexchange.com/questions/82773
复制相似问题