Réorganisation du code

This commit is contained in:
Antoine Van Elstraete 2022-01-24 19:44:49 +01:00
parent 8b9175454b
commit 0a8c29de74

View File

@ -4,6 +4,118 @@
import json
import argparse
def load_dict(dict_file):
try :
with open(dict_file, "r") as dico:
liste = json.load(dico)
return liste
except:
print("Impossible d'ouvrir le fichier" + fp)
exit(2)
def noDouble(mots):
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
ndList = []
for mot in mots:
llist = []
for l in mot:
if not l in llist:
llist.append(l)
if len(mot) == len(llist):
ndList.append(mot)
return(ndList)
# First Try : sélectionne des mots ayant des chances de valider ou invalider des voyelles
def firstTryFilter(mots, ft):
vLetters = ['A', 'E', 'I', 'O', 'U', 'Y']
bestWords = []
for mot in mots:
vCount = 0
for l in mot:
if l in vLetters:
vCount +=1
if vCount >= ft:
bestWords.append(mot)
return(bestWords)
# Fonction de comparaison des lettres d'un mot, avec celle de la pattern
# Plus nécessaire actuellement mais peut être dans une prochaine vie
'''
def patternCheck(pattern, mot, l):
goodPosCounter = 0
for pos in range(len(pattern)):
print("Checking : " + l + "against : " + pattern[pos] + " and " + mot[pos])
if pattern[pos] == l and mot[pos] == l:
goodPosCounter +=1
print("checked")
if goodPosCounter == pattern.count(l):
return(0)
else:
return(1)
'''
def resolv_fl(liste, firstletter):
# On ne garde que les mots qui commence par FirstLetter
return [mot for mot in liste if mot[0] == firstletter]
def resolv_len(liste, lenght):
# On ne garde que les mots qui ont la bonne longueur
return [mot for mot in liste if len(mot) == lenght]
# First Try
#if ft:
# bestWords = noDouble(firstTryFilter(goodLen, ft))
# Pattern : élimination des mots ne satisfaisant pas la pattern
#if pattern:
# patternCount = len(pattern) - pattern.count(".")
# for mot in goodLen:
# patternEval = 0
# for l in range(len(mot)):
# if mot[l] == pattern[l]:
# patternEval += 1
# if patternEval == patternCount :
# patternFilter.append(mot)
#
# Known Letters : élimination des mots ne comprenant pas les lettres validées (hors pattern)
#if kl:
# if patternFilter:
# mots = patternFilter
# else:
# mots = goodLen
# for mot in mots:
# validate = 0
# for l in kl:
# if l in mot:
# validate += 1
# if validate == len(kl):
# klFilter.append(mot)
# Bad Letters : élimination des mots contenant des lettres invalidées (hors pattern)
#if bl:
# if klFilter:
# mots = klFilter
# elif patternFilter:
# mots = patternFilter
# else:
# mots = goodLen
# for mot in mots:
# invalidate = 0
# for l in bl:
# if l in mot and l in pattern:
# if pattern.count(l) != mot.count(l):
# invalidate += 1
# elif l in mot:
# invalidate += 1
# if invalidate == 0:
# blFilter.append(mot)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Cheating with SUTOM'
)
@ -63,22 +175,7 @@ parser.add_argument(
args = parser.parse_args()
def load_dict(dict_file):
try :
with open(dict_file, "r") as dico:
liste = json.load(dico)
except:
print("Impossible d'ouvrir le fichier" + fp)
# First letter
fl = args.firstLetter
# Nbr Letters
nb = args.nbLetters
# Pattern
pattern = args.pattern
if pattern and args.nbLetters and len(pattern) != nb:
if args.pattern and args.nbLetters and len(pattern) != nb:
print("Tu as merdé ta pattern petit scarabé !")
exit(2)
@ -91,115 +188,9 @@ bl = args.badLetters
# First Try
ft = args.firstTry
patternFilter = []
klFilter = []
blFilter = []
ftFilter = []
def noDouble(mots):
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
ndList = []
for mot in mots:
llist = []
for l in mot:
if not l in llist:
llist.append(l)
if len(mot) == len(llist):
ndList.append(mot)
return(ndList)
# First Try : sélectionne des mots ayant des chances de valider ou invalider des voyelles
def firstTryFilter(mots, ft):
vLetters = ['A', 'E', 'I', 'O', 'U', 'Y']
bestWords = []
for mot in mots:
vCount = 0
for l in mot:
if l in vLetters:
vCount +=1
if vCount >= ft:
bestWords.append(mot)
return(bestWords)
# Fonction de comparaison des lettres d'un mot, avec celle de la pattern
# Plus nécessaire actuellement mais peut être dans une prochaine vie
'''
def patternCheck(pattern, mot, l):
goodPosCounter = 0
for pos in range(len(pattern)):
print("Checking : " + l + "against : " + pattern[pos] + " and " + mot[pos])
if pattern[pos] == l and mot[pos] == l:
goodPosCounter +=1
print("checked")
if goodPosCounter == pattern.count(l):
return(0)
else:
return(1)
'''
def resolv_fl(liste, firstletter):
# On ne garde que les mots qui commence par FirstLetter
return [mot for mot in liste if mot[0] == firstletter]
def resolv_len(liste, lenght):
# On ne garde que les mots qui ont la bonne longueur
return [mot for mot in liste if len(mot) == lenght]
# First Try
if ft:
bestWords = noDouble(firstTryFilter(goodLen, ft))
# Pattern : élimination des mots ne satisfaisant pas la pattern
if pattern:
patternCount = len(pattern) - pattern.count(".")
for mot in goodLen:
patternEval = 0
for l in range(len(mot)):
if mot[l] == pattern[l]:
patternEval += 1
if patternEval == patternCount :
patternFilter.append(mot)
# Known Letters : élimination des mots ne comprenant pas les lettres validées (hors pattern)
if kl:
if patternFilter:
mots = patternFilter
else:
mots = goodLen
for mot in mots:
validate = 0
for l in kl:
if l in mot:
validate += 1
if validate == len(kl):
klFilter.append(mot)
# Bad Letters : élimination des mots contenant des lettres invalidées (hors pattern)
if bl:
if klFilter:
mots = klFilter
elif patternFilter:
mots = patternFilter
else:
mots = goodLen
for mot in mots:
invalidate = 0
for l in bl:
if l in mot and l in pattern:
if pattern.count(l) != mot.count(l):
invalidate += 1
elif l in mot:
invalidate += 1
if invalidate == 0:
blFilter.append(mot)
if __name__ == "__main__":
liste = load_dict(args.dictionary)
liste = resolv_len(liste, nb)
liste = resolv_fl(liste, fl)
liste = resolv_len(liste, args.nbLetters)
liste = resolv_fl(liste, args.firstLetter)
if args.noDoubleLetters:
liste = noDouble(liste)
if len(liste) > 1: