From 5a9f1b1083343b60a7e21ea7982cb55697bb89a8 Mon Sep 17 00:00:00 2001 From: Midgard Date: Thu, 14 Mar 2019 20:05:44 +0100 Subject: [PATCH] Initial commit --- simprizza.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 simprizza.py diff --git a/simprizza.py b/simprizza.py new file mode 100755 index 0000000..a2bda9f --- /dev/null +++ b/simprizza.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python + +import math + + +def main(): + print("Reduction in cents: ", end="") + reduction = int(input()) + print() + + prices = [] + i = 1 + done = False + while not done: + + if i == 1: + print(f"Price {i} in cents: ", end="") + else: + print(f"Price {i} (empty to finish): ", end="") + read = input() + if not read: + break + price = int(read) + + print(f" Amount of orders at this price: ", end="") + amount = int(input()) + + print() + + prices.append((amount, price)) + + i += 1 + + print() + + print("----------------") + print() + + total_without_reduction = sum(amount * price for amount, price in prices) + total_with_reduction = total_without_reduction - reduction + + reduction_percentage = reduction / total_without_reduction + reduction_prices = [ + math.ceil((1 - reduction_percentage) * price) + for _, price in prices + ] + total_reducted_prices = sum( + amount * reduction_price + for (amount, _), reduction_price in zip(prices, reduction_prices) + ) + gap = total_with_reduction - total_reducted_prices + + total_items = sum(amount for amount, _ in prices) + + + print(f"You got {reduction_percentage:.0%} reduction") + print() + + print(" # | Normal price | Reduction price") + print("---|--------------|----------------") + + for (amount, price), reduction_price in zip(prices, reduction_prices): + print(f"{amount:2d} | €{price/100:11.2f} | €{reduction_price/100:14.2f}") + + print("---|--------------|----------------") + print(f"{total_items:2d} | €{total_without_reduction/100:11.2f} | €{total_reducted_prices/100:14.2f}") + if abs(gap) > 1e-9: + print(f" You paid €{total_with_reduction/100:14.2f}") + if gap < 0: + print(f"This solution has a bonus of {-gap}¢ for the courier.") + else: + print(f"This solution leaves {gap}¢ unaccounted for.") + + +if __name__ == "__main__": + main()