ゼリー、126バイト
「演算子の優先順位?括弧?Pah、誰がそれを必要としますか?」-オペレーターの優先課題にゼリーを使用することの課題。
⁾[]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」などの文字列として取得されます。乗算では「*」ではなく「×」が使用され、除算では「/」ではなく「:」が使用され、減算では「-」ではなく「_」が使用されます。
仕組み
プログラムは3つの部分に分かれています。異なる演算子優先順位のすべての式を生成し、それらを評価し、最大値と最小値の差を返します。
すべての式はコードで生成されます:
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.
%
2番目の例では、2つの異なる優先順位を持つものとして扱っているように見えます。