1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
   | #!/usr/bin/python3
# -*- coding: utf-8 -*-
# Python 3
 
"""
conversion en Python du programme Pascal de:
http://www.chambily.com/recursivite/chap_IV_7.htm
"""
 
import random
random.seed()
 
def operations(t, max):
 
    signe = '+-*/'
 
    for i in range(0, 4):
        for j1 in range(1, max):
            for j2 in range(j1 + 1, max + 1):
                if i == 0:
                    a = t[j1] + t[j2]
                elif i == 1:
                    a = t[j1] - t[j2]
                elif i == 2:
                    a = t[j1] * t[j2]
                elif i == 3:
                    a = t[j1] // t[j2]
                    if t[j2] * a != t[j1]:
                        a = 0
 
                if a > 0:
                    if a == t[0]:
                        print("%s%s%s=%s" % (t[j1], signe[i], t[j2], a))
                        trouve = True
                        return trouve
 
                    t1 = t[:]
 
                    t1[j1] = a
                    t1[j2] = 0
                    while True:
                        echange = False
                        for ii in range(1, max):
                            if t1[ii] < t1[ii + 1]:
                                aa = t1[ii]
                                t1[ii] = t1[ii + 1]
                                t1[ii + 1] = aa
                                echange = True
                        if not echange:
                            break
                    trouve = operations(t1, max - 1)
                    if trouve:
                        print("%s%s%s=%s" % (t[j1], signe[i], t[j2], a))
                        return trouve
 
nb_a_trouver = random.randint(101, 999)
print("Nombre à trouver: ", nb_a_trouver)
 
ressources = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100]
nbs_dispo = [random.choice(ressources) for i in range(0, 6)]
nbs_dispo.sort(reverse=True)
print("Nombres disponibles", nbs_dispo)
print()
 
nombres = [nb_a_trouver] + nbs_dispo
 
trouve = operations(nombres, 6)
 
if not trouve:
    print("pas de solution") |