果冻,126字节
“操作员优先?括号?帕,谁需要那个?” -将果冻用于操作员优先级挑战的挑战。
⁾[]i$€Ḥæ%3+\¬œp¹Ḋ€
ǵḟØDO%9µÐṀṪɓœṣ⁹,ṚÑj@¥/
ǵVṾµ1ĿFḟØDḟ”-Lµ?ÐL
5Ḷx@€“]“[”ż⁸j/€,@y³Fɓ³i@€Ṁ’x@“[“]”jÇ
“+_×:%”Œ!Ç€µṾL_L’ỊµÐfV€ṢIS
在线尝试!
输入被视为字符串,例如“ 1 + 2_3×4:5%6”。注意乘法使用“×”代替“ *”,除法使用“:”代替“ /”,减法使用“ _”代替“-”。
工作原理
该程序分为三个部分:生成具有不同运算符优先级的所有表达式,对其求值,以及返回最大值和最小值之间的差。
所有表达式都是使用以下代码生成的:
5Ḷx@€“]“[”ż⁸j/€,@y³Fɓ³i@€Ṁ’x@“[“]”jÇ (4) helper link: returns all outputs given a permutation. Input e.g. "_+:×%"
5Ḷx@€“]“[” - repeat outer brackets to get ["",""],["]","["],["]]","[["],["]]]","[[["],["]]]]","[[[["]
ż⁸j/€ - insert the operations in to get "_","]+[","]]:[[","]]]×[[[","]]]]%[[[["
,@ - turn this into a mapping equivalent to "_"↦"_","+"↦"]+[",":"↦"]]:[[","×"↦"]]]×[[[","%"↦"]]]]%[[[["
y³F - use this mapping to get the right number of outward brackets on each operation. e.g. "1]+[3]]]×[[[4"
ɓ³i@€Ṁ’x@“[“]”j - add the right number of brackets to the end to get e.g."[[[1]+[3]]]×[[[4]]]"
Ç - this calls the link which evaluates the expression
“+_×:%”Œ!Ç€ (5a) main link. Input e.g. "1+3×4"
“+_×:%” - the string "+_×:%"
Œ! - all permutations
Ç€ - apply link (4) to each permutation
对此链接进行了评估(我可能会使用其他结构进行改进):
⁾[]i$€Ḥæ%3+\¬œp¹Ḋ€ (1) Helper link: Outputs a list of expressions within brackets, e.g. "[[[1]+[3]]]×[[[4]]]"↦"[[1]+[3]]","[[4]]"
⁾[]i$€Ḥæ%3 - map "[" to 2, "]" to -2, and any other character to 0.
+\¬ - cumulative sum negated: 1s at characters not in brackets (includes opening brackets), 0s otherwise (includes closing brackets)
œp¹ - partition the input, not including borders, based on the sum to get "[[[1]+[3]]","[[[4]]"
Ḋ€ - remove opening brackets
ǵḟØDO%9µÐṀṪɓœṣ⁹,ṚÑj@¥/ (2) Return the input to this link with one of the expressions from (1) evaluated
ǵVṾµ1ĿFḟØDḟ”-Lµ?ÐL (3) link called from part 1: Evaluates expressions
µ µ µ? - if:
1ĿFḟØDḟ”-L - the input contains no operators within brackets:
VṾ - evaluate this one expression with normal Jelly calculation and return to string
- otherwise:
Ç - evaluate one subexpression using link (2)
ÐL - repeat this until a single output is determined
最大值和最小值之间的差异是使用链接(5)中的代码计算的:
µṾL_L’ỊµÐfV€ṢIS (5b) determine difference between minimum and maximum
µ µÐf - filter out outputs involving division or modulo by 0. Determined with:
ṾL_L’Ị - actual numbers have their unevaled form Ṿ no more than one byte longer than the non-unevaled form.
V€ - evaluate each of these valid numbers to get integers from strings
Ṣ - sort
IS - return the sum of all difference between consecutive elements.
%
在第二个示例中,您好像被视为具有两个不同的优先级。