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