Диана обнови решението на 19.03.2015 20:35 (преди над 9 години)
+from functools import reduce
+
+
+def extract_type(pairs, wanted_type):
+ pairs_of_type = (list(filter(lambda x: type(x[0]) is wanted_type, pairs)))
+ return reduce(lambda x, y: x + str(y[0])*y[1], pairs_of_type, "")
+
+
+def reversed_dict(regular_order):
+ return {value: key for key, value in regular_order.items()}
+
+
+def reps(collection):
+ return tuple(filter(lambda x: collection.count(x) > 1, collection))
+
+
+def flatten_dict(unflattened):
+ pair_queue = list(unflattened.items())
+ flattened = {}
+ while pair_queue:
+ key, value = pair_queue[0]
+ if(type(value) is dict):
+ for internal_key, internal_value in value.items():
+ pair_queue.append((key + '.' + internal_key, internal_value))
+ else:
+ flattened[key] = value
+ pair_queue.remove((key, value))
+ return flattened
+
+
+def unflatten_dict(flattened):
+ if type(flattened) is not dict:
+ return flattened
+ unflattened = {}
+ for key, value in flattened.items():
+ if '.' not in key:
+ unflattened[key] = value
+ else:
+ first = key.split('.', 1)[0]
+ rest = key.split('.', 1)[1]
+ if first not in unflattened:
+ unflattened[first] = {rest: value}
+ else:
+ unflattened[first][rest] = value
+ return {key: unflatten_dict(value) for key, value in unflattened.items()}