05AB1E、166の 147 141 139 135 バイト
„byK“¡×€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿áÓÁÏ“#vyN:}'tK'…§¦'+T«:'°¡„ *т«©:.•1×j›o!ĆÖ•3ôŽ9oS:'y®¨:©“‰´Øè„Æ€ººß“'tK#UX¡εð¡õK2ôJ.EO}®áX"+-**/"S:Sðì.ιJ.E
あまりにも長い..ここから下にゴルフしようとします。
@Emignaのおかげで-4バイト。@JoKingの
おかげで-2バイト。
オンラインそれを試してみたり、すべてのテストケースを確認してください。
説明:
„byK # Remove "by" from the (implicit) input-string
“¡×€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿áÓÁÏ“
# Push string "zero one two three four five six seven eight nine ten eleven twelve"
# # Split by spaces
v } # Loop over each of these items:
yN: # Replace the item with its 0-indexed index
'tK '# Remove all "t"
'…§¦ '# Push string "teen", and remove the first character: "een"
'+ '# Push string "+"
T« # Append 10: "+10"
: # Replace all "een" with "+10"
'°¡ '# Push string "hundred"
„ * # Push string " *"
т« # Append 100: " *100"
© # Store it in the register (without popping)
: # Replace all "hundred" with " *100"
.•4º»Ÿć'Rþн• '# Push string "wenhirforfif"
3ô # Split the string into parts of size 3: ["wen","hir","for","fif"]
Ž9o # Push integer 2345
S # Split to digits: [2,3,4,5]
: # Replace each
'y '# Push string "y"
® # Push the " *100" from the register
¨ # Remove the last character: " *10"
: # Replace all "y" with " *10"
© # Save the current string in the register (without popping)
“‰´Øè„Æ€ººß“ # Push string "plus minus times time divided"
'tK '# Remove all "t": "plus minus imes ime divided"
# # Split by spaces: ["plus","minus","imes","ime","divided"]
U # Pop and save it in variable `X`
X # And push variable `X` back again
¡ # Split the string by those operator-strings
ε } # Map each substring to:
ð¡ # Split by spaces (NOTE: cannot be `#`; if the string contains no
# spaces, `#` remains string, whereas `ð¡` wraps it in a list)
õK # Remove empty strings from the list
2ô # Split the list into parts of two
J # Join each pair together
.E # Evaluate each as a Python `eval` expression
O # Sum them
® # Put the string from the register to the stack again
á # Remove everything except for letters
X # Push variable `X`: ["plus","minus","imes","ime","divided"]
"+-**/" # Push string "+-**/"
S # Split to characters: ["+","-","*","*","/"]
: # Replace each
S # Split the string of operators to loose characters
ðì # Prepend a space before each
.ι # Interweave all sums with these operator-characters
J # Join everything together to a single string
.E # Evaluate each as a Python `eval` expression (and output implicitly)
私のこの05AB1Eヒントを参照してください(セクションがどのように辞書を使用するには?、どのように辞書の圧縮文字列への一部ではありません?、そしてどのように大きな整数を圧縮する?)どのように理解するために“¡×€µ‚•„í†ìˆÈŒšï¿Ÿ¯¥Š—¿áÓÁÏ“、'…§、'°¡、.•4º»Ÿć'Rþн•、Ž9o、と“‰´Øè„Æ€ººß“仕事を。
ステップバイステップの例:
- 入力:
two hundred twenty two divided by two times minus fifty seven plus three hundred eighteen minus minus ten
- ステップ1:「by」を削除します。
two hundred twenty two divided two times minus fifty seven plus three hundred eighteen minus minus ten
- ステップ2:正しい数値で「ゼロ」から「12」に変換します。
2 hundred twenty 2 divided 2 times minus fifty 7 plus 3 hundred 8een minus minus 10
- ステップ3:すべての「t」を削除します。
2 hundred weny 2 divided 2 imes minus fify 7 plus 3 hundred 8een minus minus 10
- ステップ4:すべての「een」を「+10」に置き換えます。
2 hundred weny 2 divided 2 imes minus fify 7 plus 3 hundred 8+10 minus minus 10
- ステップ5:すべての「百」を「* 100」に置き換えます。
2 *100 weny 2 divided 2 imes minus fify 7 plus 3 *100 8+10 minus minus 10
- ステップ6:すべての["wen"、 "hir"、 "for"、 "fif"]を正しい数字に置き換えます。
2 *100 2y 2 divided 2 imes minus 5y 7 plus 3 *100 8+10 minus minus 10
- ステップ7:すべての「y」を「* 10」に置き換えます。
2 *100 2 *10 2 divided 2 imes minus 5 *10 7 plus 3 *100 8+10 minus minus 10
- ステップ8:["plus"、 "minus"、 "ime"、 "imes"、 "divided"]で分割:
["2 *100 2 *10 2 "," 2 "," "," 5 *10 7 "," 3 *100 8+10 "," "," 10"]
- ステップ9:スペースでそれぞれを分割します。
[["2","","*100","2","*10","2",""],["","","2",""],["",""],["","5","*10","7",""],["","3","","*100","8+10",""],["",""],["","10"]]
- ステップ10:空のアイテムを削除します。
[["2","*100","2","*10","2"],["2"],[],["5","*10","7"],["3","*100","8+10"],[],["10"]]
- ステップ11:サイズ2の部分に分割して結合します。
[["2*100","2*10","2"],["2"],"",["5*10","7"],["3*100","8+10"],"",["10"]]
- ステップ12:
eval各Python :[[200,20,2],[2],"",[50,7],[300,18],"",[10]]
- ステップ13:それぞれを合計します。
[222,2,"",57,318,"",10]
- ステップ14:レジスタから文字列を再プッシュし、文字を除くすべてを削除します。
dividedimesminusplusminusminus
- ステップ15:「プラス」、「マイナス」、「imes」、「ime」、「分割」を演算子文字に置き換え、スペースを先頭に追加します。
[" /"," *"," -"," +"," -"," -"]
- ステップ16:両方を織り交ぜて結合します。
222 /2 * -57 +318 - -10
- 出力: Python
eval文字列、および暗黙的に出力:-5999.0