Решение на Пет малки функции от Данаил Димитров

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

Към профила на Данаил Димитров

Резултати

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

Код

import functools
def extract_type(pairs, type_filter):
return functools.reduce(lambda x, y: x + y,
[str(x[0]) * x[1]
for x in pairs if isinstance(x[0],
type_filter)])
def reversed_dict(dict_to_reverse):
return {key: value for value, key in dict_to_reverse.items()}
def flatten_pair(fat_dict, prefix):
new_dict = {}
for key, value in fat_dict.items():
if isinstance(value, dict):
new_dict.update(flatten_pair(value, prefix + key + '.'))
else:
new_dict[prefix + key] = fat_dict[key]
return new_dict
def flatten_dict(fat_dict):
return flatten_pair(fat_dict, '')
def unflatten_dict_r(dict_object, splitted_keys, value):
key = splitted_keys.pop(0)
if key in dict_object:
unflatten_dict_r(dict_object[key], splitted_keys, value)
else:
dict_object.update({
key: add_new_key(splitted_keys, value)
})
def add_new_key(splitted_keys, value):
if not splitted_keys:
return value
new_dict = {}
key = splitted_keys.pop(0)
new_dict[key] = add_new_key(splitted_keys,
value)
return new_dict
def unflatten_dict(flat_dict):
fat_dict = {}
for key, value in flat_dict.items():
splitted_key = key.split('.')
unflatten_dict_r(fat_dict,
splitted_key,
value)
return fat_dict
def reps(reps_list):
return [item for item in reps_list if reps_list.count(item) > 1]

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.137s

OK

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

Данаил обнови решението на 23.03.2015 12:56 (преди около 9 години)

+import functools
+
+
+def extract_type(pairs, type_filter):
+ return functools.reduce(lambda x, y: x + y,
+ [str(x[0]) * x[1]
+ for x in pairs if isinstance(x[0],
+ type_filter)])
+
+
+def reversed_dict(dict_to_reverse):
+ return {key: value for value, key in dict_to_reverse.items()}
+
+
+def flatten_pair(fat_dict, prefix):
+ new_dict = {}
+ for key, value in fat_dict.items():
+ if isinstance(value, dict):
+ new_dict.update(flatten_pair(value, prefix + key + '.'))
+ else:
+ new_dict[prefix + key] = fat_dict[key]
+ return new_dict
+
+
+def flatten_dict(fat_dict):
+ return flatten_pair(fat_dict, '')
+
+
+def unflatten_dict_r(dict_object, splitted_keys, value):
+ key = splitted_keys.pop(0)
+ if key in dict_object:
+ unflatten_dict_r(dict_object[key], splitted_keys, value)
+ else:
+ dict_object.update({
+ key: add_new_key(splitted_keys, value)
+ })
+
+
+def add_new_key(splitted_keys, value):
+ if not splitted_keys:
+ return value
+ new_dict = {}
+ key = splitted_keys.pop(0)
+ new_dict[key] = add_new_key(splitted_keys,
+ value)
+ return new_dict
+
+
+def unflatten_dict(flat_dict):
+ fat_dict = {}
+ for key, value in flat_dict.items():
+ splitted_key = key.split('.')
+ unflatten_dict_r(fat_dict,
+ splitted_key,
+ value)
+ return fat_dict
+
+
+def reps(reps_list):
+ return [item for item in reps_list if reps_list.count(item) > 1]