Домашни > Да върнем левчето обратно! > Решения > Решението на Милан Георгиев

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

6 точки общо

10 успешни теста
0 неуспешни теста
Код

 1#def test_alternative(exchange_rates):
 2#    for currency in exchange_rates:
 3#        if type(currency) == str and len(currency) == 3:
 4#            exchange_rates[currency] = round(1 / exchange_rates[currency], 4)
 5#
 6#    return exchange_rates
 7
 8def курс_в_лева(exchange_rates):
 9    for currency, rate in exchange_rates.items():
10        if type(currency) == str and len(currency) == 3:
11            exchange_rates[currency] = round(1 / rate, 4)
12
13    return exchange_rates
14
15
16def валута_към_левчета(*args, **kwargs):
17    input_sum = {}
18    
19    for pair in args:
20        if pair[0] not in input_sum:
21            input_sum[pair[0]] = 0
22
23        input_sum[pair[0]] += pair[1]
24
25
26    res = [(currency, round(value if currency == 'BGN' else value / kwargs[currency], 4))
27           for currency, value in input_sum.items()]
28    #res = []
29    #for currency in input_sum:
30    #    if currency == 'BGN':
31    #        value = round(input_sum[currency], 4)
32    #        res.append((currency, value))
33    #    else:
34    #        value = round(input_sum[currency] / kwargs[currency], 4)
35    #        res.append((currency, value))
36
37    return res
38
39
40def е_патриотична(amount, exchange_rates):
41    converted_input = валута_към_левчета(*amount, **exchange_rates)
42    final_sum = 0
43
44    for value in converted_input:
45        final_sum += value[1]
46
47    final_sum = round(final_sum, 2)
48
49    return "ПАТРИОТИЧНА!" if final_sum % 1 == 0 else "НЕПАТРИОТИЧНА!"

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

OK

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

f1#def test_alternative(exchange_rates):f1#def test_alternative(exchange_rates):
2#    for currency in exchange_rates:2#    for currency in exchange_rates:
3#        if type(currency) == str and len(currency) == 3:3#        if type(currency) == str and len(currency) == 3:
4#            exchange_rates[currency] = round(1 / exchange_rates[currency], 4)4#            exchange_rates[currency] = round(1 / exchange_rates[currency], 4)
5#5#
6#    return exchange_rates6#    return exchange_rates
77
8def курс_в_лева(exchange_rates):8def курс_в_лева(exchange_rates):
9    for currency, rate in exchange_rates.items():9    for currency, rate in exchange_rates.items():
10        if type(currency) == str and len(currency) == 3:10        if type(currency) == str and len(currency) == 3:
11            exchange_rates[currency] = round(1 / rate, 4)11            exchange_rates[currency] = round(1 / rate, 4)
1212
13    return exchange_rates13    return exchange_rates
1414
1515
16def валута_към_левчета(*args, **kwargs):16def валута_към_левчета(*args, **kwargs):
17    input_sum = {}17    input_sum = {}
18    18    
19    for pair in args:19    for pair in args:
20        if pair[0] not in input_sum:20        if pair[0] not in input_sum:
21            input_sum[pair[0]] = 021            input_sum[pair[0]] = 0
2222
23        input_sum[pair[0]] += pair[1]23        input_sum[pair[0]] += pair[1]
2424
nn25 
26    res = [(currency, round(value if currency == 'BGN' else value / kwargs[currency], 4))
27           for currency, value in input_sum.items()]
25    res = []28    #res = []
26    for currency in input_sum:29    #for currency in input_sum:
27        if currency == 'BGN':30    #    if currency == 'BGN':
28            value = round(input_sum[currency], 4)31    #        value = round(input_sum[currency], 4)
29            res.append((currency, value))32    #        res.append((currency, value))
30        else:33    #    else:
31            value = round(input_sum[currency] / kwargs[currency], 4)34    #        value = round(input_sum[currency] / kwargs[currency], 4)
32            res.append((currency, value))35    #        res.append((currency, value))
3336
34    return res37    return res
3538
3639
37def е_патриотична(amount, exchange_rates):40def е_патриотична(amount, exchange_rates):
38    converted_input = валута_към_левчета(*amount, **exchange_rates)41    converted_input = валута_към_левчета(*amount, **exchange_rates)
39    final_sum = 042    final_sum = 0
4043
41    for value in converted_input:44    for value in converted_input:
42        final_sum += value[1]45        final_sum += value[1]
4346
44    final_sum = round(final_sum, 2)47    final_sum = round(final_sum, 2)
4548
46    return "ПАТРИОТИЧНА!" if final_sum % 1 == 0 else "НЕПАТРИОТИЧНА!"49    return "ПАТРИОТИЧНА!" if final_sum % 1 == 0 else "НЕПАТРИОТИЧНА!"
t47 t
48 
49 
50amount = [("EUR", 1), ("USD", 3), ("DKK", 7.6), ("EUR", 3)]
51exchange_rates = {"EUR": 0.5, "USD": 0.6, "DKK": 3.8}
52print(е_патриотична(amount, exchange_rates))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op