lots of corrections !
This commit is contained in:
parent
92c27b8230
commit
2cec5324f6
100
wgamesolv.py
Normal file → Executable file
100
wgamesolv.py
Normal file → Executable file
@ -12,19 +12,21 @@ parser = argparse.ArgumentParser(
|
||||
parser.add_argument(
|
||||
'-d',
|
||||
'--dictionary',
|
||||
help='Dictionnaire des mots du jeu'
|
||||
help='Dictionnaire des mots du jeu',
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-fl',
|
||||
'--firstLetter',
|
||||
help='Première lettre'
|
||||
help='Première lettre',
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-nb',
|
||||
'--nbLetters',
|
||||
type=int,
|
||||
help='Nombre de lettres, un entier (default: %(default)s)',
|
||||
default=5
|
||||
help='Nombre de lettres, un entier',
|
||||
required=True
|
||||
)
|
||||
parser.add_argument(
|
||||
'-kl',
|
||||
@ -51,23 +53,35 @@ parser.add_argument(
|
||||
'-ft',
|
||||
'--firstTry',
|
||||
type=int,
|
||||
help="Sélectionne des mots commençants par firstLetter et composés d'un maximum de voyelles lettres différentes: -ft 5",
|
||||
help="Sélectionne des mots commençants par firstLetter et composés de ft voyelles différentes: -ft 4",
|
||||
)
|
||||
|
||||
if not len(sys.argv) > 1:
|
||||
print("-h or --help for synthaxe and options")
|
||||
print("-h or --help pour afficher l'aide")
|
||||
exit(1)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Dictionary
|
||||
if args.dictionary:
|
||||
fp = args.dictionary
|
||||
try :
|
||||
f = open(fp)
|
||||
liste = json.load(f)
|
||||
except:
|
||||
print("Impossible d'ouvrir le fichier" + fp)
|
||||
|
||||
# First Letter
|
||||
if args.firstLetter:
|
||||
fl = args.firstLetter
|
||||
else:
|
||||
fl = False
|
||||
# Nbr Letters
|
||||
nb = args.nbLetters
|
||||
|
||||
pattern = False
|
||||
# Nbr Letters
|
||||
if args.firstLetter:
|
||||
nb = args.nbLetters
|
||||
else:
|
||||
nbLetters = False
|
||||
|
||||
# Pattern
|
||||
if args.pattern:
|
||||
@ -75,36 +89,37 @@ if args.pattern:
|
||||
if args.nbLetters and len(pattern) != nb:
|
||||
print("Tu as merdé ta pattern petit scarabé !")
|
||||
exit(2)
|
||||
else:
|
||||
pattern = False
|
||||
|
||||
# Known Letters
|
||||
if args.knownLetters:
|
||||
kl = args.knownLetters
|
||||
else:
|
||||
kl = False
|
||||
|
||||
# Bad Letters
|
||||
if args.badLetters:
|
||||
bl = args.badLetters
|
||||
else:
|
||||
bl = False
|
||||
|
||||
# No Double
|
||||
if args.noDoubleLetters:
|
||||
nd = True
|
||||
else: nd = False
|
||||
|
||||
# First Try
|
||||
if args.firstTry:
|
||||
ft = args.firstTry
|
||||
else: ft = False
|
||||
|
||||
fp = args.dictionary
|
||||
f = open(fp)
|
||||
liste = json.load(f)
|
||||
|
||||
noDoubleLetters = []
|
||||
patternFilter = []
|
||||
klFilter = []
|
||||
blFilter = []
|
||||
ftFilter = []
|
||||
|
||||
# Fonction d'élimination de mots contenant plusieurs occurences d'une ou plusieurs lettres
|
||||
# Fonction d'élimination de mots contenant plusieurs occurences de lettres
|
||||
def noDouble(mots):
|
||||
ndList = []
|
||||
for mot in mots:
|
||||
@ -116,6 +131,7 @@ def noDouble(mots):
|
||||
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 = []
|
||||
@ -128,33 +144,36 @@ def firstTryFilter(mots, ft):
|
||||
bestWords.append(mot)
|
||||
return(bestWords)
|
||||
|
||||
|
||||
# Fonction de comparaison des lettres d'un mot, avec celle de la pattern
|
||||
'''
|
||||
def patternCheck(pattern, mot, l):
|
||||
goodPosCounter = 0
|
||||
for pos in range(len(pattern)):
|
||||
# print("Checking : " + l + "against : " + pattern[pos] + " and " + mot[pos])
|
||||
print("Checking : " + l + "against : " + pattern[pos] + " and " + mot[pos])
|
||||
if pattern[pos] == l and mot[pos] == l:
|
||||
goodPosCounter +=1
|
||||
# print("checked")
|
||||
print("checked")
|
||||
if goodPosCounter == pattern.count(l):
|
||||
return(0)
|
||||
else:
|
||||
return(1)
|
||||
'''
|
||||
|
||||
# Suppression de tous les mots ne commençants pas par FirstLetter
|
||||
# First Letter : élimination des mots ne commençant pas par FirstLetter
|
||||
if fl:
|
||||
goodFl = [mot for mot in liste if mot.startswith(fl)]
|
||||
|
||||
# Suppression des mots n'ayant pas le bon nombre de lettres
|
||||
# Nb Lettres : élimination des mots n'ayant pas le bon nombre de lettres
|
||||
if fl:
|
||||
goodLen = [mot for mot in goodFl if len(mot) == nb]
|
||||
else:
|
||||
goodLen = [mot for mot in liste if len(mot) == nb]
|
||||
|
||||
# First Try
|
||||
if ft:
|
||||
bestWords = noDouble(firstTryFilter(goodLen, ft))
|
||||
print(bestWords)
|
||||
|
||||
# Pattern : élimination des mots ne satisfaisant pas la pattern
|
||||
if pattern:
|
||||
patternCount = len(pattern) - pattern.count(".")
|
||||
for mot in goodLen:
|
||||
@ -165,9 +184,7 @@ if pattern:
|
||||
if patternEval == patternCount :
|
||||
patternFilter.append(mot)
|
||||
|
||||
# print(len(patternFilter))
|
||||
# print(patternFilter)
|
||||
|
||||
# Known Letters : élimination des mots ne comprenant pas les lettres validées (hors pattern)
|
||||
if kl:
|
||||
if patternFilter:
|
||||
mots = patternFilter
|
||||
@ -180,11 +197,9 @@ if kl:
|
||||
validate += 1
|
||||
if validate == len(kl):
|
||||
klFilter.append(mot)
|
||||
# print(len(klFilter))
|
||||
# print(klFilter)
|
||||
|
||||
# Bad Letters : élimination des mots contenant des lettres invalidées (hors pattern)
|
||||
if bl:
|
||||
print(bl)
|
||||
if klFilter:
|
||||
mots = klFilter
|
||||
elif patternFilter:
|
||||
@ -194,10 +209,33 @@ if bl:
|
||||
for mot in mots:
|
||||
invalidate = 0
|
||||
for l in bl:
|
||||
if l in mot:
|
||||
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)
|
||||
|
||||
print(len(blFilter))
|
||||
print(blFilter)
|
||||
|
||||
# Affiche du résultat
|
||||
if pattern and kl and bl:
|
||||
print(", ".join(blFilter))
|
||||
print(str(len(blFilter)) + " mots dans la liste" )
|
||||
elif pattern and kl:
|
||||
print(", ".join(klFilter))
|
||||
print(str(len(klFilter)) + " mots dans la liste" )
|
||||
elif pattern:
|
||||
print(", ".join(patternFilter))
|
||||
print(str(len(patternFilter)) + " mots dans la liste" )
|
||||
elif kl and bl:
|
||||
print(", ".join(blFilter))
|
||||
print(str(len(blFilter)) + " mots dans la liste" )
|
||||
elif bl and pattern:
|
||||
print(", ".join(blFilter))
|
||||
print(str(len(blFilter)) + " mots dans la liste" )
|
||||
elif ft:
|
||||
print(", ".join(bestWords))
|
||||
print(str(len(bestWords)) + " mots dans la liste" )
|
||||
else:
|
||||
print(", ".join(goodLen))
|
||||
print(str(len(goodLen)) + " mots dans la liste" )
|
||||
|
Loading…
Reference in New Issue
Block a user