Skip to content

Conversation

@shining-ai
Copy link
Owner

Copy link

@nodchip nodchip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この問題を読んだとき、再帰下降構文解析を連想しました。

再帰下降構文解析でも書いてみていただけますか?

Comment on lines +7 to +9
while index < len(s):
if s[index].isdigit():
num = 10 * num + int(s[index])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"1 2" のような入力が12と出力されないですか?問題上そのような入力は来ないと思いますが。

Copy link
Owner Author

@shining-ai shining-ai May 14, 2024

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():
Copy link

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] == "(":
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これは最初に持ってきて、 early return した方がシンプルになると思います。

Copy link
Owner Author

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 @@
# 再帰下降構文解析
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

単項演算子に対応できているでしょうか?
-1
という入力に対して何を返しますか?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-1が返ります。
一応想定通りの挙動です。

Copy link

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 の処理を行っているようです。他の方がソースコードを読んだとき、見落とす可能性が高いと思います。単項演算子を明示的にケアしたほうが良いと思います。

Copy link
Owner Author

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()

Copy link

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
Copy link

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 @@
# 再帰下降構文解析
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

入力文字列内のスペースを最初にすべて取り除いてしまっても良いように思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定数倍遅くなるのが気になり読み取りながら削除してましたが、最初に取り除いた方がシンプルですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants