Александър обнови решението на 18.03.2015 11:02 (преди над 9 години)
+def extract_type(symbols, type_):
+ return ''.join([str(symbol) for symbol, times in symbols
+ for _ in range(times) if type(symbol) is type_])
+
+
+def reversed_dict(input_dict):
+ return {input_dict[k]: k for k in input_dict}
+
+
+def change_keys(mydict, prefix):
+ return dict((prefix + k, f(v) if hasattr(v, 'keys') else v)
+ for k, v in mydict.items())
+
+
+def flatten_dict(input_dict):
+ result = {}
+ dict_items = input_dict.items()
+ for key, value in dict_items:
+ if type(value) is dict:
+ more_results = flatten_dict(value)
+ result.update(change_keys(more_results, '{}.'.format(key)))
+ else:
+ result[key] = value
+ return result
+
+
+def unflatten_dict(input_dict):
+ result = {}
+ dict_items = input_dict.items()
+ for key, value in dict_items:
+ changing_dict = result
+ keys = key.split('.')
+ for sub_key in keys[:-1]:
+ if sub_key not in changing_dict:
+ changing_dict[sub_key] = {}
+ changing_dict = changing_dict[sub_key]
+ changing_dict[keys[-1]] = value
+ return result
+
+
+def reps(input_list):
+ return tuple([x for x in input_list if input_list.count(x) > 1])