Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Moria Doors by Sim0000
def find_word(message):
def f(s1, s2):
point = 10 * ((s1[0] == s2[0]) + (s1[-1] == s2[-1]))
point += 30 * min(len(s1), len(s2)) / max(len(s1), len(s2))
point += 50 * len(set(s1) & set(s2)) / len(set(s1 + s2))
return point
m = ''.join(c if c.islower() else ' ' for c in message.lower()).split()
score = {}
for i, w1 in enumerate(m):
score[i] = sum(f(w1, w2) for j, w2 in enumerate(m) if i != j)
return m[max(reversed(range(len(m))), key=score.get)]
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert find_word("Speak friend and enter.") == "friend", "Friend"
assert find_word("Beard and Bread") == "bread", "Bread is Beard"
assert find_word("The Doors of Durin, Lord of Moria. Speak friend and enter. "
"I Narvi made them. Celebrimbor of Hollin drew these signs") == "durin", "Durin"
assert find_word("Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy."
" According to a researcher at Cambridge University.") == "according", "Research"
assert find_word("One, two, two, three, three, three.") == "three", "Repeating"
Sept. 24, 2014
Comments: