def solution():
    """
    `protectorate` is 12 letters long and encodes to `.--..-.----.-.-.----.-..--.`, 
    which is a palindrome. Find the only 13-letter word that encodes to a 
    palindrome.

    Returns: `intransigence`
    """
    def __is_palindrome(text):
        idx = 0
        max_idx = int(len(text) / 2)
        while idx < max_idx and text[idx] == text[-(idx + 1)]:
            idx += 1

        if idx == max_idx: return True
        return False

    assert __is_palindrome(smorse("protectorate"))

    valid_palindromes = set()
    for word in word_list:
        if len(word) != 13: continue
        if __is_palindrome(smorse(word)): valid_palindromes.add(word)

    assert len(valid_palindromes) == 1
    return valid_palindromes.pop()
Ejemplo n.º 2
0
def solution() -> str:
    """
    Call a word perfectly balanced if its code has the same number of dots as 
    dashes. `counterdemonstrations` is one of two 21-letter words that’s 
    perfectly balanced. Find the other one.
    
    Returns: `overcommercialization`
    """

    valid_balanced_words = set()
    for word in word_list:
        if len(word) != 21: continue

        smorsed_word = smorse(word)
        difference = 0
        for c in smorsed_word:
            if c == ".": difference += 1
            else: difference -= 1

        if difference == 0: valid_balanced_words.add(word)

    assert len(valid_balanced_words) == 2
    assert "counterdemonstrations" in valid_balanced_words
    valid_balanced_words.discard("counterdemonstrations")

    return valid_balanced_words.pop()
Ejemplo n.º 3
0
    def add_word(self, word) -> int:
        """
        Add the smooshed morse code representation of `word` to the trie.

        Returns: the number of words with that smooshed morse code representation.
        """
        self.add_morse_code(smorse(word))
Ejemplo n.º 4
0
def solution() -> str:
    """
    `autotomous` encodes to `.-..--------------..-...`, which has 14 dashes in 
    a row. Find the only word that has 15 dashes in a row.

    Returns: `bottommost` which encodes to `-...---------------...-`
    """
    for word in word_list:
        smorsed_word = smorse(word)
        if "---------------" in smorsed_word:
            return word
    return None
Ejemplo n.º 5
0
def solution() -> List[str]:
    """
    `--.---.---.--` is one of five 13-character sequences that does not appear 
    in the encoding of any word. Find the other four.

    Returns: `['---.---.-----', '--.---.------', '---.----.----', '---.---.---.-']`
    """
    valid_sequences = set()
    search_space = []
    for word in word_list:
        search_space.append(smorse(word))

    search_space = "+".join(search_space)  # Any char that's not '-' or '.'

    for sequence in morse_sequences_of_length_n(13):
        if sequence not in search_space:
            valid_sequences.add(sequence)

    assert len(valid_sequences) == 5
    assert "--.---.---.--" in valid_sequences

    valid_sequences.remove("--.---.---.--")
    return list(valid_sequences)
Ejemplo n.º 6
0
 def word_exists(self, word) -> bool:
     """Returns: `True` if `words` smorse exists in the trie."""
     return self.morse_code_exists(smorse(word))