Exercice 22
Ecrire un algorithme qui prends en entrée l'ensemble python E = {'a' , 'b' , 'c' , 'd' , 'e'} et qui renvoie la liste des parties de E ayant 2 ou 3 éléments.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
E = {'a' , 'b' , 'c' , 'd' , 'e'} # initialiser la liste demandée L =[] for x in E: for y in E: for z in E: F = {x,y,z} if len(F) >=2: if F not in L: L.append(F) # afficher la liste print(L) """ output: [{'c', 'a'}, {'c', 'b'}, {'c', 'd'},{'c', 'e'}, {'c', 'b', 'a'}, {'c', 'd', 'a'}, {'c', 'a', 'e'}, {'c', 'b', 'd'}, {'c', 'b', 'e'},{'c', 'd', 'e'}, {'b', 'a'}, {'d', 'a'}, {'a', 'e'},{'b', 'd', 'a'}, {'b', 'a', 'e'}, {'d', 'a', 'e'}, {'b', 'd'}, {'b', 'e'}, {'b', 'd', 'e'}, {'d', 'e'}] """ |
1 thought on “Solution Exercice 22: ensemble des parties ayant 2 ou 3 éléments en python”