Деян обнови решението на 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