Димитър обнови решението на 23.03.2015 12:59 (преди над 9 години)
+from collections import defaultdict
+from functools import reduce
+
+
+def extract_type(text, cls):
+ return "".join([str(symbol) * times for (symbol, times) in text
+ if type(symbol) == cls])
+
+
+def reversed_dict(input_dict):
+ return {input_dict[key]: key for key in input_dict}
+
+
+def flatten_dict(input_dict, parent_key='', sep='.'):
+ result = {}
+ for key, value in input_dict.items():
+ new_key = parent_key + sep + key if parent_key != '' else key
+ if isinstance(value, dict):
+ result.update(flatten_dict(value, new_key).items())
+ else:
+ result[new_key] = value
+
+ return dict(result)
+
+
+def recursive_update(dict1, dict2):
+ if all([key not in dict1 for key in dict2]):
+ dict1.update(dict2)
+ return dict1
+
+ if (all([not isinstance(value, dict) for value in dict1.values()]) and
+ all([not isinstance(value, dict) for value in dict2.values()])):
+ dict1.update(dict2)
+ return dict1
+
+ for key, value in dict2.items():
+ if key in dict1:
+ if isinstance(dict1[key], dict) and isinstance(dict2[key], dict):
+ dict1[key] = recursive_update(dict1[key], dict2[key])
+ else:
+ dict1[key] = dict2[key]
+
+ return dict1
+
+
+def unflatten_dict(input_dict, sep='.'):
+ result = {}
+ for key, value in input_dict.items():
+ dicts = key.split(sep)
+ dicts.append(value)
+ recursive_update(result, reduce(lambda x, y: {y: x}, dicts[::-1]))
+ return result
+
+
+def reps(input_collection):
+ return tuple([elem for elem in input_collection
+ if input_collection.count(elem) > 1])