Skip to content

Instantly share code, notes, and snippets.

@brunomsantiago
Created February 1, 2022 04:16
Show Gist options
  • Save brunomsantiago/e20369c5a27c86fc210ce1fe0f9725df to your computer and use it in GitHub Desktop.
Save brunomsantiago/e20369c5a27c86fc210ce1fe0f9725df to your computer and use it in GitHub Desktop.
Beating term.ooo game by brute force (version of wordly in portuguese)
from urllib.request import urlopen
import unicodedata
def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
return u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
def has_letters(word, letters=''):
return all((letter in word for letter in letters))
def hasnt_letters(word, letters=''):
return all((letter not in word for letter in letters))
def right_place(word, right_places='*****'):
return all([word_letter == right_letter or right_letter == '*'
for word_letter, right_letter
in zip(word, right_places)])
def wrong_place(word, wrong_places='*****'):
return all([word_letter != wrong_letter or wrong_letter == '*'
for word_letter, wrong_letter
in zip(word, wrong_places)])
def word_round(input_words, has=None, hasnt=None, right=None, wrong=None):
remaining_words = input_words
if has:
remaining_words = [
word for word in remaining_words if has_letters(word, has)]
if hasnt:
remaining_words = [
word for word in remaining_words if hasnt_letters(word, hasnt)]
if right:
remaining_words = [
word for word in remaining_words if right_place(word, right)]
if wrong:
remaining_words = [
word for word in remaining_words if wrong_place(word, wrong)]
return remaining_words
url = 'https://raw.githubusercontent.com/pythonprobr/palavras/master/palavras.txt'
file = urlopen(url)
all_words = file.read().decode('utf-8').split('\n')
five_letter_words = [remove_accents(word).lower()
for word
in all_words
if (len(word) == 5) and ('-' not in word)]
five_letter_words = list(set(five_letter_words))
result = word_round(five_letter_words, has='aeio')
result_string = ' '.join(result)
print(f'\nPossible tries for first round:\n {result_string}')
# 1st TRY
# Input
tried = 'feiao'
tries = 1
# Results from term.ooo
has = 'feo'
hasnt = 'ia'
right = 'f****'
wrong = '*e**o'
# Possibles tries for next round
result = word_round(five_letter_words, has, hasnt, right, wrong)
result_string = ' '.join(result)
print(f'\nRound #{tries}. tried {tried}')
print(f'Possible tries for next round:\n {result_string}')
# 2nd TRY
# Input
tried = 'fonte'
tries += 1
# Results from term.ooo
has = 'fot'
hasnt = 'ian'
right = 'fo*te'
wrong = None
# Possibles tries for next round
result = word_round(five_letter_words, has, hasnt, right, wrong)
result_string = ' '.join(result)
print(f'\nRound #{tries}. tried {tried}')
print(f'Possible tries for next round:\n {result_string}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment