Python Learning Summary 4

Source: Internet
Author: User
Tags pow

I did a question on Codewears today, only 6kyu, but I got a lot of it.

The title is: Write a function, Sum_dig_pow (A, B), find range[a, b]之间所有符合特殊规则的的数字,将其放入列表中,并返回。

This particular rule is as follows;

135 = 1^1 + 3^2 + 5^3

89 = 8^1 + 9^2

The functions that I wrote at the beginning are:

Import Mathdef Sum_dig_pow (A, B):     lis=[] for    i in range (a,b+1): li=[        ]        new_i=i        #找到数字是几位数        k= Int (MATH.LOG10 (new_i))        #将数字切分, such as 135 is cut into [1,3,5] for        J in Range (k+1): li.append (int (new_i/(Math.pow (            10), K-J))))            new_i=new_i% (Math.pow (10,k-j))                #求如 1^1 + 3^2 + 5^3 as shown in and values        ex=1        sum=0 for        x in Li:            Sum+=math.pow (X,ex)            ex+=1                #判断和值是否与当前数字相等        if sum==i:            lis.append (i)                             return lis     

After reviewing some of the previous content, the above code to improve;

Lis=[]def Sum_dig_pow (A, B):     for I in Range (a,b+1):        #将数字切分字符串, such as 135 cut into [' 1 ', ' 3 ', ' 5 ']        li=re.findall (' [0-9 ] ', str (i))        sum=0                #求和        for Key,value in Enumerate (LI):            sum+=pow (int (value), int (key) +1)                        #判断            If sum==i:                lis.append (i)    return lis   


Python Learning Summary 4

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.