def Test(N):
tmp=N
list=[]
while tmp / 10.0 >0:
s=tmp % 10
list.append(s)
tmp=tmp // 10
sum = 0
for item in list:
sum+=item**len(list)
if sum == N:
return True
else:
return False
print(Test(153))
True
优化方案二、 12行
[Python] 纯文本查看复制代码
def Test(N):
tmp=N
list=[]
while tmp / 10.0 >0:
s=tmp % 10
list.append(s)
tmp=tmp // 10
sum = 0
for item in list:
sum+=item**len(list)
return sum == N
print(Test(253))
优化方案三、10行
[Python] 纯文本查看复制代码
def test2(N):
strN=str(N)
list=[]
for item in strN:
list.append(item)
sum=0
for item in list:
sum+=int(item)**len(list)
return sum == N
print(test2(153))
优化方案四、8行
[Python] 纯文本查看复制代码
def test2(N):
strN=str(N)
list=[item for item in strN]
sum=0
for item in list:
sum+=int(item)**len(list)
return sum == N
print(test2(153))
优化方案五 7行
[Python] 纯文本查看复制代码
ef test2(N):
list1=list(str(N))
sum=0
for item in list1:
sum+=int(item)**len(list1)
return sum == N
print(test2(153))