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

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

Към профила на Деян Боиклиев

Резултати

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

Код

import functools
def extract_type(symbol_occurrence_pairs, extractedType):
def repeate_symbols(result, symbol_occurence_pair):
symbol = symbol_occurence_pair[0]
occurence = symbol_occurence_pair[1]
if type(symbol) is extractedType:
return str(result) + str(symbol) * occurence
else:
return result
return functools.reduce(repeate_symbols, symbol_occurrence_pairs, "")
def reversed_dict(key_value_pairs):
return {value: key for key, value in key_value_pairs.items()}
def reps(elements):
return tuple([x for x in elements if elements.count(x) > 1])
def flatten_dict(dict_to_flatten):
result = []
for key, value in dict_to_flatten.items():
for key_value_pair in flatten(key, value):
result.append(key_value_pair)
return dict(result)
def flatten(key, value):
if type(value) is dict:
result = []
for inner_key, value in flatten_dict(value).items():
result.append((key + '.' + inner_key, value))
return result
else:
return [(key, value)]
def unflatten_dict(dictionary):
unflattened_dict = dict()
for combined_keys, value in dictionary.items():
separate_keys = combined_keys.split('.')
nested_dict = unflattened_dict
for key in separate_keys[:-1]:
if key not in nested_dict:
nested_dict[key] = dict()
nested_dict = nested_dict[key]
nested_dict[separate_keys[-1]] = value
return unflattened_dict

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.131s

OK

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

Деян обнови решението на 22.03.2015 23:43 (преди над 9 години)

+import functools
+
+
+def extract_type(symbol_occurrence_pairs, extractedType):
+ def repeate_symbols(result, symbol_occurence_pair):
+ symbol = symbol_occurence_pair[0]
+ occurence = symbol_occurence_pair[1]
+ if type(symbol) is extractedType:
+ return str(result) + str(symbol) * occurence
+ else:
+ return result
+ return functools.reduce(repeate_symbols, symbol_occurrence_pairs, "")
+
+
+def reversed_dict(key_value_pairs):
+ return {value: key for key, value in key_value_pairs.items()}
+
+
+def reps(elements):
+ return tuple([x for x in elements if elements.count(x) > 1])
+
+
+def flatten_dict(dict_to_flatten):
+ result = []
+ for key, value in dict_to_flatten.items():
+ for key_value_pair in flatten(key, value):
+ result.append(key_value_pair)
+ return dict(result)
+
+
+def flatten(key, value):
+ if type(value) is dict:
+ result = []
+ for inner_key, value in flatten_dict(value).items():
+ result.append((key + '.' + inner_key, value))
+ return result
+ else:
+ return [(key, value)]
+
+
+def unflatten_dict(dictionary):
+ unflattened_dict = dict()
+ for combined_keys, value in dictionary.items():
+ separate_keys = combined_keys.split('.')
+ nested_dict = unflattened_dict
+ for key in separate_keys[:-1]:
+ if key not in nested_dict:
+ nested_dict[key] = dict()
+ nested_dict = nested_dict[key]
+ nested_dict[separate_keys[-1]] = value
+ return unflattened_dict