Антоан обнови решението на 21.03.2015 00:51 (преди над 9 години)
+from functools import reduce
+
+
+def extract_type(archive, symbol_type):
+ return "".join([str(symbol) * count
+ for symbol, count in archive
+ if type(symbol) is symbol_type])
+
+
+def reversed_dict(dictionary):
+ return {dictionary[key]: key for key in dictionary}
+
+
+def flatten_dict(dictionary):
+
+ dict_copy = dict(dictionary)
+
+ for key in dictionary:
+ if type(dictionary[key]) is dict:
+ flatten_dict_recursive(dict_copy, dict_copy[key], key)
+ del dict_copy[key]
+ return dict_copy
+
+
+def flatten_dict_recursive(original_dictionary, sub_dictionary, recursion_key):
+ for key in sub_dictionary:
+ if type(sub_dictionary[key]) is dict:
+ flatten_dict_recursive(
+ original_dictionary, sub_dictionary[key],
+ recursion_key + "." + key)
+ else:
+ original_dictionary[
+ recursion_key + "." + key] = sub_dictionary[key]
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key in dictionary:
+ keys = key.split(".")
+ inner_most = reduce(get_dict_element, keys[:len(keys) - 1], result)
+ inner_most[keys[-1]] = dictionary[key]
+
+ return result
+
+
+def get_dict_element(dictionary, key):
+ dictionary[key] = dictionary[key] if key in dictionary else {}
+ return dictionary[key]
+
+
+def reps(list):
+ return tuple(item for item in list if list.count(item) > 1)