Домашни > Да върнем левчето обратно! > Решения > Решението на Емилияна Атанасова

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

6 точки общо

10 успешни теста
0 неуспешни теста
Код (Поправени са неточностите и rev_rates[curr]=[rou])

 1from collections import defaultdict
 2from functools import reduce
 3
 4DECIMAL_PLACES = 4
 5STOTINKI_PLACES = 2
 6BULGARIAN_CURRENCY = 'BGN'
 7
 8def курс_в_лева(exchange_rates):
 9    reverted_rates = {}
10    for currency in exchange_rates:
11        reverted_rates[currency] = round(1 / exchange_rates[currency], DECIMAL_PLACES)
12    return reverted_rates
13
14def валута_към_левчета(*args, **kwargs):
15    results = []
16    totals = defaultdict(float)
17
18    for curr, count in  args:
19        totals[curr] += count
20
21    for curr, count in  totals.items():
22        левчета = count if curr == BULGARIAN_CURRENCY else count * (1 / kwargs[curr])
23        results.append((curr, round(левчета, DECIMAL_PLACES)))
24
25    return results
26
27def е_патриотична(amounts, exchange_rates):
28    sum = reduce(lambda acc, pair: acc + pair[1], валута_към_левчета(*amounts, **exchange_rates), 0)
29    leva = round(sum, STOTINKI_PLACES)
30    return 'ПАТРИОТИЧНА!' if leva.is_integer() else 'НЕПАТРИОТИЧНА!'

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

OK

Дискусия
История

f1from collections import defaultdictf1from collections import defaultdict
2from functools import reduce2from functools import reduce
33
4DECIMAL_PLACES = 44DECIMAL_PLACES = 4
5STOTINKI_PLACES = 25STOTINKI_PLACES = 2
6BULGARIAN_CURRENCY = 'BGN'6BULGARIAN_CURRENCY = 'BGN'
77
8def курс_в_лева(exchange_rates):8def курс_в_лева(exchange_rates):
9    reverted_rates = {}9    reverted_rates = {}
10    for currency in exchange_rates:10    for currency in exchange_rates:
n11        rate = exchange_rates[currency]n11        reverted_rates[currency] = round(1 / exchange_rates[currency], DECIMAL_PLACES)
12        reverted_rate = 1 / rate
13        rounded = round(reverted_rate, DECIMAL_PLACES)
14        reverted_rates[currency] = [rounded]
15    return reverted_rates12    return reverted_rates
1613
17def валута_към_левчета(*args, **kwargs):14def валута_към_левчета(*args, **kwargs):
18    results = []15    results = []
19    totals = defaultdict(float)16    totals = defaultdict(float)
2017
21    for curr, count in  args:18    for curr, count in  args:
22        totals[curr] += count19        totals[curr] += count
2320
24    for curr, count in  totals.items():21    for curr, count in  totals.items():
n25        if curr == BULGARIAN_CURRENCY:n22        левчета = count if curr == BULGARIAN_CURRENCY else count * (1 / kwargs[curr])
26             левчета = count23        results.append((curr, round(левчета, DECIMAL_PLACES)))
27        else:
28            reverted_rate = 1 / kwargs[curr]
29            левчета = count * reverted_rate
3024
n31        rounded = round(левчета, DECIMAL_PLACES)n
32        results.append((curr, rounded))
33    return results25    return results
3426
n35def е_патриотична(amount, exchange_rates):n27def е_патриотична(amounts, exchange_rates):
36    sum = reduce(lambda acc, pair: acc + pair[1], валута_към_левчета(*amount, **exchange_rates), 0)28    sum = reduce(lambda acc, pair: acc + pair[1], валута_към_левчета(*amounts, **exchange_rates), 0)
37    leva = round(sum, STOTINKI_PLACES)29    leva = round(sum, STOTINKI_PLACES)
n38    if (leva.is_integer()):n30    return 'ПАТРИОТИЧНА!' if leva.is_integer() else 'НЕПАТРИОТИЧНА!'
39        return 'ПАТРИОТИЧНА!'
40    else:
41        return 'НЕПАТРИОТИЧНА!'
4231
t43 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op