Решение на Пет малки функции от Вера Бойчева

Обратно към всички решения

Към профила на Вера Бойчева

Резултати

  • 10 точки от тестове
  • 1 отнета точка
  • 9 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def extract_type(data):
return [x[0]*x[1] for x in data[0:-1] if type(x[0]) is data[-1]]
def reversed_dict(destinations):
return {destinations[country]: country for country in destinations.keys()}
print(reversed_dict({
"Israel": "Jerusalem",
"Austria": "Vienna",
"Palestine": "Jerusalem",
"Sweden": "Stockholm"}))
def reps(numbers):
return [number for number in numbers
if numbers.count(number) is not 1]
print(reps([1, 4, 2, 6, 7, 2, 4, 11, 1, 9, 0, 2, 5, 3, 1]))
def flatten_dict(data):
result = {}
def helper(unflattened, last_key):
for element in unflattened.keys():
if type(unflattened[element]) is dict:
last_key_temp = last_key + element + '.'
helper(unflattened[element], last_key_temp)
else:
result[last_key + element] = unflattened[element]
helper(data, '')
return result
def unflatten_dict(data):
def helper(key, count):
if '.' in key[count:]:
count += 2
return {key[count]: helper(key, count)}
else:
return data[key]
return {key[0]: helper(key, 0) for key in data.keys()}

Лог от изпълнението

{'Stockholm': 'Sweden', 'Jerusalem': 'Israel', 'Vienna': 'Austria'}
[1, 4, 2, 2, 4, 1, 2, 1]
...................
----------------------------------------------------------------------
Ran 19 tests in 0.141s

OK

История (1 версия и 1 коментар)

Вера обнови решението на 23.03.2015 08:09 (преди над 9 години)

+def extract_type(data):
+ return [x[0]*x[1] for x in data[0:-1] if type(x[0]) is data[-1]]
+
+
+def reversed_dict(destinations):
+ return {destinations[country]: country for country in destinations.keys()}
+print(reversed_dict({
+ "Israel": "Jerusalem",
+ "Austria": "Vienna",
+ "Palestine": "Jerusalem",
+ "Sweden": "Stockholm"}))
+
+
+def reps(numbers):
+ return [number for number in numbers
+ if numbers.count(number) is not 1]
+print(reps([1, 4, 2, 6, 7, 2, 4, 11, 1, 9, 0, 2, 5, 3, 1]))
+
+
+def flatten_dict(data):
+ result = {}
+
+ def helper(unflattened, last_key):
+ for element in unflattened.keys():
+ if type(unflattened[element]) is dict:
+ last_key_temp = last_key + element + '.'
+ helper(unflattened[element], last_key_temp)
+ else:
+ result[last_key + element] = unflattened[element]
+
+ helper(data, '')
+ return result
+
+
+def unflatten_dict(data):
+ def helper(key, count):
+ if '.' in key[count:]:
+ count += 2
+ return {key[count]: helper(key, count)}
+ else:
+ return data[key]
+
+ return {key[0]: helper(key, 0) for key in data.keys()}