Домашни > Да върнем левчето обратно! > Решения > Решението на Боян Байданов

Резултати
6 точки от тестове
0 точки от учител

6 точки общо

10 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1"""
 2Funcs that convert exchange rates
 3"""
 4
 5def курс_в_лева(exchange_rates):
 6    """
 7    Convert exchange rates to amount of currency equal to 1 leva
 8
 9    Returns:
10        dict: Pairs of currency codes and quantity in leva
11    """
12    amount_of_leva = {}
13    for currency, rate in exchange_rates.items():
14        amount_of_leva[currency] = round(1 / rate, 4)
15    return amount_of_leva
16
17
18def валута_към_левчета(*args, **kwargs):
19    """
20    Takes any number of (identifier, value) pairs and custom rates kwargs (USD=0.5),
21    then returns the amount of money in leva
22
23    Args:
24        *args: Tuples of (currency_code, amount) e.g. ("EUR", 1.5)
25        **kwargs: Exchange rates as currency_code=rate e.g. EUR=0.5
26
27    Returns:
28        list: Pairs of (currency_code, leva_amount)
29    """
30    currencies = []
31
32    # Accumulate total amount per currency
33
34    for pair in args:
35        currencies.append(pair[0])
36    currency_amounts = dict.fromkeys(set(currencies), 0)
37
38    for currency, quantity in args:
39        currency_amounts[currency] += quantity
40
41    # Convert totals to leva using rates
42
43    result = []
44    for currency, total in currency_amounts.items():
45        if currency != "BGN":
46            rate = kwargs[currency]
47        else:
48            rate = 1
49        
50        result.append((currency, round((total / rate), 4)))
51
52    return result
53
54
55def е_патриотична(amount, exchange_rates):
56    """
57    Checks if the sum of all the money in the list is a round number
58
59    Returns:
60        str: Патриотична / Непатриотична
61    """
62    converted = валута_към_левчета(*amount, **exchange_rates)
63    
64    total = 0
65    for _, leva in converted:
66        total += leva
67    
68    if round(total, 2) % 1 == 0:
69        return "ПАТРИОТИЧНА!"
70    else:
71        return "НЕПАТРИОТИЧНА!"

..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s

OK

Дискусия
История
Това решение има само една версия.