#! /usr/bin/env python3 from random import randint, sample from string import ascii_lowercase # 1 booléens def q11a(a, b): return (a%2 + b%2) == 1 and (a%3 + b%3) == 0 assert q11a(3, 6) == True def q11b(x): return 3 <= len(x) <= 10 and x[0] == 'a' assert q11b("abricot") == True def q12(a, b, x): assert not q11a(a, b) == (a%2 + b%2 != 1) or (a%3 + b%3 != 0) assert not q11b(x) == (len(x) < 3 or 10 < len(x) or x[0] != 'a') for _ in range(10): a = randint(0, 12) b = randint(0, 12) c = ''.join(sample(ascii_lowercase, randint(0, 12))) # 2 mot le plus long def q2(): w = input("mot ? ") n = 0 first = w longest = w while w != 'stop': n += 1 if w < first: first = w if len(w) > len(longest): longest = w w = input("mot ? ") print(n, "mots") if n > 0: print("premier :", first) print("plus long :", longest) #q2() # 4 scrabble values = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 10, 1, 2, 1, 1, 3, 8, 1, 1, 1, 1, 4, 10, 10, 10, 10] def valeur_mot(values, word): res = 0 for letter in word: res += values[ord(letter)-ord('a')] return res assert valeur_mot(values, "bebe") == 8 def count(letter, word): n = 0 for l in word: if l == letter: n += 1 return n def mot_possible(word, letters): for l in word: if count(l, word) > count(l, letters): return False return True assert mot_possible("courir", ['c', 'o', 'r', 'u', 'i', 'z', 'x']) == False assert mot_possible("pied", ['p', 'a', 'i', 'd', 'e', 'w', 'k']) == True def mots_jouables(words, letters): possibles = [] for word in words: if mot_possible(word, letters): possibles.append(word) return possibles assert mots_jouables( ["courir", "pied", "depit", "tapir", "marcher"], ['p', 'i', 'd', 'e', 't', 'a', 'r'] ) == ["pied", "depit", "tapir"] def meilleur_mot(words, letters, values): possibles = mots_jouables(words, letters) w = "" s = 0 for word in possibles: score = valeur_mot(values, word) if score > s: w = word s = score return w assert meilleur_mot( ["courir", "pied", "depit", "tapir", "marcher"], ['p', 'i', 'd', 'e', 't', 'a', 'r'], values ) == "depit"