Skip to content

Conversation

cheeseNA
Copy link
Owner

Problem link
参照した他の解答についてはnote.mdに記載しております.
old.pyはかなり前に解いたコードなので, レビューいただかなくても大丈夫です!

@@ -0,0 +1,22 @@
class Solution:
def clamp_to_32bit_signed(self, num: int) -> int:
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.

承知しました, ありがとうございます.
https://peps.python.org/pep-0008/#method-names-and-instance-variables
https://google.github.io/styleguide/pyguide.html#3162-naming-conventions
PEP8にもGoogle style guideにも記載されていますね. 最近読んだのに失念しておりました.

is_positive = False
index += 1

while index < len(s) and "0" <= s[index] <= "9":
Copy link

Choose a reason for hiding this comment

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

s[index].isdigit() のほうが直感的に感じます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど, isdigitというのがあることを知りませんでした!
ドキュメント を確認したのですが, isdigitは10進数以外の数字(例: Kharosthi numbers)でもTrueを返すそうです. 今回のmyAtoiはそのようなケースは考慮外のため"0", "9"を使用したほうが意図しない結果を防ぐことができるかと考えたのですが, myAtoiに渡されるdigitは"0"~"9"のみであるという仮定の下isdigitを使うのが良いでしょうか.
これも選択の問題でしょうか.

Copy link

Choose a reason for hiding this comment

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

どちらでもよいように思いますが、なぜその選択をしたかは説明できる必要があると思います。
もし自分が書くのであれば、 isdigit() を使用し、ソースコードコメントで 10 進数以外の数字で True を返すが、入力としてそのような文字は現れないと書くと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど, ありがとうございます!
選択肢を持っておいて, 自分なりの根拠でどれか一つを選ぶということですね.
承知しました.

Choose a reason for hiding this comment

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

isdigitだと以下のような文字列にもTrueを返すんですね。

  • "٠١٢٣٤٥٦٧٨٩" (アラビア・インド数字)
  • "๐๑๒๓๔๕๖๗๘๙" (タイ数字)

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.

3 participants