Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
In the end a lot of code solution in Clear category for Moria Doors by Mysta
from string import punctuation
from typing import Dict, List, Set
DECIMALS: int = 6
def find_word(message: str) -> str:
# First, pick out the words from the text:
# 1. Eliminate all punctuation characters using translate()
# 2. Split the remaining text at whitespaces
trans: Dict[int, int | None] = str.maketrans("", "", punctuation)
words: List[str] = message.translate(trans).lower().split()
# Always remember the best results
best_likeness: float = 0
best_word: str = ""
# Loop over each word and calculate its overall likeness
for c, current_word in enumerate(words):
# It's not needed to calculate the likeness of each word separately
# because after all words are processed the sum of their likenesses
# is required. Hence we can continuously increase the likeness and
# will get that sum automatically!
likeness: float = 0
# The unique letters of the current word will be needed multiple
# times inside the following for loop
cset: Set[str] = set(current_word)
# Compare the current word with all other words
for w, word in enumerate(words):
if c == w:
# Don't check a word with itself!
continue
# If the first letters of the words are equal, then add 10%
if word[0] == current_word[0]:
likeness += 10
# If the last letters of the words are equal, then add 10%
if word[-1] == current_word[-1]:
likeness += 10
# Length comparison:
if (clen := len(current_word)) >= (wlen := len(word)):
likeness += (wlen / clen) * 30
else:
likeness += (clen / wlen) * 30
# Take all unique letters from the both words in one set and
# count how many letters appear in the both words.
# Add to the coefficient
# (common_letter_number / unique_letters_numbers) * 50
wset = set(word)
likeness += (
len(wset.intersection(cset))
/ len(set(current_word + word))
* 50
)
# If the current word has an equal or better likeness, then
# remember it as possible result
if round(likeness, DECIMALS) >= round(best_likeness, DECIMALS):
best_likeness = likeness
best_word = current_word
return best_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"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Jan. 10, 2026
Comments: