该脚本以指数形式增加数字时显示(0,1,2,3,4,5,6,7,8,9)中出现的最后一个数字。
我认为我的问题集中在循环上。
def EjA(n):
#List of numbers to control
l1=[0,1,2,3,4,5,6,7,8,9]
#Convert l2 to str
l2=''.join(map(str,l1))
l3=[]
#List of deleted elements
l_item_deleted=[]
cont=1
#Conditional to check n is between 1 and 10000
if n<=1 or n>= 10000:
return None
else:
#To delete data until have just the last number
while len(l2)>1:
#Add to l3 n**cont to collect all the number in l3 list
l3.append(n**cont)
#Convert l3 to str
l4=''.join(map(str,l3))
l_item_deleted_aux=''.join(map(str,l_item_deleted))
#Delete the element deleted before
l4=l4.replace(l_item_deleted_aux,'')
cont+=1
for i in l4:
if (i in l2) and len(l2)>1:
#Delete from l2 all the numbers which appear in l4
l2=l2.replace(i,'')
#Collect the element deleted in a list
l_item_deleted.append(i)
elif (i not in l2) and len(l2)>1:
l_item_deleted.append(i)
return int(l2)
EjA(2)
目前,我的执行时间过长,无法完成所有测试随机案例。
目标是改进代码以减少执行时间。
一些建议?