当前位置 博文首页 > boysoft2002的专栏:一起挑战pythontip的题目(3)

    boysoft2002的专栏:一起挑战pythontip的题目(3)

    作者:[db:作者] 时间:2021-07-27 20:56

    第11题:结尾0的个数
    题目描述:给你一个正整数列表 L, 输出L内所有数字的乘积末尾0的个数。(提示:不要直接相乘,数字很多,相乘得到的结果可能会很大)。
    例如: L=[2,8,3,50],
    则输出:2
    示例:输入:L = [4, 2, 25, 7777777, 100, 3, 77777777, 77777777, 77777777, 77777777]
    输出:4

    def div(m,n):
        k=0
        while m%n==0:
            m=m//n
            k+=1
        return k
    
    def zero(L):
        k0=k2=k5=0
        for i,k in enumerate(L):
            if div(k,10):
                k0+=div(k,10)
                L[i]=l//10**div(k,10)
        for i in L:
            if div(i,2):
                k2+=div(i,2)
            if div(i,5):
                k5+=div(i,5)
        return k0+min(k2,k5)
    
    
    L = [2,8,3,50]
    print(zero(L)) # ==2
    
    L = [4, 2, 25, 7777777, 100, 3, 77777777, 77777777, 77777777, 77777777]
    print(zero(L)) # ==4
    
    K = [4, 2, 25, 77500, 175000, 3, 7720, 125000, 665, 578]
    print(zero(K)) # ==15
    
    # 用相乘来验算:
    n = 1
    K = [4, 2, 25, 77500, 175000, 3, 7720, 125000, 665, 578]
    for i in K: n*=i
    print(n)
    print(div(n,10)) # ==15

    ============================================================
    第12题:结尾非零数的奇偶性
    题目描述:给你一个正整数列表 L, 判断列表内所有数字乘积的最后一个非零数字的奇偶性。如果为奇数输出1,偶数则输出0.。
    例如:L=[2,8,3,50]
    则输出:0
    示例:输入:L = [2, 8, 3, 50]
    输出:0

    >>> def func(L):
    	n=1
    	for i in L:n*=i
    	return int(str(int(str(n)[::-1]))[0])%2
    
    >>> L=[2,8,3,50]
    >>> print(func(L))
    0
    >>> L=[2,7,3,50,100]
    >>> print(func(L))
    1
    >>> 

    ============================================================
    第13题:光棍的悲伤
    题目描述:光棍们对1总是那么敏感,因此每年的11.11被戏称为光棍节。小Py光棍几十载,光棍自有光棍的快乐。让我们勇敢地面对光棍的身份吧,现在就证明自己:给你一个整数a,数出a在二进制表示下1的个数,并输出。
    例如:a=7
    则输出:3
    示例:输入:a = 7
    输出:3

    >>> a=7
    >>> list(bin(a)).count('1')
    3
    >>> a=8
    >>> list(bin(a)).count('1')
    1
    >>> 

    ============================================================
    第14题:Python之美
    题目描述:输出Python之禅。
    注意:输出python之禅的源码即可,不要转换为英文。(小小的提示:print this.s)

    >>> import this
    The Zen of Python, by Tim Peters
    
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    >>> 

    ============================================================
    第15题:大小写转换
    题目描述:给定一个字符串a, 将a中的大写字母 转换成小写,其它字符不变,并输出。
    例如:a="aaaaaabbbDDDDD"
    则输出:aaaaaabbbddddd
    示例:输入:a = "KDJIskos234k,.;djfeiJ"
    输出:kdjiskos234k,.;djfeij

    >>> a = "KDJIskos234k,.;djfeiJ"
    >>> a.lower()
    'kdjiskos234k,.;djfeij'
    >>> 

    ============================================================

    来源:

    Python 爬取pythontip网站的挑战题目(附全部题目178题)

    cs