tp d'informatique 27 octobre 2022
This commit is contained in:
parent
84c0136d56
commit
bcc891bcb6
15
27oct/ex4.py
Normal file
15
27oct/ex4.py
Normal file
@ -0,0 +1,15 @@
|
||||
import random
|
||||
|
||||
|
||||
def creer_enchevetrements(bag, i, max_en):
|
||||
"""creer une suite d'enchevetrement aleatoire """
|
||||
autre_baguettes = bag[:]
|
||||
autre_baguettes.pop(i)
|
||||
return [(random.choice(autre_baguettes), i) for _ in range(random.randint(1, max_en))]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for _ in range(100):
|
||||
p = list(range(42))
|
||||
random.shuffle(p)
|
||||
print(creer_enchevetrements(p, 8, 10))
|
18
27oct/ex5.py
Normal file
18
27oct/ex5.py
Normal file
@ -0,0 +1,18 @@
|
||||
import random
|
||||
from ex4 import creer_enchevetrements
|
||||
|
||||
|
||||
def creer_mikado(bag):
|
||||
"""creer un sac de mikado avec des enchevetrement aleatoire """
|
||||
game = list()
|
||||
nombre_enchevetrements = random.randint(1, len(bag))
|
||||
while len(game) < nombre_enchevetrements:
|
||||
game.append(creer_enchevetrements(bag, random.choice(bag), 1)[0])
|
||||
return game
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for _ in range(100):
|
||||
bag = list(range(random.randint(2, 42)))
|
||||
random.shuffle(bag)
|
||||
print(creer_mikado(bag))
|
27
27oct/ex6.py
Normal file
27
27oct/ex6.py
Normal file
@ -0,0 +1,27 @@
|
||||
def peut_retirer(i, bag, jeu):
|
||||
"""verifie si une baguette peut etre retiree """
|
||||
on_top = bag[:]
|
||||
for intersection in jeu:
|
||||
if intersection[1] in on_top:
|
||||
on_top.remove(intersection[1])
|
||||
return i in on_top
|
||||
|
||||
|
||||
def retirer(i, bag, jeu):
|
||||
bag.remove(i)
|
||||
for intersection in jeu:
|
||||
if i in intersection:
|
||||
jeu.remove(intersection)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from ex5 import creer_mikado
|
||||
|
||||
bag = list(range(10))
|
||||
game = creer_mikado(bag)
|
||||
print(game)
|
||||
retirer(5, bag, game)
|
||||
print(game)
|
||||
# print(bag)
|
||||
# print(game)
|
||||
# print(peut_retirer(5, bag, game))
|
33
27oct/ex7.py
Normal file
33
27oct/ex7.py
Normal file
@ -0,0 +1,33 @@
|
||||
from ex6 import peut_retirer, retirer
|
||||
|
||||
|
||||
def quel_ordre(_bag, jeu):
|
||||
"""retourne un ordre possible sinon None """
|
||||
bag = _bag[:]
|
||||
order = list()
|
||||
last_order = None
|
||||
while order != last_order:
|
||||
last_order = order[:]
|
||||
print(last_order)
|
||||
for i in bag:
|
||||
if peut_retirer(i, bag, jeu):
|
||||
order.append(i)
|
||||
retirer(i, bag, jeu)
|
||||
bag_t, order_t = bag[:], order[:]
|
||||
bag_t.sort()
|
||||
order_t.sort()
|
||||
print(order_t, bag_t)
|
||||
if(order_t == bag_t):
|
||||
return order
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from ex5 import creer_mikado
|
||||
|
||||
bag = list(range(10))
|
||||
game = creer_mikado(bag)
|
||||
print(bag)
|
||||
print(game)
|
||||
print(quel_ordre(bag, game))
|
BIN
27oct/serie6.pdf
Normal file
BIN
27oct/serie6.pdf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user