Une possibilité pour pop2 qui ne nécessite pas d'utiliser pop(i) :
def pop2(tpl, i, j): res = [] for k in range(len(tpl)): if k != i and k != j: res.append(tpl[k]) return tpl[i], tpl[j], tuple(res)
Avec cette fonction, les quatre fonctions suivantes s'écrivent simplement :
def somme(tpl, i, j): x, y, reste_tpl = pop2(tpl, i, j) return reste_tpl + (x+y,) def produit(tpl, i, j): x, y, reste_tpl = pop2(tpl, i, j) return reste_tpl + (x*y,) def difference(tpl, i, j): x, y, reste_tpl = pop2(tpl, i, j) return reste_tpl + (abs(x-y),) def quotient(tpl, i, j): x, y, reste_tpl = pop2(tpl, i, j) if x == 0 and y == 0: return None if x == 0 or y == 0: return reste_tpl + (0,) if x%y == 0 : return reste_tpl + (x//y,) if y%x == 0 : return reste_tpl + (y//x,) return None
On écrit possible comme le suggère le sujet :
def possible(tpl, obj): if tpl == None: return False if obj in tpl: return True for i in range(len(tpl)-1): for j in range(i+1, len(tpl)): if (possible(somme(tpl, i, j), obj) or possible(produit(tpl, i, j), obj) or possible(difference(tpl, i, j), obj) or possible(quotient(tpl, i, j), obj)): return True return False
Pour déterminer et compter les objectifs entre 1 et 200 qui peuvent être atteints avec (1, 4, 5, 8, 9) :
compteur = 0 for obj in range(1, 201): if possible((1,4,5,8,9), obj): compteur = compteur + 1 else: print(obj, "impossible") print(compteur, "objectifs possibles")
Pour faire en sorte de devoir utiliser tous les entiers, le changement est marginal :
def possible_tous(tpl, obj): if tpl == None: return False if tpl == (obj,): # <- unique changement return True for i in range(len(tpl)-1): for j in range(i+1, len(tpl)): if (possible_tous(somme(tpl, i, j), obj) or possible_tous(produit(tpl, i, j), obj) or possible_tous(difference(tpl, i, j), obj) or possible_tous(quotient(tpl, i, j), obj)): return True return False
Il y a un seul objectif qui soit possible avec une partie de (1, 4, 5, 8, 9) mais pas en utilisant les cinq :
compteur = 0 for obj in range(1, 201): if possible_tous((1,4,5,8,9), obj): compteur = compteur + 1 else: print(obj, "impossible") print(compteur, "objectifs possibles")
Pour obtenir une solution, on adapte possible :
def solution(tpl, obj): if tpl == None: return None if obj in tpl: return [] for i in range(len(tpl)-1): for j in range(i+1, len(tpl)): x, y = tpl[i], tpl[j] r = solution(somme(tpl, i, j), obj) if r is not None: return [f'{x}+{y}={x+y}']+r r = solution(produit(tpl, i, j), obj) if r is not None: return [f'{x}*{y}={x*y}']+r r = solution(difference(tpl, i, j), obj) if r is not None: if y > x: x, y = y, x return [f'{x}-{y}={x-y}']+r r = solution(difference(tpl, i, j), obj) if r is not None: if y > x or y == 0: x, y = y, x return [f'{x}/{y}={x//y}']+r return None
Pour trouver un quadruplet permettant d'obtenir une solution pour tous les objectifs de 1 à 65 :
from itertools import combinations for tpl in combinations(range(1, 10), 4): if all(solution(tpl, obj) != None for obj in range(1, 66)): print(tpl, "convient")
Pour aller plus vite, on mémoïse solution :
def solution_memo(tpl, obj, mem): # <- Ajout de mem tpl = tuple(sorted(tpl)) if (tpl, obj) in mem: # <- Déjà calculé ? return mem[tpl, obj] if tpl == None: mem[tpl, obj] = None # <- Ajout dans mem return tpl[mem, obj] if obj in tpl: mem[tpl, obj] = [] return mem[tpl, obj] for i in range(len(tpl)-1): for j in range(i+1, len(tpl)): x, y = tpl[i], tpl[j] r = solution_memo(somme(tpl, i, j), obj, mem) if r is not None: mem[tpl, obj] = [f'{x}+{y}={x+y}']+r return mem[tpl, obj] r = solution_memo(produit(tpl, i, j), obj, mem) if r is not None: mem[tpl, obj] = [f'{x}*{y}={x*y}']+r return mem[tpl, obj] r = solution_memo(difference(tpl, i, j), obj, mem) if r is not None: if y > x: x, y = y, x mem[tpl, obj] = [f'{x}-{y}={x-y}']+r return mem[tpl, obj] r = solution_memo(difference(tpl, i, j), obj, mem) if r is not None: if y > x or y == 0: x, y = y, x mem[tpl, obj] = [f'{x}/{y}={x//y}']+r return mem[tpl, obj] mem[tpl, obj] = None return mem[tpl, obj]
Cela permet de travailler avec de gros n-uplets :
print(solution_memo((3,7,11,14,17,21,22,29,42,54,78), 1234567, {}))
Pour vérifier que (3, 4, 5, 9, 75, 100) permet d'obtenir tous les objectifs de 1 à 999 :
mem = {} print(all(solution_memo((3,4,5,9,75,100), obj, mem) != None for obj in range(1, 1000)))