-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】6問目224. Basic Calculator #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
nodchip
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この問題を読んだとき、再帰下降構文解析を連想しました。
- https://ja.wikipedia.org/wiki/%E5%86%8D%E5%B8%B0%E4%B8%8B%E9%99%8D%E6%A7%8B%E6%96%87%E8%A7%A3%E6%9E%90
- https://dai1741.github.io/maximum-algo-2012/docs/parsing/
再帰下降構文解析でも書いてみていただけますか?
| while index < len(s): | ||
| if s[index].isdigit(): | ||
| num = 10 * num + int(s[index]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"1 2" のような入力が12と出力されないですか?問題上そのような入力は来ないと思いますが。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この問題は電卓をイメージしていたので、"1 2"は12の認識でした。
ちょっと問題読み直してみます。
| def calculate(self, s: str) -> int: | ||
| index = 0 | ||
|
|
||
| def get_next_expression(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
get で始まる関数名は、何かを取得しているように見えます。 skip_spaces() はいかがでしょうか?
| while index < len(s) and s[index].isdigit(): | ||
| result = 10 * result + int(s[index]) | ||
| get_next_expression() | ||
| if index < len(s) and s[index] == "(": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これは最初に持ってきて、 early return した方がシンプルになると思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s[index].isdigit()
")"の後に数字は来ないから、無駄に条件判定させない方がいいということですね。
| @@ -0,0 +1,38 @@ | |||
| # 再帰下降構文解析 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
単項演算子に対応できているでしょうか?
-1
という入力に対して何を返しますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1が返ります。
一応想定通りの挙動です。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ソースコード読み直してみました。 -1 を expr() で処理する際、 factor() から 0 が返り、そのあと - が処理されて、 1 を引いているように見えます。実質 0 - 1 の処理を行っているようです。他の方がソースコードを読んだとき、見落とす可能性が高いと思います。単項演算子を明示的にケアしたほうが良いと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントの意図通りかわからないのですが、「-」始まりの計算単行演算子のケアしてみました。
if index < len(s) and s[index] == "-":
index += 1
result = -factor()
else:
result = factor()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。よいと思います。
|
|
||
| def factor(): | ||
| nonlocal index | ||
| result = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result に 0 を代入してから、 if 文の中で再度 expr() を代入している点に違和感を感じました。 result = 0 は、if 文が終わったあと、 while 文の前に持ってきた方が、読み手にとって分かりやすいと思います。
| @@ -0,0 +1,39 @@ | |||
| # 再帰下降構文解析 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
入力文字列内のスペースを最初にすべて取り除いてしまっても良いように思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
定数倍遅くなるのが気になり読み取りながら削除してましたが、最初に取り除いた方がシンプルですね。
This reverts commit 2c68e38.
問題
https://leetcode.com/problems/basic-calculator/