Upgrade to Pro — share decks privately, control downloads, hide ads and more …

四則演算の計算結果を返すプログラムを書きながら学習とは何かについて考える

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 四則演算の計算結果を返すプログラムを書きながら学習とは何かについて考える

LT資料

Avatar for YuheiNakasaka

YuheiNakasaka

June 03, 2026

More Decks by YuheiNakasaka

Other Decks in Technology

Transcript

  1. 条件 文字列 s は、非負整数・空白・演算子 + - * / だけからなる有効な式である s

    を評価して結果を返せ * / は通常通り優先し、整数除算は 0 方向に切り捨てる 1 <= s.length <= 3 * 10^5 eval などの式評価関数は禁止 https://leetcode.com/problems/basic-calculator-ii/ 4
  2. BNF(バッカスナウア)記法とは Backus–Naur Form の略 記号の書き換え規則を用いて文字列の構造を定義する仕組み プログラミング言語やプロトコルの構文定義で使われる <expr> ::= <term> [

    ('+'|'-') <term> ]* <term> ::= <number> [ ('*'|'/') <number> ]* <number> ::= '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' https://en.wikipedia.org/wiki/Backus–Naur_form 8
  3. 逆ポーランド記法とは 演算子を、対応するオペランドの 後ろ に置く記法 1 + 2 * 3 は

    1 2 3 * + 左から読み、数値はスタックに積み、演算子が来たら計算する 1 2 3 * + push 1 push 2 push 3 * => 2 * 3 = 6 + => 1 + 6 = 7 https://en.wikipedia.org/wiki/Reverse_Polish_notation 12
  4. 模範解答の考え方 * / だけ、その場で計算する + n は n をスタックに積む /

    - n は -n をスタックに積む * n は直前の値を取り出して掛ける / / n は直前の値を取り出して割る 最後にスタックを合計する 3 + 2 * 2 stack: [3] stack: [3, 2] stack: [3, 4] sum : 7 20
  5. 参考資料 LeetCode 227. Basic Calculator II: https://leetcode.com/problems/basic-calculator-ii/description/ Recursive descent parser:

    https://en.wikipedia.org/wiki/Recursive_descent_parser BNF notation for syntax, W3C: https://www.w3.org/Notation.html バッカス・ナウア記法: https://ja.wikipedia.org/wiki/バッカス・ナウア記法 Reverse Polish notation: https://en.wikipedia.org/wiki/Reverse_Polish_notation Shunting yard algorithm: https://en.wikipedia.org/wiki/Shunting_yard_algorithm Law of the instrument: https://en.wikipedia.org/wiki/Law_of_the_instrument 28