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

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

6 точки общо

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

 1def is_exchange_rate_valid(exchange_rates):
 2    for currency in exchange_rates:
 3        if len(currency) != 3:
 4            return False
 5        if exchange_rates[currency] <= 0:
 6            return False
 7    return True
 8
 9def курс_в_лева(exchange_rates):
10    if not is_exchange_rate_valid(exchange_rates):
11        return None
12    
13    result = {}
14    for currency in exchange_rates:
15        result[currency] = round(1 / exchange_rates[currency], 4)
16    return result 
17
18def валута_към_левчета(*currencies, **exchange_rates):
19    
20    if not is_exchange_rate_valid(exchange_rates):
21        return None
22
23    sum = {}
24    for currency, amount in currencies:
25        if currency not in sum:
26            sum[currency] = 0
27        sum[currency] += amount
28    
29    result = []
30    for currency in sum:
31        if currency == "BGN":
32            result.append((currency, sum[currency]))
33        else:
34            result.append((currency, round(sum[currency] / exchange_rates[currency], 4)))
35    return result
36
37def е_патриотична(currencies, exchange_rates): 
38
39    if not is_exchange_rate_valid(exchange_rates):
40        return None
41
42    total_sum = 0
43
44    for currency, amount in currencies:
45        if currency == "BGN":
46            total_sum += amount
47        else: 
48            total_sum += (amount / exchange_rates[currency])
49    
50    rounded = round(total_sum, 2)
51    if rounded % 1 == 0:
52        return "ПАТРИОТИЧНА!"
53    else: 
54        return "НЕПАТРИОТИЧНА!"
55    

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

OK

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

nn1def is_exchange_rate_valid(exchange_rates):
2    for currency in exchange_rates:
3        if len(currency) != 3:
4            return False
5        if exchange_rates[currency] <= 0:
6            return False
7    return True
8 
1def курс_в_лева(exchange_rates):9def курс_в_лева(exchange_rates):
nn10    if not is_exchange_rate_valid(exchange_rates):
11        return None
12    
2    result = {}13    result = {}
3    for currency in exchange_rates:14    for currency in exchange_rates:
n4        if len(currency) != 3:n
5            return None
6        
7        if exchange_rates[currency] <= 0:
8            return None
9 
10        result[currency] = round(1 / exchange_rates[currency], 4)15        result[currency] = round(1 / exchange_rates[currency], 4)
11    return result 16    return result 
1217
13def валута_към_левчета(*currencies, **exchange_rates):18def валута_към_левчета(*currencies, **exchange_rates):
14    19    
nn20    if not is_exchange_rate_valid(exchange_rates):
21        return None
22 
15    sum = {}23    sum = {}
16    for currency, amount in currencies:24    for currency, amount in currencies:
17        if currency not in sum:25        if currency not in sum:
18            sum[currency] = 026            sum[currency] = 0
19        sum[currency] += amount27        sum[currency] += amount
20    28    
21    result = []29    result = []
22    for currency in sum:30    for currency in sum:
23        if currency == "BGN":31        if currency == "BGN":
24            result.append((currency, sum[currency]))32            result.append((currency, sum[currency]))
25        else:33        else:
26            result.append((currency, round(sum[currency] / exchange_rates[currency], 4)))34            result.append((currency, round(sum[currency] / exchange_rates[currency], 4)))
27    return result35    return result
2836
29def е_патриотична(currencies, exchange_rates): 37def е_патриотична(currencies, exchange_rates): 
3038
nn39    if not is_exchange_rate_valid(exchange_rates):
40        return None
41 
31    total_sum = 042    total_sum = 0
3243
33    for currency, amount in currencies:44    for currency, amount in currencies:
34        if currency == "BGN":45        if currency == "BGN":
35            total_sum += amount46            total_sum += amount
36        else: 47        else: 
37            total_sum += (amount / exchange_rates[currency])48            total_sum += (amount / exchange_rates[currency])
38    49    
39    rounded = round(total_sum, 2)50    rounded = round(total_sum, 2)
40    if rounded % 1 == 0:51    if rounded % 1 == 0:
41        return "ПАТРИОТИЧНА!"52        return "ПАТРИОТИЧНА!"
42    else: 53    else: 
43        return "НЕПАТРИОТИЧНА!"54        return "НЕПАТРИОТИЧНА!"
tt55    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op