solution42 Algorithm

The solution42 Algorithm is a cutting-edge artificial intelligence-based technique designed to solve complex problems and optimize various processes in diverse domains. This advanced algorithm leverages the power of machine learning, deep learning, natural language processing, and other AI methodologies to analyze large datasets, identify patterns, and generate predictions. It is built on a well-structured and adaptive framework, allowing it to address a wide array of challenges, from optimizing business operations and enhancing customer experiences to supporting scientific research and promoting sustainable development. One of the key features of the solution42 Algorithm is its ability to adapt and improve over time. As it processes more data and gains more experience, the algorithm refines its models and strategies, leading to more accurate and efficient outcomes. This self-learning capability allows the solution42 Algorithm to stay ahead of the curve and provide valuable insights that can drive innovation and growth. Furthermore, the algorithm's versatility and scalability make it applicable to various industries, including healthcare, finance, retail, and manufacturing, where it can help organizations optimize their resources, streamline processes, and make well-informed decisions that can lead to success in today's highly competitive and rapidly evolving world.
"""
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so
the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its
alphabetical position and adding these values we form a word value. For example,
the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a
triangle number then we shall call the word a triangle word.

Using words.txt (right click and 'Save Link/Target As...'), a 16K text file
containing nearly two-thousand common English words, how many are triangle
words?
"""
import os


# Precomputes a list of the 100 first triangular numbers
TRIANGULAR_NUMBERS = [int(0.5 * n * (n + 1)) for n in range(1, 101)]


def solution():
    """
    Finds the amount of triangular words in the words file.

    >>> solution()
    162
    """
    script_dir = os.path.dirname(os.path.realpath(__file__))
    wordsFilePath = os.path.join(script_dir, "words.txt")

    words = ""
    with open(wordsFilePath, "r") as f:
        words = f.readline()

    words = list(map(lambda word: word.strip('"'), words.strip("\r\n").split(",")))
    words = list(
        filter(
            lambda word: word in TRIANGULAR_NUMBERS,
            map(lambda word: sum(map(lambda x: ord(x) - 64, word)), words),
        )
    )
    return len(words)


if __name__ == "__main__":
    print(solution())

LANGUAGE:

DARK MODE: