dotfiles/scripts/ter

66 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
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)
def trajet_str(a, b):
return f"{gares[a]}{gares[b]}"
def chemin_str(stack):
return ", ".join([trajet_str(stack[i], stack[i+1]) for i in range(len(stack)-1)])
# Demande des prix des trajets
prices = dict()
for i in range(N):
for j in range(N-1, i, -1):
p = None
while not isinstance(p, float):
try:
p = float(input(f"Prix du trajet {trajet_str(i, j)} ? ").replace(',', '.'))
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
def register_path(stack):
price = sum([prices[stack[i]][stack[i+1]]for i in range(len(stack)-1)])
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}")
stack = [0]
while stack[0] == 0:
if stack[-1] >= N - 1:
register_path(stack)
stack.pop()
stack[-1] += 1
else:
stack.append(stack[-1]+1)
print(f"Prix minimum: {chemin_str(miniStack)} = {miniPrice:.2f}")
print(f"Prix maximum: {chemin_str(maxiStack)} = {maxiPrice:.2f}")