# fair warning: this program was optimized for coding time import re su = "shintekiuntamed" def anagram(s): letters = re.findall('.', s) letters.sort() return ''.join(letters) su_an = anagram(su) su_len = len(su) f = open('grease_fire.txt', 'r') lines = f.readlines() words = [] for line in lines: linewords = line.split() for word in linewords: word = word.lower() explode = re.findall('[a-z]+', word) word = ''.join(explode) if not len(word): continue words.append(word) words.reverse() candidate = [] candidate_len = 0 while 1: while candidate_len < su_len and words: word = words.pop() candidate_len += len(word) candidate.append(word) if candidate_len < su_len and not words: break while candidate_len > su_len: candidate_len -= len(candidate[0]) candidate = candidate[1:] if candidate_len == su_len: word = ''.join(candidate) if anagram(word) == su_an: print word candidate_len -= len(candidate[0]) candidate = candidate[1:]