Skip to content

Instantly share code, notes, and snippets.

@pncnmnp
Created December 16, 2022 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pncnmnp/f745fe9ee1d807a0b3373aef0a33693a to your computer and use it in GitHub Desktop.
Save pncnmnp/f745fe9ee1d807a0b3373aef0a33693a to your computer and use it in GitHub Desktop.
Game of Life but with alphabets
# initialize the grid with random alphabets
import random
from itertools import permutations
from collections import Counter
import pickle
import enchant
# define the grid size
GRID_SIZE = 8
# relative frequency in the english language
# alpha_freq = {
# "a": 8.1,
# "b": 1.5,
# "c": 2.8,
# "d": 4.3,
# "e": 12.9,
# "f": 2.2,
# "g": 2,
# "h": 6.1,
# "i": 7,
# "j": 0.15,
# "k": 0.77,
# "l": 4,
# "m": 2.4,
# "n": 6.7,
# "o": 7.4,
# "p": 1.9,
# "q": 0.095,
# "r": 6,
# "s": 6.3,
# "t": 8.981,
# "u": 2.8,
# "v": 0.98,
# "w": 2.4,
# "x": 0.15,
# "y": 2,
# "z": 0.074,
# }
alpha_freq = {
"a": 3.846,
"b": 3.846,
"c": 3.846,
"d": 3.846,
"e": 3.846,
"f": 3.846,
"g": 3.846,
"h": 3.846,
"i": 3.846,
"j": 3.846,
"k": 3.846,
"l": 3.846,
"m": 3.846,
"n": 3.846,
"o": 3.846,
"p": 3.846,
"q": 3.846,
"r": 3.846,
"s": 3.846,
"t": 3.846,
"u": 3.846,
"v": 3.846,
"w": 3.846,
"x": 3.846,
"y": 3.846,
"z": 3.846,
}
def generate_grid():
population, weights = list(), list()
for alphabet, weight in alpha_freq.items():
population.append(alphabet)
weights.append(weight)
samples = random.choices(population, weights, k=10)
grid = [["." for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
grid[0][1] = samples.pop()
grid[1][2] = samples.pop()
grid[2][0] = samples.pop()
grid[2][1] = samples.pop()
grid[2][2] = samples.pop()
return grid, population, weights
def letter_to_words(letters, min_len):
# Based on code from Nate Crow
# https://gist.github.com/natecrow/28f96c7a1f87d446ddc8906b4a0c943b
# set up spell checker
dict = enchant.Dict("en_US")
# get permutations from the given letters and
# add it to list if it is an English word
words = list()
for i in range(min_len, len(letters) + 1):
for p in permutations(letters, i):
if dict.check("".join(p)):
words.append("".join(p))
words.sort(key=str.lower)
words.sort(key=len)
return words
def game_of_life(grid, population, weights):
new_grid = [["." for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
words = []
for i in range(GRID_SIZE):
for j in range(GRID_SIZE):
# count the number of neighbors
neighbors = 0
alphabets = []
for di in [-1, 0, 1]:
for dj in [-1, 0, 1]:
if di == 0 and dj == 0:
continue
if 0 <= i + di < GRID_SIZE and 0 <= j + dj < GRID_SIZE:
if grid[i + di][j + dj] != ".":
neighbors += 1
alphabets += list(grid[i + di][j + dj])
# apply the rules of the game
if grid[i][j] == ".":
# if a cell is dead and has exactly three neighbors, it becomes alive
if neighbors == 3:
new_grid[i][j] = random.choices(population, weights)[0]
else:
# if a cell is alive and has less than two or more than three neighbors, it dies
if neighbors < 2 or neighbors > 3:
new_grid[i][j] = "."
# it lives
else:
alphabets += list(grid[i][j])
new_grid[i][j] = set(alphabets)
words += letter_to_words("".join(set(alphabets)), 3)
return new_grid, words
if __name__ == "__main__":
counter = Counter()
for iterations in range(100):
print(iterations)
grid, population, weights = generate_grid()
word_list = []
for _ in range(4):
grid, words = game_of_life(grid, population, weights)
word_list += words
word_list = list(set(word_list))
for word in word_list:
counter[word] += 1
if iterations % 100 == 0:
file = open("common_words_random", "wb")
pickle.dump(counter.most_common(), file)
file.close()
print(counter.most_common()[:100])
@pncnmnp
Copy link
Author

pncnmnp commented Dec 16, 2022

This code is licensed under MIT License:

Copyright 2022 Parth Parikh

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment