Solución
solution.tsTypeScript
from typing import Dict
MIN_VALUE_THRESHOLD: int = 0
HEAVY_PACKAGE_THRESHOLD: int = 20
HEAVY_PACKAGE_DISCOUNT: float = 0.10
ZONE_CONFIG: Dict = {
"local" : {"base_per_kg": 2, "extra_per_km": 0.10},
"regional": {"base_per_kg": 5, "extra_per_km": 0.05},
"nacional": {"base_per_kg": 10, "extra_per_km": 0.02}
}
def is_valid_input(weight: float, distance: float) -> bool:
return weight > MIN_VALUE_THRESHOLD and distance > MIN_VALUE_THRESHOLD
def calculate_raw_cost(weight: float, distance: float, zone: str) -> float:
config: Dict = ZONE_CONFIG.get(zone.lower())
if not config:
return 0.0
cost_from_weight: float = weight * config["base_per_kg"]
cost_from_distance: float = distance * config["extra_per_km"]
return cost_from_weight + cost_from_distance
def apply_discounts(total_cost: float, weight: float) -> float:
if weight > HEAVY_PACKAGE_THRESHOLD:
total_cost -= (total_cost * HEAVY_PACKAGE_DISCOUNT)
return total_cost
def calculate_shipping_cost(weight: float, distance: float, zone: str) -> float:
if not is_valid_input(weight, distance):
return -1.0
total_cost: float = calculate_raw_cost(weight, distance, zone)
final_cost: float = apply_discounts(total_cost, weight)
return round(final_cost, 2)
__all__ = ["calculate_shipping_cost"]0respuestas