summaryrefslogtreecommitdiff
path: root/098.py
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2014-08-31 20:21:45 +0200
committerReiner Herrmann <reiner@reiner-h.de>2014-08-31 20:35:09 +0200
commit95341b61b030c9e1290f3b326cb7ec584f543aea (patch)
tree852386fa04d32eb859bca11c0eff7b5ef9e50f00 /098.py
parent571164d977f91925c4c76a292f74f5f93d09ae23 (diff)
moved files to higher directory after split to new repositoryHEADtrunk
Diffstat (limited to '098.py')
-rw-r--r--098.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/098.py b/098.py
new file mode 100644
index 0000000..452e15e
--- /dev/null
+++ b/098.py
@@ -0,0 +1,77 @@
+from math import sqrt, ceil
+
+# is x anagram of y?
+def anagram(x, y):
+ x2 = [c for c in x]
+ y2 = [c for c in y]
+ x2.sort()
+ y2.sort()
+ return x2 == y2
+
+
+# returns replacement list
+def replacement(word, number):
+ digits = [False] * 10
+ for char in word[::-1]:
+ digit = number % 10
+ number /= 10
+
+ if char in digits and digits[digit] != char:
+ return None
+
+ if digits[digit] == False:
+ digits[digit] = char
+ elif digits[digit] != char:
+ return None
+ return digits
+
+
+# find numbers matching the specified word in descending order
+def match(word, numbers):
+ result = []
+ for n in numbers:
+ digits = replacement(word, n)
+ if digits != None:
+ result.append(n)
+
+ result.sort(reverse=True)
+ return result
+
+
+f = open('098.txt', 'r')
+words = f.read()
+f.close()
+
+maxlen = 14
+
+words = set([ w.strip('"') for w in words.split(',') ])
+squares = [ [ x*x for x in xrange(int(ceil(sqrt(10**n)))-1, int(ceil(sqrt(10**(n-1))))-1, -1) ] for n in xrange(1, maxlen+1) ]
+
+maxsquare = 0
+
+for l in xrange(1, maxlen+1):
+ cur = [ w for w in words if len(w) == l ]
+
+ for word1 in cur:
+ for word2 in cur:
+ if word1 == word2 or word1 == word2[::-1] or not anagram(word1, word2):
+ continue
+
+ m1 = match(word1, squares[l-1])
+ m2 = match(word2, squares[l-1])
+
+ # compare result lists
+ for n1 in m1:
+ found = False
+ for n2 in m2:
+ if replacement(word1, n1) == replacement(word2, n2):
+ #print word1, word2, n1, n2
+ if n1 > maxsquare:
+ maxsquare = n1
+ found = True
+ break
+ if found:
+ break
+
+print maxsquare
+