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 次幂函数,以计算211次方(2028)为例,常规算法是计算102与原数相乘。

为了简化计算,可以先计算出2×2=4的值,这样211次方可以写成4X4×4X4×4×2(此处多余一个4×2)的形式,再计算4X4=16的值,211次方可以写成16×16×4×2的值,这样计算2X2,4×4,16×164X2的值,只计算了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 1

        res ,curr = 1, abs(n)

        while curr > 0:

            if curr & 1 == 1:

                res *= x

            curr >>= 1

            x *= x

        if n < 0:

            return 1 / res

        return  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 bits
    else:
        return [0]

number_to_bits(6)

The output to stderr is:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打酱油的工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值