Решение на Пет малки функции от Ангел Венчев

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

Към профила на Ангел Венчев

Резултати

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

Код

from itertools import groupby
def extract_type(typed_tuples, type_specimen):
return "".join(["".join([str(value)] * repeat_count)
for (value, repeat_count)
in typed_tuples
if type(value) is type_specimen])
def reversed_dict(dictionary):
return {value: key for key, value in dictionary.items()}
def flatten_dict(dictionary):
return iterate_and_flatten("", dictionary)
def iterate_and_flatten(key_prefix, dictionary):
aggregator = {}
for key, value in dictionary.items():
new_key = '.'.join(filter(None, [key_prefix, key]))
aggregator.update(flatten(new_key, value))
return aggregator
def flatten(key, value):
if type(value) is dict:
return iterate_and_flatten(key, value)
else:
return {key: value}
def unflatten_dict(dictionary):
unflattened = {}
all_keys = get_sorted_keys(dictionary)
for key, group in groupby(all_keys, lambda x: x.split('.')[0]):
grouped_keys = list(group)
if '.' in grouped_keys[0]:
nested_dict = create_nested_dict(grouped_keys, dictionary)
unflattened[key] = unflatten_dict(nested_dict)
else:
unflattened[key] = dictionary[grouped_keys[0]]
return unflattened
def get_sorted_keys(dictionary):
all_keys = list(dictionary.keys())
all_keys.sort()
return all_keys
def create_nested_dict(grouped_keys, old_dictionary):
new_keys = remove_preffix_from_keys(grouped_keys)
nested_dictionary = {}
for i in range(len(new_keys)):
nested_dictionary[new_keys[i]] = old_dictionary[grouped_keys[i]]
return nested_dictionary
def remove_preffix_from_keys(grouped_keys):
return [".".join(x.split('.')[1:]) for x in grouped_keys]
def reps(elements):
return tuple([x for x in elements if elements.count(x) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.135s

OK

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

Ангел обнови решението на 23.03.2015 16:33 (преди над 9 години)

+from itertools import groupby
+
+
+def extract_type(typed_tuples, type_specimen):
+ return "".join(["".join([str(value)] * repeat_count)
+ for (value, repeat_count)
+ in typed_tuples
+ if type(value) is type_specimen])
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def flatten_dict(dictionary):
+ return iterate_and_flatten("", dictionary)
+
+
+def iterate_and_flatten(key_prefix, dictionary):
+ aggregator = {}
+ for key, value in dictionary.items():
+ new_key = '.'.join(filter(None, [key_prefix, key]))
+ aggregator.update(flatten(new_key, value))
+ return aggregator
+
+
+def flatten(key, value):
+ if type(value) is dict:
+ return iterate_and_flatten(key, value)
+ else:
+ return {key: value}
+
+
+def unflatten_dict(dictionary):
+ unflattened = {}
+ all_keys = get_sorted_keys(dictionary)
+ for key, group in groupby(all_keys, lambda x: x.split('.')[0]):
+ grouped_keys = list(group)
+ if '.' in grouped_keys[0]:
+ nested_dict = create_nested_dict(grouped_keys, dictionary)
+ unflattened[key] = unflatten_dict(nested_dict)
+ else:
+ unflattened[key] = dictionary[grouped_keys[0]]
+ return unflattened
+
+
+def get_sorted_keys(dictionary):
+ all_keys = list(dictionary.keys())
+ all_keys.sort()
+ return all_keys
+
+
+def create_nested_dict(grouped_keys, old_dictionary):
+ new_keys = remove_preffix_from_keys(grouped_keys)
+ nested_dictionary = {}
+ for i in range(len(new_keys)):
+ nested_dictionary[new_keys[i]] = old_dictionary[grouped_keys[i]]
+
+ return nested_dictionary
+
+
+def remove_preffix_from_keys(grouped_keys):
+ return [".".join(x.split('.')[1:]) for x in grouped_keys]
+
+
+def reps(elements):
+ return tuple([x for x in elements if elements.count(x) > 1])