| f | from collections import defaultdict | f | from collections import defaultdict |
| from functools import reduce | | from functools import reduce |
| | | |
| DECIMAL_PLACES = 4 | | DECIMAL_PLACES = 4 |
| STOTINKI_PLACES = 2 | | STOTINKI_PLACES = 2 |
| BULGARIAN_CURRENCY = 'BGN' | | BULGARIAN_CURRENCY = 'BGN' |
| | | |
| def курс_в_лева(exchange_rates): | | def курс_в_лева(exchange_rates): |
| reverted_rates = {} | | reverted_rates = {} |
| for currency in exchange_rates: | | for currency in exchange_rates: |
| n | rate = exchange_rates[currency] | n | reverted_rates[currency] = round(1 / exchange_rates[currency], DECIMAL_PLACES) |
| reverted_rate = 1 / rate | | |
| rounded = round(reverted_rate, DECIMAL_PLACES) | | |
| reverted_rates[currency] = [rounded] | | |
| return reverted_rates | | return reverted_rates |
| | | |
| def валута_към_левчета(*args, **kwargs): | | def валута_към_левчета(*args, **kwargs): |
| results = [] | | results = [] |
| totals = defaultdict(float) | | totals = defaultdict(float) |
| | | |
| for curr, count in args: | | for curr, count in args: |
| totals[curr] += count | | totals[curr] += count |
| | | |
| for curr, count in totals.items(): | | for curr, count in totals.items(): |
| n | if curr == BULGARIAN_CURRENCY: | n | левчета = count if curr == BULGARIAN_CURRENCY else count * (1 / kwargs[curr]) |
| левчета = count | | results.append((curr, round(левчета, DECIMAL_PLACES))) |
| else: | | |
| reverted_rate = 1 / kwargs[curr] | | |
| левчета = count * reverted_rate | | |
| | | |
| n | rounded = round(левчета, DECIMAL_PLACES) | n | |
| results.append((curr, rounded)) | | |
| return results | | return results |
| | | |
| n | def е_патриотична(amount, exchange_rates): | n | def е_патриотична(amounts, exchange_rates): |
| sum = reduce(lambda acc, pair: acc + pair[1], валута_към_левчета(*amount, **exchange_rates), 0) | | sum = reduce(lambda acc, pair: acc + pair[1], валута_към_левчета(*amounts, **exchange_rates), 0) |
| leva = round(sum, STOTINKI_PLACES) | | leva = round(sum, STOTINKI_PLACES) |
| n | if (leva.is_integer()): | n | return 'ПАТРИОТИЧНА!' if leva.is_integer() else 'НЕПАТРИОТИЧНА!' |
| return 'ПАТРИОТИЧНА!' | | |
| else: | | |
| return 'НЕПАТРИОТИЧНА!' | | |
| | | |
| t | | t | |