Python|每日一练|幂函数算法|位运算|>>右移|分析神器pysnooper|日志输出:Pow(x, n)
Pow(x, n)
實現?pow(x,?n)(https://www.cplusplus.com/reference/valarray/pow/)?,即計算 x 的 n 次冪函數(即,xn)。
示例 1:
輸入:x = 2.00000, n = 10
輸出:1024.00000
示例 2:
輸入:x = 2.10000, n = 3
輸出:9.26100
示例 3:
輸入:x = 2.00000, n = -2
輸出:0.25000
解釋:2-2 = 1/22 = 1/4 = 0.25
?
提示:
- -100.0 <?x?< 100.0
- -231?<= n <=?231-1
- -104 <= xn <= 104
解法參考鏈接:https://blog.csdn.net/weixin_43813003/article/details/102020554
算法思路:
Pow(x, n)求幕即計算 x 的 n 次冪函數,以計算2的11次方(2028)為例,常規算法是計算10次2與原數相乘。
為了簡化計算,可以先計算出2×2=4的值,這樣2的11次方可以寫成4X4×4X4×4×2(此處多余一個4×2)的形式,再計算4X4=16的值,則2的11次方可以寫成16×16×4×2的值,這樣計算2X2,4×4,16×16,4X2的值,只計算了5次即得出結果。
由于計算機執行的是二進制,所以可以通過位運算進行計算。對上述冪算法進行優化,例如判斷n是否偶數,可以使用“按位與”運算符“&”與1進行與計算,即判斷n的值是否為0即可。而n=n/2(即階乘每次降低1半)可以使用“右移”運算符“>>”,即n>>=1來操作。
例如:求解2^11
求解變量值記錄如下:
--*--Source path:... E:/BLOG/python_day/python_day_code/D.py--*--Starting var:.. self = <__main__.Solution object at 0x000002025F6867B8>--*--Starting var:.. x = 2.0--*--Starting var:.. n = 11--*--14:02:56.664210 call???????? 5???? def myPow(self, x, n):--*--14:02:56.664210 line???????? 6???????? if n == 0:--*--14:02:56.664210 line???????? 8???????? res ,curr = 1, abs(n)--*--New var:....... res = 1--*--New var:....... curr = 11--*--14:02:56.664210 line???????? 9???????? while curr > 0:--*--14:02:56.664210 line??????? 10???????????? if curr & 1 == 1:--*--14:02:56.664210 line??????? 11???????????????? res *= x--*--Modified var:.. res = 2.0--*--14:02:56.664210 line??????? 12???????????? curr >>= 1--*--Modified var:.. curr = 5--*--14:02:56.664210 line??????? 13???????????? x *= x--*--Modified var:.. x = 4.0--*--14:02:56.664210 line???????? 9???????? while curr > 0:--*--14:02:56.664210 line??????? 10???????????? if curr & 1 == 1:--*--14:02:56.664210 line??????? 11???????????????? res *= x--*--Modified var:.. res = 8.0--*--14:02:56.664210 line??????? 12???????????? curr >>= 1--*--Modified var:.. curr = 2--*--14:02:56.679833 line??????? 13???????????? x *= x--*--Modified var:.. x = 16.0--*--14:02:56.679833 line???????? 9???????? while curr > 0:--*--14:02:56.679833 line??????? 10???????????? if curr & 1 == 1:--*--14:02:56.679833 line??????? 12???????????? curr >>= 1--*--Modified var:.. curr = 1--*--14:02:56.679833 line??????? 13???????????? x *= x--*--Modified var:.. x = 256.0--*--14:02:56.679833 line???????? 9???????? while curr > 0:--*--14:02:56.679833 line??????? 10???????????? if curr & 1 == 1:--*--14:02:56.679833 line??????? 11???????????????? res *= x--*--Modified var:.. res = 2048.0--*--14:02:56.679833 line??????? 12???????????? curr >>= 1--*--Modified var:.. curr = 0--*--14:02:56.679833 line??????? 13???????????? x *= x--*--Modified var:.. x = 65536.0--*--14:02:56.679833 line???????? 9???????? while curr > 0:--*--14:02:56.679833 line??????? 14???????? if n < 0:--*--14:02:56.679833 line??????? 16???????? return? res--*--14:02:56.679833 return????? 16???????? return? res--*--Return value:.. 2048.0--*--Elapsed time: 00:00:00.015623最終得出x = 2048
示例代碼:
import pysnooper? #pip install pysnooper后重新加載IDE后使用@pysnooper.snoop("./log/debug.log", prefix="--*--") #需提前建立目錄及文件class Solution:def myPow(self, x, n):if n == 0:return 1res ,curr = 1, abs(n)while curr > 0:if curr & 1 == 1:res *= xcurr >>= 1x *= xif n < 0:return 1 / resreturn? res# %%s = Solution()print(s.myPow(x = 2.00000, n = 11))?PS:日志輸出神器PySnooper
項目地址:GitHub - cool-RR/PySnooper: Never use print for debugging again
便捷安裝:pip install pysnooper
PySnooper-不再使用打印進行調試
官方介紹及DEMO
PySnooper - Never use print for debugging again
PySnooper?is a poor man's debugger. If you've used Bash, it's like?set -x?for Python, except it's fancier.
Your story: You're trying to figure out why your Python code isn't doing what you think it should be doing. You'd love to use a full-fledged debugger with breakpoints and watches, but you can't be bothered to set one up right now.
You want to know which lines are running and which aren't, and what the values of the local variables are.
Most people would use?print?lines, in strategic locations, some of them showing the values of variables.
PySnooper?lets you do the same, except instead of carefully crafting the right?print?lines, you just add one decorator line to the function you're interested in. You'll get a play-by-play log of your function, including which lines ran and when, and exactly when local variables were changed.
What makes?PySnooper?stand out from all other code intelligence tools? You can use it in your shitty, sprawling enterprise codebase without having to do any setup. Just slap the decorator on, as shown below, and redirect the output to a dedicated log file by specifying its path as the first argument.
Example
We're writing a function that converts a number to binary, by returning a list of bits. Let's snoop on it by adding the?@pysnooper.snoop()?decorator:
import pysnooper @pysnooper.snoop() def number_to_bits(number):if number:bits = []while number:number, remainder = divmod(number, 2)bits.insert(0, remainder)return bitselse:return [0]number_to_bits(6)The output to stderr is:
總結
以上是生活随笔為你收集整理的Python|每日一练|幂函数算法|位运算|>>右移|分析神器pysnooper|日志输出:Pow(x, n)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win2008R2安装Epson630K
- 下一篇: java 构造函数和super