Trick(一)——判断一个数的位数

57 篇文章 0 订阅
30 篇文章 0 订阅

传统的做法是,不断地除以10,

def digits(n):
    cnt = 0
    while n:
        cnt += 1
        n //= 10
    return cnt

在微博上看到一种较为数学(也较为tricky)的方法,使用 log10 (以10为底,而) 映射的结果即为该数的位数,

  • x=19 0<log10x<1
  • x=1099 1<log10x<2

我们只需对其映射结果取整加1即可。我们甚至只需定义一个匿名函数:

from math import log10
digits = lambda n: int(log10(n))+1

举一反三

上文默认都是十进制数,如何判断一个10进制数对应的二进制的位数呢,自然我们用的对数是以2为底的对数: log2

  • x=47 2<log2(x)<3
  • x=1015 3<log2(x)<4
from math import log2
digits = lambda n: int(log2(n))+1
Title: The Next Big Thing Opening shot: A bustling city street with people walking and cars honking. Voiceover: "In a world where everyone is seeking fame and success, one man has a plan to make it big." Cut to an office building where we see our protagonist, John, working as a low-level employee. Voiceover: "Meet John, a hardworking man with big dreams but a small paycheck." John is seen working at his desk, staring at his computer screen, looking frustrated. John: "This is not what I want. I need to find a way to make it big." Cut to John brainstorming ideas with his friends at a local bar. John's friend 1: "You need to create something that people can't resist, something that's different, something that's never been done before." John's friend 2: "But what could that be?" John: "I don't know yet, but I'll figure it out." Cut to John walking down the street and seeing a group of people gathered around a street performer. Street performer: "Ladies and gentlemen, watch as I perform this incredible trick!" John watches as the street performer successfully performs a trick, and the crowd cheers. John: "That's it! I'll create a new kind of street performance that will blow people's minds." Cut to John practicing his new street performance routine. John's friend 1: "This is amazing! You're going to be a hit!" John's friend 2: "We need to film this and put it on social media." Cut to John's street performance going viral on social media. Voiceover: "In just a few days, John's street performance went viral, and he became an overnight sensation." Cut to John being interviewed on a talk show. Talk show host: "John, how did you come up with this incredible street performance?" John: "It was just an idea I had, and I put in the hard work to make it a reality." Cut to John performing his street performance in front of a huge crowd. Voiceover: "John's street performance became a global phenomenon, and he became the next big thing." Closing shot: John walking down the street with his friends, looking happy and successful. Voiceover: "All it takes is one great idea and the determination to make it happen. Who knows? You could be the next big thing."
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

五道口纳什

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

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

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

打赏作者

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

抵扣说明:

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

余额充值