Conversation
| @@ -0,0 +1,22 @@ | |||
| class Solution: | |||
| def clamp_to_32bit_signed(self, num: int) -> int: | |||
There was a problem hiding this comment.
細かい点となりますが、他のクラスから直接呼ばれないことを想定している関数は、関数名の先頭に「_」を付けることをお勧めいたします。
There was a problem hiding this comment.
承知しました, ありがとうございます.
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": |
There was a problem hiding this comment.
なるほど, isdigitというのがあることを知りませんでした!
ドキュメント を確認したのですが, isdigitは10進数以外の数字(例: Kharosthi numbers)でもTrueを返すそうです. 今回のmyAtoiはそのようなケースは考慮外のため"0", "9"を使用したほうが意図しない結果を防ぐことができるかと考えたのですが, myAtoiに渡されるdigitは"0"~"9"のみであるという仮定の下isdigitを使うのが良いでしょうか.
これも選択の問題でしょうか.
There was a problem hiding this comment.
どちらでもよいように思いますが、なぜその選択をしたかは説明できる必要があると思います。
もし自分が書くのであれば、 isdigit() を使用し、ソースコードコメントで 10 進数以外の数字で True を返すが、入力としてそのような文字は現れないと書くと思います。
There was a problem hiding this comment.
なるほど, ありがとうございます!
選択肢を持っておいて, 自分なりの根拠でどれか一つを選ぶということですね.
承知しました.
There was a problem hiding this comment.
isdigitだと以下のような文字列にもTrueを返すんですね。
- "٠١٢٣٤٥٦٧٨٩" (アラビア・インド数字)
- "๐๑๒๓๔๕๖๗๘๙" (タイ数字)
Problem link
参照した他の解答についてはnote.mdに記載しております.
old.pyはかなり前に解いたコードなので, レビューいただかなくても大丈夫です!