Finishing Moria gates

master
Clément 12 months ago
parent f5059f11d1
commit 10190b0d23
  1. 80
      Storage/gate-puzzles.py

@ -0,0 +1,80 @@
def calculate_simil(word1: str, word2: str):
score = 0
if word1[0] == word2[0]:
score += 10
if word1[-1] == word2[-1]:
score += 10
length_of_word1 = len(word1)
length_of_word2 = len(word2)
score += (length_of_word1 / length_of_word2) * 30 \
if (length_of_word1 <= length_of_word2) \
else (length_of_word2 / length_of_word1) * 30
uniques = ''.join(set(word1 + word2))
commons = ''.join(set(word1).intersection(word2))
score += (len(commons) / len(uniques)) * 50
return score
def get_max_score_in_words_list(lists):
current_max = 0
for score in lists.values():
current_max = max([current_max, score])
maxes = []
for word, score in lists.items():
if score == current_max:
maxes.append(word)
return maxes
def calculate_scores(words):
scores = {}
for word in words:
scores[word] = []
i = 0
for word1 in words:
j = 0
for word2 in words:
if i == j:
continue
score = calculate_simil(word1, word2)
scores[word1].append(score)
scores[word2].append(score)
j += 1
i += 1
return scores
def calculate_average(scores):
avgs = {}
for word, score in scores.items():
avgs[word] = sum(score) / len(score)
return avgs
def find_word(message):
words = "".join((char.lower() if char.isalpha() else " ") for char in message).split()
scores = calculate_scores(words)
avgs = calculate_average(scores)
max_words = get_max_score_in_words_list(avgs)
if len(max_words) == 1:
return max_words[0]
for word in reversed(words):
if word in max_words:
return word
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"
Loading…
Cancel
Save