<jupyter_start><jupyter_text>Jouer avec les mots Les mots utilisés dans ce chapitre sont des mots français pour le Scrabble de <NAME>, disponible sous http://jph.durand.free.fr/scrabble.txt  Lire des listes de mots<jupyter_code>fin = open('mots.txt')<jupyter_output><empty_output><jupyter_text>Le premier mot est *AA* qui est une sorte de lave. Les mots se terminent par le caractère de saut de ligne `\n` (newline).<jupyter_code>fin.readline()<jupyter_output><empty_output><jupyter_text>L'objet `fin` garde en mémoire la ligne lu, et continue avec la prochaine ligne.<jupyter_code>fin.readline()<jupyter_output><empty_output><jupyter_text>Nous pouvons nous débarasser du newline avec la fonction `strip`<jupyter_code>fin.readline().strip()
for i in range(10):
    print(fin.readline().strip())<jupyter_output>KA
LA
LE
LI
LU
MA
ME
MI
MU
NA<jupyter_text>ex 1 - Mots avec 14 caractères<jupyter_code>fin = open('mots.txt')
n = 0
for line in fin:
    line = line.strip()
    if len(line)==14:
        print(line)
        n += 1
    if n>20:
        break<jupyter_output>ABANDONNERIONS
ABASOURDISSAIT
ABASOURDISSANT
ABASOURDISSENT
ABOLITIONNISME
ABOLITIONNISTE
ABOMINABLEMENT
ABOUTISSEMENTS
ABRACADABRANTE
ABRACADABRANTS
ABSTIENDRAIENT
ACADEMICIENNES
ACCAPARERAIENT
ACCELERATRICES
ACCELERERAIENT
ACCENTUERAIENT
ACCESSOIREMENT
ACCESSOIRISTES
ACCLIMATERIONS
ACCOMMODEMENTS
ACCOMMODERIONS<jupyter_text>ex 2 - mots sans E<jupyter_code>def sans_e(mot):
    return not 'E' in mot

sans_e('HELLO'), sans_e('CIAO')
fin = open('mots.txt')
n = 0
for line in fin:
    line = line.strip()
    if len(line)>14 and sans_e(line):
        print(line)
        n += 1
    if n>10:
        break
fin = open('mots.txt')
n = 0
i = 0
for line in fin:
    line = line.strip()
    n += 1
    if sans_e(line):
        i += 1
print(i, i/n)<jupyter_output>18020 0.1380240048407975<jupyter_text>ex 3 - eviter<jupyter_code>def evite(mot, interdit):
    for c in mot:
        if c in interdit: 
            return False
    return True

evite('hello', 'epfl'), evite('ciao', 'epfl')
interdit = input('forbidden letters = ')
fin = open('mots.txt')
interdit = interdit.upper()
n = 0
k = 0
for line in fin:
    line = line.strip()
    if evite(line, interdit):
        n += 1
        if k < 10:
            print(line)
            k += 1
print(n)<jupyter_output>forbidden letters = AIUEO
VS
CPT
DRY
GYM
LYS
PFF
PST
PSY
GYMS
LYNX
13<jupyter_text>ex 4 - utiliser seulement<jupyter_code>def utilise(mot, permis):
    for c in mot:
        if c not in permis:
            return False
    return True

utilise('hello', 'helpo'), utilise('hello', 'hefko'), 
permis = input('allowed letters = ')
fin = open('mots.txt')
permis = permis.upper()
n = 0
k = 0
for line in fin:
    line = line.strip()
    if utilise(line, permis):
        n += 1
        if k < 10:
            print(line)
            k += 1
print(n)<jupyter_output>allowed letters = acefhlo
AA
AH
CA
CE
EH
FA
HA
HE
HO
LA
122<jupyter_text>ex 5 - utiliser tous<jupyter_code>def utilise_tout(mot, lettres):
    for c in lettres:
        if c not in mot:
            return False
    return True

utilise_tout('hello', 'hel'), utilise_tout('hello', 'helk')
lettres = 'aeiouy'.upper()
fin = open('mots.txt')
n = 0
k = 0
for line in fin:
    line = line.strip()
    if utilise_tout(line, lettres):
        n += 1
        if k < 10:
            print(line)
            k += 1
print(n)<jupyter_output>NOYAUTIEZ
NOYAUTERAI
ROUGEOYAIT
RUDOYAIENT
TUTOYAIENT
YAOURTIERE
COUDOYAIENT
LOUVOYAIENT
NOYAUTAIENT
NOYAUTERAIS
60<jupyter_text>ex 6 - ordre alphabétique<jupyter_code>def est_en_ordre_alphabetique(mot):
    n = len(mot)
    for i in range(n-1):
        if mot[i]>mot[i+1]:
            return False
    return True

est_en_ordre_alphabetique('hello'), est_en_ordre_alphabetique('bello'),
fin = open('mots.txt')
n = 0
k = 0
for line in fin:
    line = line.strip()
    if est_en_ordre_alphabetique(line):
        n += 1
        if 180 < n and k<10:
            print(line)
            k += 1
print(n)<jupyter_output>ABBES
ABCES
ACCES
ACCRU
AGIOS
ALORS
BIJOU
BOSSU
CEINT
CHIOT
205<jupyter_text>Exercices  ex - 3 fois une lettre double Trouver un mot avec 3 fois une lettre double, conséqutifs.<jupyter_code>def double_letters(s):
    n = len(s)
    for i in range(n-5):
        if s[i] == s[i+1] and s[i+2] == s[i+3] and s[i+4] == s[i+5]:
            return True
    return False

double_letters('hello'), double_letters('helloo'), double_letters('heelloo'), 
fin = open('mots.txt')
for line in fin:
    word = line.strip()
    if double_letters(word):
        print(word)<jupyter_output>IRREELLE
IRREELLES<jupyter_text>ex - palindromes<jupyter_code>for i in range(1000, 9999):
    s = str(i)
    if (s[0]==s[3] and s[1]==s[2]):
        print(i)
for i in range(100000, 999999):
    s = str(i)
    if (s[2]==s[5] and s[3]==s[4]):
        i += 1
        s = str(i)
        if (s[1]==s[5] and s[2]==s[4]):
            i += 1
            s = str(i)
            if (s[1]==s[4] and s[2]==s[3]):
                i += 1
                s = str(i)
                if (s[0]==s[5] and s[1]==s[4]) and s[2]==s[3]:
                    print(i-3)<jupyter_output>198888
199999<jupyter_text>ex age<jupyter_code>for dif in range(15, 100):
    n = 0
    for mom in range(dif, 100):
        s = str(mom-dif).zfill(2)
        m = str(mom)
        if s[0]==m[1] and s[1]==m[0]:
            n += 1
    if n == 8:
        print(dif)
dif = 18
for mom in range(dif, 100):
    s = str(mom-dif).zfill(2)
    m = str(mom)
    if s[0]==m[1] and s[1]==m[0]:
        print(s, m)<jupyter_output>02 20
13 31
24 42
35 53
46 64
57 75
68 86
79 97
{'fraction_non_alphanumeric': 0.12724684107492437, 'fraction_numerical': 0.03755116568784481, 'mean_word_length': 2.9745403111739743, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '73277', 'license': 'MIT', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2309, 'n_words': 579, 'path': 'thinkpython/09_word_games.ipynb', 'repo_name': 'Bugnon/oc-2018', 'size': '5633'}