Befunge-93、28の 27 19の 18バイト
ASCII値が9の倍数であることを確認した場合、PyFungeの答えから、欠落している数字の特別なチェックは不要であることが示されたMistah Figginsにクレジットする必要があります。
さらに、文字を数値に完全に変換する必要はなく、単純に3を減算して9を基準とする値を取得できることを示したJo Kingに感謝します(ASCII 0
マイナス3は45、9の倍数) 。
3_v#`0:~--
%9_@.+9
オンラインでお試しください!
これが機能する*
ためには、欠落している数字に文字を使用する必要があります(他にも機能する可能性のあるものがありますが、それが最も優れています)。
9
欠落している数字が0または9の可能性がある場合に出力します。
説明
3 Push 3 onto the stack.
_ Since this is non-zero, drop the 3 and branch left.
3 Now executing right to left, push 3 again.
-- Negate twice, leaving the value unchanged as our starting sum.
~ Now we start the main loop, reading a character from stdin.
`0: Duplicate and check if it's > 0, i.e. not end-of-stream.
_ # If so, continue to the left.
3 - Subtract 3 to make the number relative to base 9.
- Then subtract it from the total, and repeat the loop.
v Once we reach the end of the input, we go down.
_ Drop the EOF character from the stack and go left.
%9 +9 Mod the total with 9, and add 9.
@. Output the result and exit.
基本的に、すべての数字の合計に1桁あたり45を加えて計算します(9で変更すると最終的にキャンセルされます)。この合計は3(開始合計)から差し引かれ、さらに39が欠落している数字(ASCII *
-3)から差し引かれます。繰り返しますが、3マイナス39は9の倍数であるため、9で変更するとキャンセルされます。
最終的に、すべての桁の負の合計、mod 9、プラス9を計算しています。つまり、
9 - (digitsum % 9)
そして、それは私たちに欠けている数字を与えてくれます。
0
か?何について[0, 9]
(2つの数字の配列またはリスト)?