Conversation
|
|
||
| https://github.com/python/cpython/blob/main/Objects/longobject.c#L4494 | ||
|
|
||
| divmodを呼び出しているっぽい。なるほど、一回小数点にして切り捨てをしているわけではないようだ。 |
There was a problem hiding this comment.
大きい値の場合は割り算に気をつけようと思いました。
| https://github.com/olsen-blue/Arai60/pull/60/files#diff-f2b395d63173ac2f2d3c547e8f9e8e07c9acfbeb6b5da935bb28d83d8bcc7a04 | ||
|
|
||
| - int()は使わないほうが今回の問題の空気は読めてそう | ||
| - オーバーフローのある言語だったらどうするか |
There was a problem hiding this comment.
C++ のオーバーフローは、
__builtin_add_overflow
stdckdint.h の ckd_add などが用意されています。
https://discord.com/channels/1084280443945353267/1364239095639048274/1378091610973409351
| i += 1 | ||
| if i >= len(s): # ここなくてエラー | ||
| return 0 | ||
| signedness = 1 |
| elif s[i] == '-': | ||
| signedness = -1 | ||
| i += 1 | ||
| num = 0 |
There was a problem hiding this comment.
自分なら、ここで一つの処理の区切りになるため、空行を入れ、処理の区切りを視覚的に分かりやすくすると思います。
| MAX_INT32 = -(MIN_INT32 + 1) | ||
| class Solution: | ||
| def myAtoi(self, s: str) -> int: | ||
| def calc_int(num, c, signedness): |
There was a problem hiding this comment.
英単語から文字を削って省略すると、読み手に取って認知負荷が上がる場合があります。原則避けることをお勧めします。
参考までにスタイルガイドへのリンクを貼ります。
https://google.github.io/styleguide/pyguide.html#316-naming
Avoid abbreviation. In particular, do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word.
ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。
There was a problem hiding this comment.
まれに見る気がして使ってしまいました。
https://en.m.wikipedia.org/wiki/Calc
チームに合わせようと思います。
| MAX_INT32 = -(MIN_INT32 + 1) | ||
| class Solution: | ||
| def myAtoi(self, s: str) -> int: | ||
| def calc_int(num, c, signedness): |
There was a problem hiding this comment.
calc_int という関数名ですと、中でどのような処理をしているのか想起しづらいように思いました。 append_digit() などはいかがでしょうか?
There was a problem hiding this comment.
そちらのほうが自然ですね。
何か別のところを考えていると変数名が雑になる傾向があるみたいなので気をつけようと思います。
|
|
||
| ```python | ||
| MIN_INT32 = - 2 ** 31 | ||
| MAX_INT32 = -(MIN_INT32 + 1) |
There was a problem hiding this comment.
末尾に 32 をつけて 64bit の最大/最小値と区別していていいなと思いました。
MAX_INT32 は回りくどく書かずに単に = 2 ** 31 - 1 でいいと思いました。
| # 上限に収まるか計算 | ||
| if total > MAX_INT32 // 10: | ||
| return MAX_INT32 | ||
| if total == MAX_INT32 // 10 and digit > MAX_INT32 % 10: |
There was a problem hiding this comment.
この行の意味を読み解くのに時間がかかったのでコメントがあるとありがたいなと思いました。
|
|
||
| https://leetcode.com/problems/string-to-integer-atoi/ | ||
|
|
||
| オートマトンっぽいなと思いました。O(N) |
There was a problem hiding this comment.
https://leetcode.com/problems/string-to-integer-atoi/description/?source=submission-noac