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

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

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

Резултати

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

Код

SEPARATOR = "."
def extract_type(values, type_to_extract):
extracted = str()
for value in values:
if type(value[0]) is type_to_extract:
extracted += str(value[0]) * value[1]
return extracted
def reversed_dict(input_):
return dict(zip(input_.values(), input_))
def flatten_dict_recursion(data, parent_key):
items = []
for key, value in data.items():
if len(parent_key) > 0:
new_key = parent_key + SEPARATOR + key
else:
new_key = key
if isinstance(value, dict):
items.extend(flatten_dict_recursion(value, new_key).items())
else:
items.append((new_key, value))
return dict(items)
def flatten_dict(data):
return flatten_dict_recursion(data, "")
def unflatten_dict(data):
unflattened_result = dict()
for key, value in data.items():
key_parts = key.split(SEPARATOR)
d = unflattened_result
for new_key in key_parts[:-1]:
if new_key not in d:
d[new_key] = dict()
d = d[new_key]
d[key_parts[-1]] = value
return unflattened_result
def reps(data):
return tuple(filter((lambda x: data.count(x) > 1), data))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.160s

OK

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

Диан обнови решението на 22.03.2015 16:56 (преди над 9 години)

+SEPARATOR = "."
+
+
+def extract_type(values, type_to_extract):
+ extracted = str()
+ for value in values:
+ if type(value[0]) is type_to_extract:
+ extracted += str(value[0]) * value[1]
+ return extracted
+
+
+def reversed_dict(input_):
+ return dict(zip(input_.values(), input_))
+
+
+def flatten_dict_recursion(data, parent_key):
+ items = []
+ for key, value in data.items():
+ if len(parent_key) > 0:
+ new_key = parent_key + SEPARATOR + key
+ else:
+ new_key = key
+ if isinstance(value, dict):
+ items.extend(flatten_dict_recursion(value, new_key).items())
+ else:
+ items.append((new_key, value))
+ return dict(items)
+
+
+def flatten_dict(data):
+ return flatten_dict_recursion(data, "")
+
+
+def unflatten_dict(data):
+ unflattened_result = dict()
+ for key, value in data.items():
+ key_parts = key.split(SEPARATOR)
+ d = unflattened_result
+ for new_key in key_parts[:-1]:
+ if new_key not in d:
+ d[new_key] = dict()
+ d = d[new_key]
+ d[key_parts[-1]] = value
+ return unflattened_result
+
+
+def reps(data):
+ return tuple(filter((lambda x: data.count(x) > 1), data))