Елена обнови решението на 18.03.2015 20:48 (преди над 9 години)
+from functools import reduce
+from collections import defaultdict
+
+
+def extract_type(compressed_text, type_):
+ def to_string(pair):
+ return str(pair[0]) * pair[1] if type(pair[0]) is type_ else ""
+ return ''.join(map(to_string, compressed_text))
+
+
+def reversed_dict(dictionary):
+ return {v: k for k, v in dictionary.items()}
+
+
+def flatten_dict(dictionary):
+ def flatten(value, keys=[]):
+ if isinstance(value, dict):
+ return sum([flatten(v, keys + [k]) for k, v in value.items()], [])
+ return [(".".join(keys), value)]
+
+ return dict(flatten(dictionary))
+
+
+def unflatten_dict(dictionary):
+ def merge(source, value):
+ merged = source
+ keys = value[0].split(".")
+ for key in keys[0:-1]:
+ if not(isinstance(merged.get(key, None), dict)):
+ merged[key] = {}
+ merged = merged[key]
+ merged[keys[-1]] = value[1]
+ return source
+
+ return reduce(merge, dictionary.items(), {})
+
+
+def reps(values):
+ return tuple(filter(lambda x: values.count(x) > 1, values))