dotfiles/hm/scripts/ter

77 lines
1.7 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env nix-shell
#! nix-shell -i python3 --pure
#! nix-shell -p python3
2019-03-20 22:14:09 +01:00
import sys
from math import inf
gares = sys.argv[1:]
N = len(gares)
if N < 2:
print("Ben reste chez toi alors.")
sys.exit(1)
2021-06-13 11:49:21 +02:00
2019-03-20 22:14:09 +01:00
def trajet_str(a, b):
return f"{gares[a]} → {gares[b]}"
2021-06-13 11:49:21 +02:00
2019-03-20 22:14:09 +01:00
def chemin_str(stack):
2021-06-13 11:49:21 +02:00
return ", ".join(
[trajet_str(stack[i], stack[i + 1]) for i in range(len(stack) - 1)]
)
2019-03-20 22:14:09 +01:00
# Demande des prix des trajets
prices = dict()
for i in range(N):
2021-06-13 11:49:21 +02:00
for j in range(N - 1, i, -1):
2019-03-20 22:14:09 +01:00
p = None
while not isinstance(p, float):
try:
2021-06-13 11:49:21 +02:00
p = float(
input(f"Prix du trajet {trajet_str(i, j)} ? ").replace(",", ".")
)
2019-03-20 22:14:09 +01:00
except ValueError:
print("C'est pas un prix ça !")
if i not in prices:
prices[i] = dict()
prices[i][j] = float(p)
# Calcul des prix des chemins
miniPrice = +inf
miniStack = None
maxiPrice = -inf
maxiStack = None
2021-06-13 11:49:21 +02:00
2019-03-20 22:14:09 +01:00
def register_path(stack):
2021-06-13 11:49:21 +02:00
price = sum([prices[stack[i]][stack[i + 1]] for i in range(len(stack) - 1)])
2019-03-20 22:14:09 +01:00
global miniPrice, maxiPrice, miniStack, maxiStack
if price < miniPrice:
miniPrice = price
miniStack = stack.copy()
if price > maxiPrice:
maxiPrice = price
maxiStack = stack.copy()
print(f"{chemin_str(stack)} = {price:.2f} €")
2021-06-13 11:49:21 +02:00
2019-03-20 22:14:09 +01:00
stack = [0]
while stack[0] == 0:
if stack[-1] >= N - 1:
register_path(stack)
stack.pop()
stack[-1] += 1
else:
2021-06-13 11:49:21 +02:00
stack.append(stack[-1] + 1)
2019-03-20 22:14:09 +01:00
print(f"Prix minimum: {chemin_str(miniStack)} = {miniPrice:.2f} €")
print(f"Prix maximum: {chemin_str(maxiStack)} = {maxiPrice:.2f} €")