Advertisement
danchaofan

Euler #98

Dec 25th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. def anagram(a, b):
  2.     a, b = str(a), str(b)
  3.     return sorted(a) == sorted(b)
  4.  
  5. file, tempstr, record = [], "", []
  6. for a in open("words.txt"):
  7.     for b in a:
  8.         if b == ",":
  9.             file.append(tempstr)
  10.             tempstr = ""
  11.         if b not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
  12.             continue
  13.         tempstr += b
  14.  
  15. for c in file:
  16.     anagrams = [c]
  17.     for d in file:
  18.         if c == d:
  19.             continue
  20.         if anagram(c, d):
  21.             anagrams.append(d)
  22.     if len(anagrams) == 1:
  23.         del c
  24.         continue
  25.     if sorted(anagrams) not in record:
  26.         record.append(sorted(anagrams))
  27.  
  28. newrecord = []
  29. for e in record:
  30.     if len(e[0]) == 5:
  31.         newrecord.append(e)
  32. print(newrecord)
  33.  
  34. lengthfive = []
  35. for x in range(10**10):
  36.     if len(str(x**2)) < 5:
  37.         continue
  38.     if len(str(x**2)) > 5:
  39.         break
  40.     if len(set(str(x**2))) == len(str(x**2)):
  41.         lengthfive.append(x**2)
  42.  
  43. newerrecord = []
  44. for c in lengthfive:
  45.     anagrams = [c]
  46.     for d in lengthfive:
  47.         if c == d:
  48.             continue
  49.         if anagram(c, d):
  50.             anagrams.append(d)
  51.     if len(anagrams) == 1:
  52.         lengthfive.remove(c)
  53.         continue
  54.     if sorted(anagrams) not in newerrecord:
  55.         newerrecord.append(sorted(anagrams))
  56. print(newerrecord)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement