-
Notifications
You must be signed in to change notification settings - Fork 0
8. String to Integer (atoi) #5
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
@@ -0,0 +1,22 @@ | |||
class Solution: | |||
def clamp_to_32bit_signed(self, num: int) -> int: |
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.
承知しました, ありがとうございます.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s[index].isdigit()
のほうが直感的に感じます。
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.
なるほど, isdigitというのがあることを知りませんでした!
ドキュメント を確認したのですが, isdigitは10進数以外の数字(例: Kharosthi numbers)でもTrueを返すそうです. 今回のmyAtoiはそのようなケースは考慮外のため"0", "9"を使用したほうが意図しない結果を防ぐことができるかと考えたのですが, myAtoiに渡されるdigitは"0"~"9"のみであるという仮定の下isdigitを使うのが良いでしょうか.
これも選択の問題でしょうか.
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.
どちらでもよいように思いますが、なぜその選択をしたかは説明できる必要があると思います。
もし自分が書くのであれば、 isdigit() を使用し、ソースコードコメントで 10 進数以外の数字で True を返すが、入力としてそのような文字は現れないと書くと思います。
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.
isdigitだと以下のような文字列にもTrueを返すんですね。
- "٠١٢٣٤٥٦٧٨٩" (アラビア・インド数字)
- "๐๑๒๓๔๕๖๗๘๙" (タイ数字)
Problem link
参照した他の解答についてはnote.mdに記載しております.
old.pyはかなり前に解いたコードなので, レビューいただかなくても大丈夫です!