Write a script to check an interesting game 6174

时间:2023-03-08 17:15:25
Write a script to check an interesting game 6174

# -*- coding: utf-8 -*-
#from ftplib import FTP
import os
def sort_reverse(x,y):
if x>y:
return -1
if x<y:
return 1
else:
return 0

def play6174(num):
a=num/1000
b=(num-a*1000)/100
c=(num-a*1000-b*100)/10
d=(num-a*1000-b*100-c*10)
a=num/1000
b=(num-a*1000)/100
c=(num-a*1000-b*100)/10
d=(num-a*1000-b*100-c*10)
list1=[a,b,c,d]
listmin=sorted(list1)
listmax=sorted(list1,sort_reverse)
max=listmax[0]*1000+listmax[1]*100+listmax[2]*10+listmax[3]
min=listmin[0]*1000+listmin[1]*100+listmin[2]*10+listmin[3]
str1=str(max)+'-'+str(min)+'='+str(max-min)
print str1
f.write(str1+'\n')
return max-min


if __name__=='__main__':
#num=int(raw_input('Just put in a number (1000~9999):'))
f=open(r'C:\Users\cchen\Desktop\result.txt','w')
for j in range(1000,10000):
print j
f.write(str(j)+'\n')
if j%1111!=0:
loop=j
i=0
while(loop!=6174):
loop=play6174(loop)
i+=1
str2='Caculate for '+str(i)+'times.'
f.write(str2+'\n')
f.write('+++++++++++++++++++++++++++++++\n')
print str2
print '+++++++++++++++++++++++++++++++\n'

f.close()