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

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

5 точки общо

9 успешни теста
1 неуспешни теста
Код

 1BG_CURRENCY = "BGN"
 2
 3def round_func(number, precision = 4):
 4    if type(number) is int:
 5        return number
 6    raise_num = 10 ** (precision + 1)
 7    floor_num = 10 ** precision
 8    raised_num = int(number * raise_num)
 9    if raised_num % 10 >= 5:
10        return ((raised_num // 10) + 1) / floor_num
11    else:
12        return (raised_num // 10) / floor_num
13
14def patriotic_value(number, precision = 2):
15    result_num = round_func(number, precision)
16    return result_num == int(result_num)
17
18
19def курс_в_лева(exchange_rates):
20    patriotic_exchange_rates = {}
21    for key, value in exchange_rates.items():
22        patriotic_exchange_rates[key] = round_func(1/ value)
23    return patriotic_exchange_rates
24
25def валута_към_левчета(*args, **kwargs):
26    if BG_CURRENCY not in kwargs:
27        kwargs[BG_CURRENCY] = 1
28
29    currency_count = {}
30    for key, value in args:
31        if key in currency_count:
32           currency_count[key] += value
33        else:
34            currency_count[key] = value
35
36    result = []
37    for key,value in currency_count.items():
38        calculated = round_func(value / kwargs[key])
39        result.append((key, calculated))
40    return result
41
42def е_патриотична(amount, exchange_rates):
43    PATRIOTIC_STRING = "ПАТРИОТИЧНА!"
44    UNPATRIOTIC_STRING = "НЕПАТРИОТИЧНА!"
45    total_amount = 0
46    if BG_CURRENCY not in exchange_rates:
47        exchange_rates[BG_CURRENCY] = 1
48    for course, value in amount:
49        total_amount += value/exchange_rates[course]
50
51    if patriotic_value(total_amount):
52        return PATRIOTIC_STRING
53    else:
54        return UNPATRIOTIC_STRING

.....F....
======================================================================
FAIL: test_е_патриотична_no_side_effects (test.TestЕПатриотична.test_е_патриотична_no_side_effects)
There should be no side effects on the input data.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 151, in test_е_патриотична_no_side_effects
self.assertEqual(rates, {"MYR": 0.4287}, "Function has side effects")
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: {'MYR': 0.4287, 'BGN': 1} != {'MYR': 0.4287}
- {'BGN': 1, 'MYR': 0.4287}
? ----------

+ {'MYR': 0.4287} : Function has side effects

----------------------------------------------------------------------
Ran 10 tests in 0.001s

FAILED (failures=1)

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

f1BG_CURRENCY = "BGN"f1BG_CURRENCY = "BGN"
22
3def round_func(number, precision = 4):3def round_func(number, precision = 4):
nn4    if type(number) is int:
5        return number
4    raise_num = 10 ** (precision + 1)6    raise_num = 10 ** (precision + 1)
5    floor_num = 10 ** precision7    floor_num = 10 ** precision
n6    if type(number) is int:n
7        return number
8    raised_num = int(number * raise_num)8    raised_num = int(number * raise_num)
9    if raised_num % 10 >= 5:9    if raised_num % 10 >= 5:
10        return ((raised_num // 10) + 1) / floor_num10        return ((raised_num // 10) + 1) / floor_num
11    else:11    else:
12        return (raised_num // 10) / floor_num12        return (raised_num // 10) / floor_num
1313
14def patriotic_value(number, precision = 2):14def patriotic_value(number, precision = 2):
n15    resultNum = round_func(number, precision)n15    result_num = round_func(number, precision)
16    return resultNum == int(resultNum)16    return result_num == int(result_num)
1717
1818
19def курс_в_лева(exchange_rates):19def курс_в_лева(exchange_rates):
20    patriotic_exchange_rates = {}20    patriotic_exchange_rates = {}
21    for key, value in exchange_rates.items():21    for key, value in exchange_rates.items():
22        patriotic_exchange_rates[key] = round_func(1/ value)22        patriotic_exchange_rates[key] = round_func(1/ value)
23    return patriotic_exchange_rates23    return patriotic_exchange_rates
2424
25def валута_към_левчета(*args, **kwargs):25def валута_към_левчета(*args, **kwargs):
26    if BG_CURRENCY not in kwargs:26    if BG_CURRENCY not in kwargs:
27        kwargs[BG_CURRENCY] = 127        kwargs[BG_CURRENCY] = 1
2828
29    currency_count = {}29    currency_count = {}
30    for key, value in args:30    for key, value in args:
t31            if key in currency_count:t31        if key in currency_count:
32                currency_count[key] += value32           currency_count[key] += value
33            else:33        else:
34                currency_count[key] = value34            currency_count[key] = value
3535
36    result = []36    result = []
37    for key,value in currency_count.items():37    for key,value in currency_count.items():
38        calculated = round_func(value / kwargs[key])38        calculated = round_func(value / kwargs[key])
39        result.append((key, calculated))39        result.append((key, calculated))
40    return result40    return result
4141
42def е_патриотична(amount, exchange_rates):42def е_патриотична(amount, exchange_rates):
43    PATRIOTIC_STRING = "ПАТРИОТИЧНА!"43    PATRIOTIC_STRING = "ПАТРИОТИЧНА!"
44    UNPATRIOTIC_STRING = "НЕПАТРИОТИЧНА!"44    UNPATRIOTIC_STRING = "НЕПАТРИОТИЧНА!"
45    total_amount = 045    total_amount = 0
46    if BG_CURRENCY not in exchange_rates:46    if BG_CURRENCY not in exchange_rates:
47        exchange_rates[BG_CURRENCY] = 147        exchange_rates[BG_CURRENCY] = 1
48    for course, value in amount:48    for course, value in amount:
49        total_amount += value/exchange_rates[course]49        total_amount += value/exchange_rates[course]
5050
51    if patriotic_value(total_amount):51    if patriotic_value(total_amount):
52        return PATRIOTIC_STRING52        return PATRIOTIC_STRING
53    else:53    else:
54        return UNPATRIOTIC_STRING54        return UNPATRIOTIC_STRING
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op