File size: 786 Bytes
0b7102d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""Order pricing for a small shop."""
from typing import List, Optional, Tuple


def apply_discount(subtotal: float, code: Optional[str]) -> float:
    """Return the subtotal after the discount code, if any.

    SAVE10 takes 10% off. FLAT5 takes 5.00 off but never below zero.
    Unknown or empty codes change nothing.
    """
    if code == "SAVE10":
        return subtotal * 0.9
    if code == "FLAT5":
        return max(subtotal - 5, 0)
    return subtotal


def order_total(items: List[Tuple[float, int]], code: Optional[str] = None) -> float:
    """items are (unit_price, quantity). Discount applies to the subtotal, then 8% tax."""
    subtotal = sum(price * qty for price, qty in items)
    discounted = apply_discount(subtotal, code)
    return round(discounted * 1.08, 2)