Skip to content

Create 8. String to Integer (atoi).md#56

Open
fuga-98 wants to merge 1 commit intomainfrom
8.-String-to-Integer-(atoi)
Open

Create 8. String to Integer (atoi).md#56
fuga-98 wants to merge 1 commit intomainfrom
8.-String-to-Integer-(atoi)

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 31, 2025


https://github.com/python/cpython/blob/main/Objects/longobject.c#L4494

divmodを呼び出しているっぽい。なるほど、一回小数点にして切り捨てをしているわけではないようだ。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python の整数は桁数に限界がないので、浮動小数点数を使うわけにはいきませんね。

Copy link
Copy Markdown
Owner Author

@fuga-98 fuga-98 Jun 1, 2025

Choose a reason for hiding this comment

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

大きい値の場合は割り算に気をつけようと思いました。

https://github.com/olsen-blue/Arai60/pull/60/files#diff-f2b395d63173ac2f2d3c547e8f9e8e07c9acfbeb6b5da935bb28d83d8bcc7a04

- int()は使わないほうが今回の問題の空気は読めてそう
- オーバーフローのある言語だったらどうするか
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

単に sign としても伝わると思いました。

elif s[i] == '-':
signedness = -1
i += 1
num = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら、ここで一つの処理の区切りになるため、空行を入れ、処理の区切りを視覚的に分かりやすくすると思います。

MAX_INT32 = -(MIN_INT32 + 1)
class Solution:
def myAtoi(self, s: str) -> int:
def calc_int(num, c, signedness):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

英単語から文字を削って省略すると、読み手に取って認知負荷が上がる場合があります。原則避けることをお勧めします。

参考までにスタイルガイドへのリンクを貼ります。

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.

ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。また、所属するチームにより何が良いとされているかは変わります。自分の中で良い書き方の基準を持ちつつ、チームの平均的な書き方で書くことをお勧めいたします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

まれに見る気がして使ってしまいました。
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):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

calc_int という関数名ですと、中でどのような処理をしているのか想起しづらいように思いました。 append_digit() などはいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

そちらのほうが自然ですね。
何か別のところを考えていると変数名が雑になる傾向があるみたいなので気をつけようと思います。


```python
MIN_INT32 = - 2 ** 31
MAX_INT32 = -(MIN_INT32 + 1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

末尾に 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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この行の意味を読み解くのに時間がかかったのでコメントがあるとありがたいなと思いました。


https://leetcode.com/problems/string-to-integer-atoi/

オートマトンっぽいなと思いました。O(N)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

同じ問題で下記コメントをいただきました。

ステートマシンを作る場合、ステートを表す enum 相当のものを使う方法もあります。

hroc135/leetcode#54 (comment)

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