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)相关推荐

  1. python 编程一日一练-python每日一练

    广告关闭 2017年12月,云+社区对外发布,从最开始的技术博客到现在拥有多个社区产品.未来,我们一起乘风破浪,创造无限可能. 过滤掉列表中的负数 筛选出字典{lilei: 79,jim: 88,lu ...

  2. Python每日一练0018

    问题 你需要对浮点数执行精确的计算操作,并且不希望有任何小误差的出现. Python的float类型是存在误差的 >>> a = 1.1 >>> b = 2.2 & ...

  3. Python每日一练0023

    问题 如何判断一个文件是否存在 解决方案 这个问题可以分成几类问题 如果这里的文件指的是文件或目录,我们可以用os.path.exists()方法 >>> import os > ...

  4. Python每日一练0004

    问题 如何保存迭代对象的最后N个元素 例如保存列表['a', 'b', 'c', 'd']的最后2个元素 或者保存某个迭代器对象的最后5个元素 解决方案 对于列表.元组这样的数据结构,可以使用切片来很 ...

  5. python 编程一日一练-Python每日一练0022

    问题 你想在一个文件里每次读入固定大小的字节,比如每次读入4个字节并转成int,或者每次读入x个字节并进行结构化,例如: l = [5, 2, 4, 1, 2, 4, 5, 6, 8] with op ...

  6. python 编程一日一练-Python每日一练0013

    问题 现在有多个字典或者映射,你想将它们从逻辑上合并为一个单一的映射后执行某些操作, 比如查找值或者检查某些键是否存在. 解决方案 使用collections库的ChainMap类,可以快速的将多个d ...

  7. python每日一练名片管理程序_Python每日一练0022

    问题 你想在一个文件里每次读入固定大小的字节,比如每次读入4个字节并转成int,或者每次读入x个字节并进行结构化,例如: l = [5, 2, 4, 1, 2, 4, 5, 6, 8] with op ...

  8. java游戏将相_(Java)算法——位运算基础及基本应用

    位运算基础及基本应用 在处理整形数值时,可以直接对组成整形数值的各个位进行操作.这意味着可以使用屏蔽技术获得整数中的各个位(??) 位运算是针对整数的二进制进行的位移操作 整数 32位 , 正数符号为 ...

  9. Python每日一练第5天——将一组数尽可能均匀地分成两堆,使两个堆中的数的和尽可能相等

    每日一练-做题 麦克叔叔去世了,他在遗嘱中给他的两个孙子阿贝和鲍勃留下了一堆珍贵的口袋妖怪卡片.遗嘱中唯一的方向是"尽可能均匀地分配纸牌的价值".作为Mike遗嘱的执行人,你已经为 ...

最新文章

  1. Lua 学习笔记(一)环境搭建
  2. iOS开发中视图相关的小笔记:push、modal、popover、replace、custom
  3. Hi3515支持NFS-ROOT启动
  4. MySQL 中事务详解
  5. Python黑帽编程2.8 套接字编程
  6. php5.0 添加接口,Thinkphp5.0模型---插入数据
  7. Python 列表下标操作
  8. linux中UDP编程
  9. 动手写一个二叉平衡树
  10. 15.编写LED程序及反汇编工具
  11. STM32串口屏学习
  12. 强大便携的多标签文件管理器 XYplorer Pro 21.60 中文版
  13. 计算机视觉最新进展概览2021年10月31日到2021年11月6日
  14. 安卓玩机搞机技巧综合资源---MIUI14全机型首版下载链接 刷机方法 获取root步骤【十二】
  15. 读书笔记 PCG in Games 程序化内容生成3 构造性方法,针对地牢式关卡
  16. Nexus上传jar问题【史上最全,亲测可用】
  17. 文章汇总【就R不E让I找D到系列part1】
  18. EMX,PROC文件与电感的简单仿真
  19. git本地分支 远程分支简单操作(后续更新)
  20. C语言入门:输入正数X,求其平方根并打印

热门文章

  1. win2003x64+Oraclex64+PL/SQL安装
  2. 三维场景计算任意两点的空间距离
  3. uniapp项目怎么连接手机真机调试
  4. python处理表格数据匹配-python爬取两个excel表里的相同数据并匹配输出
  5. 一款基于jQuery的超酷动画幻灯片
  6. html5点击效果文字跳转,JS网页特效代码,点击跳出爱心和文字特效
  7. 自醒-【德鲁克的经典五问】
  8. 激光位移传感器适用于那些领域
  9. PPT中的一些设计的原则
  10. 【分布式理论】(二)分布式存储