Решение на Пет малки функции от Цветелина Александрова

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

Към профила на Цветелина Александрова

Резултати

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

Код

def extract_type(tuples, type_to_extract):
return "".join(map(lambda extracted_tuple:
str(extracted_tuple[0]) * extracted_tuple[1],
filter(lambda entry_tuple:
type(entry_tuple[0]) is type_to_extract,
tuples)))
def reversed_dict(dict_to_reverse):
rev_dict = {}
for key in dict_to_reverse.keys():
rev_dict.update({dict_to_reverse[key]: key})
return rev_dict
def reps(items_list):
return tuple(filter(lambda item:
items_list.count(item) > 1,
items_list))
def flatten_dict(nested_dict):
return flatten_helper(nested_dict, "")
def flatten_helper(nested_dict, parent_key):
flat_dict = {}
for key, value in nested_dict.items():
new_key = key
if parent_key:
new_key = parent_key + "." + key
if isinstance(value, dict):
flat_dict.update(flatten_helper(value, new_key))
else:
flat_dict.setdefault(new_key, value)
return flat_dict
def unflatten_dict(flat_dict):
nested_dict = {}
for key, value in flat_dict.items():
current_nest_keys = key.split(".")
if current_nest_keys:
value_sub_dict = {current_nest_keys.pop(): value}
parent_dict = nested_dict
for new_key in current_nest_keys:
if new_key not in parent_dict:
parent_dict.update({new_key: {}})
parent_dict = parent_dict[new_key]
parent_dict.update(value_sub_dict)
else:
nested_dict.update({key: value})
return nested_dict

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

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

OK

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

Цветелина обнови решението на 19.03.2015 21:51 (преди над 9 години)

+def extract_type(tuples, type_to_extract):
+ return "".join(map(lambda extracted_tuple:
+ str(extracted_tuple[0]) * extracted_tuple[1],
+ filter(lambda entry_tuple:
+ type(entry_tuple[0]) is type_to_extract,
+ tuples)))
+
+
+def reversed_dict(dict_to_reverse):
+ rev_dict = {}
+ for key in dict_to_reverse.keys():
+ rev_dict.update({dict_to_reverse[key]: key})
+ return rev_dict
+
+
+def reps(items_list):
+ return tuple(filter(lambda item:
+ items_list.count(item) > 1,
+ items_list))
+
+
+def flatten_dict(nested_dict):
+ return flatten_helper(nested_dict, "")
+
+
+def flatten_helper(nested_dict, parent_key):
+ flat_dict = {}
+ for key, value in nested_dict.items():
+ new_key = key
+ if parent_key:
+ new_key = parent_key + "." + key
+ if isinstance(value, dict):
+ flat_dict.update(flatten_helper(value, new_key))
+ else:
+ flat_dict.setdefault(new_key, value)
+ return flat_dict
+
+
+def unflatten_dict(flat_dict):
+ nested_dict = {}
+ for key, value in flat_dict.items():
+ current_nest_keys = key.split(".")
+ if current_nest_keys:
+ value_sub_dict = {current_nest_keys.pop(): value}
+ parent_dict = nested_dict
+ for new_key in current_nest_keys:
+ if new_key not in parent_dict:
+ parent_dict.update({new_key: {}})
+ parent_dict = parent_dict[new_key]
+ parent_dict.update(value_sub_dict)
+ else:
+ nested_dict.update({key: value})
+ return nested_dict