A script that helps you guess words in skribblio
Here is the concept in Python, because I'm still learning JavaScript.
def levenshtein_distance(a, b):
# Initialize a matrix of size (len(s1)+1) x (len(s2)+1) with all values set to 0
matrix = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
# Initialize the first row and column of the matrix
for i in range(len(a) + 1):
matrix[i][0] = i
for j in range(len(b) + 1):
matrix[0][j] = j
# Compute the Levenshtein distance
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
if a[i - 1] == b[j - 1]:
matrix[i][j] = matrix[i - 1][j - 1]
else:
matrix[i][j] = min(
matrix[i - 1][j] + 1, # deletion
matrix[i][j - 1] + 1, # insertion
matrix[i - 1][j - 1] + 1 # substitution
)
# Return the Levenshtein distance (last element in the matrix)
return matrix[-1][-1]
def play_game():
global words
print(words)
while True:
guess = input("User Guess: ")
if guess == target:
print(f"{guess} is correct!\n{[target]}")
return
if levenshtein_distance(guess, target) == 1:
print(f"{guess} is close!")
# Filter words that have a Levenshtein distance of 1 from the guess
close_words = []
for word in words:
if levenshtein_distance(guess, word) == 1:
close_words.append(word)
words = close_words
else:
# Filter words that have a Levenshtein distance greater than 1 from the guess
distant_words = []
for word in words:
if levenshtein_distance(guess, word) > 1:
distant_words.append(word)
words = distant_words
print(words)
target = 'dune'
words = ['crumb', 'dime', 'dome', 'done', 'drum', 'dune', 'dunes', 'ring', 'rung', 'sing', 'song', 'sung']
play_game()
def levenshtein_distance(a, b): # Initialize a matrix of size (len(s1)+1) x (len(s2)+1) with all values set to 0.
matrix = [[0] * (len(b) + 1) for _ in range(len(a) + 1)]
# Initialize the first row and column of the matrix.
for i in range(len(a) + 1):
matrix[i][0] = i
for j in range(len(b) + 1):
matrix[0][j] = j
# Compute the Levenshtein distance.
for i in range(1, len(a) + 1):
for j in range(1, len(b) + 1):
if a[i - 1] == b[j - 1]:
matrix[i][j] = matrix[i - 1][j - 1]
else:
matrix[i][j] = min(
matrix[i - 1][j] + 1, # deletion.
matrix[i][j - 1] + 1, # insertion.
matrix[i - 1][j - 1] + 1 # substitution.
)
# Return the Levenshtein distance (last element in the matrix).
return matrix[-1][-1]
def play_game(): global words print(words) while True: guess = input("User Guess: ")
if guess == target:
print(f"{guess} is correct!\n{[target]}")
return
if levenshtein_distance(guess, target) == 1:
print(f"{guess} is close!")
# Filter words that have a Levenshtein distance of 1 from the guess
close_words = []
for word in words:
if levenshtein_distance(guess, word) == 1:
close_words.append(word)
words = close_words
else:
# Filter words that have a Levenshtein distance greater than 1 from the guess
distant_words = []
for word in words:
if levenshtein_distance(guess, word) > 1:
distant_words.append(word)
words = distant_words
print(words)
target = 'dune' words = ['crumb', 'dime', 'dome', 'done', 'drum', 'dune', 'dunes', 'ring', 'rung', 'sing', 'song', 'sung'] play_game()
If your guess is not a "close word," then other words that are 1 letter different can't be the answer and can therefore be removed.