<jupyter_start><jupyter_text><NAME>For Matt's MPMP Scrabble Problem<jupyter_code>import itertools
from tqdm.notebook import tqdm
from collections import Counter
import pandas as pd
from scipy.special import comb<jupyter_output><empty_output><jupyter_text>First we define the letter distribution for the English Scrabble set.<jupyter_code>dist = [(0, '-', 2), (1, 'E', 12), (1, 'A', 9), (1, 'I', 9), 
        (1, 'O', 8), (1, 'N', 6), (1, 'R', 6), (1, 'T', 6), 
        (1, 'L', 4), (1, 'S', 4), (1, 'U', 4), (2, 'D', 4), 
        (2, 'G', 3), (3, 'B', 2), (3, 'C', 2), (3, 'M', 2), 
        (3, 'P', 2), (4, 'F', 2), (4, 'H', 2), (4, 'V', 2), 
        (4, 'W', 2), (4, 'Y', 2), (5, 'K', 1), (8, 'J', 1),
        (8, 'X', 1), (10, 'Q', 1), (10, 'Z', 1)]<jupyter_output><empty_output><jupyter_text>Next we turn this distribution of points into a dict for easy lookup.<jupyter_code>points_lookup = {l : p for p, l, _ in dist}<jupyter_output><empty_output><jupyter_text>Just testing...<jupyter_code>letter = 'D'
points = points_lookup[letter]

print(f'{letter} is worth {points} points.')<jupyter_output>D is worth 2 points.<jupyter_text>Now line up all of the letters.<jupyter_code>full_set = ''
for _, l, c in dist:
    full_set += l * c

full_set
def value(combo):
    global points_lookup
    return sum([points_lookup[l] for l in combo])<jupyter_output><empty_output><jupyter_text>Find out how many iterations this will take.<jupyter_code>num_tiles = 7

effort = int(comb(len(full_set), num_tiles))

print(f'Will take {effort} iterations.')<jupyter_output>Will take 16007560800 iterations.<jupyter_text>Just 16 billion iterations, it should be fine.Let's get counting...tqdm will give us a progress bar so it's less boring.<jupyter_code>target = 46

result = Counter()

seen = set()
for combo in tqdm(itertools.combinations(full_set, num_tiles), total=effort):
    combo = ''.join(combo)
    if combo not in seen:
        seen.add(combo)
        points = value(combo)
        result[points] += 1

the_answer = result[target]
        
print(f'\nFound {the_answer} ways to make a total of {target} points')<jupyter_output><empty_output><jupyter_text>Okay, done. Now we have the number of ways for every possible number of points.We will save this in a file so we don't have to wait 2 hours again.<jupyter_code>col0 = []
col1 = []
for k in sorted(result):
    col0.append(k)
    col1.append(result[k])
results_df = pd.DataFrame({'Total Points' : col0, 'Numer of Ways' : col1})

results_df.to_csv('scrabble_result.csv', index=False)

results_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16297160637884092, 'fraction_numerical': 0.03189420458965383, 'mean_word_length': 4.472340425531915, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '243374', 'license': 'Unlicense', 'n_tokens_mistral': 1043, 'n_tokens_neox': 972, 'n_words': 288, 'path': 'scrabble-solver.ipynb', 'repo_name': 'leechristie/mpmp-scrabble', 'size': '2580'}