115 lines
2.1 KiB
Python
115 lines
2.1 KiB
Python
ff = open('../words.txt', 'r')
|
|
ww = ff.readline()
|
|
ff.close()
|
|
|
|
longest = 0
|
|
dic = {}
|
|
lisw = ww.split(',')
|
|
for i in lisw:
|
|
i = i[1:-1]
|
|
if len(i) > 2:
|
|
tmp = '.'.join(i).split('.')
|
|
tmp = ''.join(sorted(tmp))
|
|
if tmp in dic.keys():
|
|
dic.get(tmp).append(i)
|
|
else:
|
|
dic.update({tmp:[i]})
|
|
|
|
|
|
move = []
|
|
|
|
dicmove = {}
|
|
|
|
def howtomove(a, b):
|
|
tmp = ''
|
|
for i in a:
|
|
tmp += str(b.index(i))
|
|
if dicmove.has_key(tmp):
|
|
dicmove.get(tmp).append([a, b])
|
|
else:
|
|
dicmove.update({tmp:[[a, b]]})
|
|
return tmp
|
|
|
|
def howtolis(lis):
|
|
out = set([])
|
|
for i in lis:
|
|
for j in lis:
|
|
if i != j:
|
|
tmp = howtomove(i, j)
|
|
out.add(tmp)
|
|
return list(out)
|
|
|
|
words = []
|
|
|
|
|
|
for i in dic.keys():
|
|
if len(dic.get(i)) > 1:
|
|
wordlen = len(i)
|
|
while len(move) - 1 < wordlen:
|
|
move.append([])
|
|
words.append([])
|
|
move[wordlen].extend(howtolis(dic.get(i)))
|
|
words[wordlen].extend(dic.get(i))
|
|
|
|
#print words
|
|
#print move
|
|
|
|
def movenum(num, mm):
|
|
out = ''
|
|
num = str(num)
|
|
for i in xrange(len(mm)):
|
|
try:
|
|
out += num[int(mm[i])]
|
|
except IndexError:
|
|
return 3
|
|
return int(out)
|
|
|
|
|
|
def issq(x):
|
|
tmp = int(x ** 0.5)
|
|
if tmp ** 2 == x:
|
|
return True
|
|
return False
|
|
|
|
result = []
|
|
length = len(move[-1][0])
|
|
#print length
|
|
n = int((10 ** length) ** 0.5)
|
|
while length > 1 and n > 0:
|
|
sq = n ** 2
|
|
length = len(str(sq))
|
|
while len(move[length]) <= 0:
|
|
length -= 1
|
|
n = int((10 ** length) ** 0.5)
|
|
for moveit in move[length]:
|
|
newsq = movenum(sq, moveit)
|
|
if len(str(sq)) == len(str(newsq)):
|
|
if issq(newsq):
|
|
#print sq, newsq
|
|
result.append([sq, dicmove.get(moveit)[0][0]])
|
|
#if len(str(result[-1])) != len(str(result[0])):
|
|
#break
|
|
n -= 1
|
|
|
|
#print result
|
|
|
|
for item in result:
|
|
nums = str(item[0])
|
|
dicju1 = {}
|
|
dicju2 = {}
|
|
for i in xrange(len(item[1])):
|
|
numm = nums[i]
|
|
chh = item[1][i]
|
|
if numm in dicju1.keys():
|
|
if chh != dicju1.get(numm):
|
|
break
|
|
else:
|
|
dicju1.update({numm:chh})
|
|
if chh in dicju2.keys():
|
|
if numm != dicju2.get(numm):
|
|
break
|
|
else:
|
|
dicju2.update({chh:numm})
|
|
else:
|
|
print nums
|
|
quit() |