Solving Quartiles with Python (`itertools.permutations`)

 Solving Quartiles with Python

Quartiles is a fun game that was introduced to Apple's News+ (https://www.apple.com/newsroom/2024/05/apple-news-plus-introduces-quartiles-a-new-game-and-offline-mode-for-subscribers/). It involves matching tiles containing letters to words. While fun to play, it's also fun to solve with Python. This blog post will introduce how to solve the game with Python.


If you haven't been using Astral's uv package, check it out. Otherwise use Python's pip to install nltk to allow finding the words in English. This can be done by either:

uv add nltk

or

pip install nltk

Now for the fun part!

Create a script, such as main.py to include your setup.


# main.py
from itertools import permutations


tiles = ['ary', 'tion', 'rou', 'ed', 'pes', 'gh', 'ant', 'tig', 'des', 'ta', 'ly', 'olu', 'ro', 'ur', 'pi', 'ht', 'res', 'cab', 'hous', 'rev']

combinations_list = list(permutations(tiles, 4))
combinations_list.extend(list(permutations(tiles, 3)))
combinations_list.extend(list(permutations(tiles, 2)))
combinations_list.extend(list(permutations(tiles, 1)))
  

if __name__ == "__main__":
    import nltk
    from ntlk.corpus import words
    nltk.download('words')
    english_words = set(words.words())
    valid_words = []
    for combo in combinations_list:
        candidate_word = ''.join(combo).lower()
        if candidate_word in english_words:
            valid_words.append(candidate_word)
    print(valid_words)

Python's itertools.combinations will find all combinations of the tiles for 4 combinations, 3 combinations, 2 combinations, or a single tile that is a word.
And that's how you use Python to solve the Quartiles game!

Popular posts from this blog

Setup RStudio Server on Windows with a Virtual machine

Tufte and Few and the Human Connection