-
Notifications
You must be signed in to change notification settings - Fork 0
Create Pow.md #9
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
Pow(x, n)
Pow.md
Outdated
|
||
- ここで答えを見ると、x^n = (x^(n/2))^2の操作を繰り返すと計算回数を減らせると知ったので書き直してみる。 | ||
- 愚直にPowを2回呼び出すと結局再帰呼び出し回数に引っかかってしまうので、temp的な変数(half…)に入れ直す | ||
- Nが小数の時とかどうしてるんだろう→先人のgitにPowのプログラムへのリンクがあり、生まれて初めてCPythonのソースコードを読んでみたい気になる。が、手も脚も出ない:https://github.com/python/cpython/blob/d5ba4fc9bc9b2d9eff2a90893e8d500e0c367237/Objects/longobject.c#L4849 |
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.
nit: 少数
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://ja.wikipedia.org/wiki/%E5%B0%8F%E6%95%B0
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.
すいません、(勘違があったとおっしゃっていますが)こちら元々はどういう意味でおっしゃっていたのですか...?nitで調べても小数みたいな意味は出てこず...
すいません、コードレビューで使う用語だったんですね:https://www.bioerrorlog.work/entry/what-is-nits-origin
x = 1 / x | ||
|
||
if n % 2 == 0: | ||
half_even = 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.
n の型が int のため、割り算の結果の型が整数になるよう、 // 演算子を使ったほうが良いと思います。
if n % 2 == 0: | ||
half_even = self.myPow(x, n / 2) | ||
return half_even * half_even | ||
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.
elif: のほうがシンプルだと思います。
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にしてたのか思い出せないので、整理途中のコードがそのまま残っていたものと思われます...)
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.
else にするか全体を下げるかでしょうかね。
偶数奇数での差は x をかけるかだけなので、もう少しまとめることもできます。
Step 3 あたりでなされているので、選択肢が見えているならば、あとは趣味の範囲かと思います。
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と対称でない様に感じられるのでelseにしそうです。
偶数奇数での差は x をかけるかだけなので、もう少しまとめることもできます
ありがとうございます、if文に入る前に計算してhalfに入れるなどしようと思います。
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.
elif n == 0: | ||
return 1 | ||
|
||
accum_prod = 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://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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます、accumulated_productなどに変更します
elif n == 0: | ||
return 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 == 0 の条件を先頭に回したほうが単純に見える気がしますね。(下ではそうしていますね。)
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.
僕もそちらの方が自然に感じます。繰り返し打っているステップ3の方が、なんとなく色んな負荷が軽減されたコードが出力される感じがある(「しっくりくる」「書きやすい」)ので、ご指摘いただいたやり方を採用します。
def myPow(self, x: float, n: int) -> float: | ||
if n == 0: | ||
return 1 | ||
elif 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.
私は上の条件が return ならば、elif よりも if を好みます。
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.
elif以降を気にしながらifに続く処理を読む分の負荷が不要になるためですかね。であれば納得感があるので、自分もifを採用してみようと思います(違ったら教えてください)。
n = -n | ||
|
||
if n % 2 == 0: | ||
half_pow = 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.
上のコメントと重なりますが、n // 2とした方がint型のままだな、というのが読んでいてはっきりわかるので不安がなさそうです。(この場合、分母が偶数だから、、、という推理が入る)
また、そのようにしたらhalf_powまでの処理が共通化できますね。
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.
こうですね,ありがとうございます。
class Solution(object):
def myPow(self, x, n):
if n == 0:
return 1.0
if n < 0:
n = -n
x = 1/x
half_pow = self.myPow(x, n//2)
if n % 2 == 0:
return half_pow*half_pow
else:
return x*half_pow*half_pow
accum_prod = accum_prod * x | ||
x = x * x | ||
n = n // 2 | ||
print(f"{accum_prod}") |
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.
消し忘れですかね。
ちなみにご存知だったらすみませんが、Python3.8以降では、print(f"{accum_prod=}")ように{}内に=をつけると、accum_prod=10 みたいに変数名を合わせて表示されるので、複数の変数のデバッグに便利です。
https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging
問題文:50. Pow(x, n): https://leetcode.com/problems/powx-n/description/