-
Notifications
You must be signed in to change notification settings - Fork 0
Solved Arai60/50. Pow(x, n) #45
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
| return 0 | ||
| if n < 0: | ||
| x = 1.0 / x | ||
| n = -n |
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.
好みですがn *= -1としてもいいと思いました。
| x = 1.0 / x | ||
| n = -n | ||
|
|
||
| if n % 2 == 1: |
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.
n % 2, n // 2についてはそれぞれn & 1, n >> 1とも書けるかもしれません。
操作の記述としては直感的ではないかもですが、一般的には整数の割り算に比べてビット演算の方がCPUにとって早いと聞いたことがあるので、一応頭の片隅に入れておくと良いかもしれません。(すみません、実際それぞれどれぐらいのクロック数が必要かとか、pythonでこう書いたときに最適化されてビット演算と同じになるのかとかは電車内にいまして調べられていません…)
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.
その視点はなかったですが今回のように計算用の関数の場合はそう言った観点も重要そうですね
|
|
||
| if n % 2 == 1: | ||
| return x * self.myPow(x, n-1) | ||
| return self.myPow(x*x, n//2) |
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.
自分はまとまっていた方が理解しやすいと思ってしまうタイプですが、一般的なのは開ける方な気はしています。。
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/#whitespace-in-expressions-and-statements
Python の標準的なスタイル指針の PEP8 はどちらでもといってそうですね。
スタイルは統一されていることが大事なので、あんまり局所的に正誤が決まるものであるというよりは周りを見て合わせるくらいがちょうどいいかと思います。
|
|
||
| を見ると微妙 | ||
|
|
||
| - 自分で計算するときは{x^(n//2)}^2のようにすることが多いのでこれを実装 |
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.
再帰でやるのは思い付きませんでした。勉強になります 👀
|
|
||
| ```python | ||
| class Solution: | ||
| @cache |
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.
キャッシュがほとんど効かないと思いますので、外してよいと思います。
| if n == 0: | ||
| return 1 | ||
| if n % 2 == 1: | ||
| return x * self.myPow(x, n-1) |
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/#other-recommendations
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), Booleans (and, or, not).
https://google.github.io/styleguide/pyguide.html#36-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
なお、これらのスタイルガイドは唯一絶対のルールではなく、数あるガイドラインの一つに過ぎません。チームによって重視する書き方が異なる場合もあります。
そのため、ご自身の中に基準を持ちつつも、最終的にはチーム内で一般的とされる書き方に寄せていくことをお勧めいたします。
| return 1 | ||
| if n % 2 == 1: | ||
| return x * self.myPow(x, n-1) | ||
| return self.myPow(x, n // 2) * self.myPow(x, n // 2) |
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.
一度変数に格納してから掛けたほうがシンプルだと思います。
p = self.myPow(x, n // 2)
return p * p
問題文: https://leetcode.com/problems/powx-n/description/