すべてのチャレンジ
難しい 1件解決済み 正答率100.0%

Mini Stack-Language Interpreter

Evaluate a small stack language.

このチャレンジで学べること

  • Tokenize a small language
  • Execute instructions with a stack
  • Report errors for invalid programs

問題

翻訳を表示

Evaluate a small stack language. The stack starts empty; integers push values onto its top. Return the final stack from bottom to top. Tokens are separated by spaces and command names are case-insensitive.

Arithmetic +, -, *, / pops the top value as right operand and the next as left operand, then pushes left operator right. Division truncates toward zero. DUP copies the top value; DROP removes it; SWAP exchanges the top two; OVER copies the second-from-top value onto the top.

Define a word with : name body ;. A name contains only letters. The nonempty body may contain integers, built-in commands, and previously defined words. Definitions do not execute their bodies. Redefining a name affects later uses, but older definitions keep the meanings captured when they were defined. A definition may use the previous version of its own name. Nested definitions, forward references, and recursive calls are not allowed.

Every test program is valid: no unknown word, stack underflow, division by zero, or overflow occurs. You do not need to report errors.

Implement mini_stack_language_interpreter(program).

Input

program: The complete valid program.

Output

Return the final stack in bottom-to-top order.

制約

0…10,000 tokens. At most 100,000 primitive tokens across all expanded definitions and executed code. Values and intermediate arithmetic fit signed 64-bit. Integer literals use optional minus followed by digits. Tokens : and ; are separate. Built-in names cannot be redefined; user words can be redefined. Empty input returns an empty stack.

例

例 1
入力:
program = ": square dup * ; 5 square 3 +"

期待される出力:
expected = [28]
入力:
std::string program = ": square dup * ; 5 square 3 +";

期待される出力:
std::vector<long long> expected = {28};
入力:
string program = ": square dup * ; 5 square 3 +";

期待される出力:
long[] expected = new long[] {28};

解説

Define square without executing it. Push 5, duplicate it, multiply to get 25, then push 3 and add to obtain 28.

例 2
入力:
program = "-7 2 / 9 over"

期待される出力:
expected = [-3, 9, -3]
入力:
std::string program = "-7 2 / 9 over";

期待される出力:
std::vector<long long> expected = {-3, 9, -3};
入力:
string program = "-7 2 / 9 over";

期待される出力:
long[] expected = new long[] {-3, 9, -3};

解説

−7 divided by 2 truncates toward zero to −3. Push 9, then OVER copies the second-from-top value, −3.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Treat the program as two intertwined tasks: executing the current stack and remembering what each word meant when it was defined.

ヒント 2

Store each user-defined word as a sequence of resolved operations, so later redefinitions do not change earlier bodies.

ヒント 3

Watch for definition-time parsing details: capture words by their current meaning, ignore execution inside definitions, and handle case-insensitive built-ins with truncating division.

目安の計算量

O(T) time, O(T) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 Н"Никита "Programmer" Python 1,390 ms - 4/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が1件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。