Мариян обнови решението на 20.03.2015 23:47 (преди над 9 години)
+def extract_type(symbols_and_repeats, chosen_type):
+ filtered_symbols = list(filter((lambda x: type(x[0]) is chosen_type),
+ symbols_and_repeats))
+ result = ''
+ for symbols, repeats in filtered_symbols:
+ for i in range(repeats):
+ result += symbols
+ return result
+
+
+def reversed_dict(dictionary):
+ countries_and_cities = dictionary.items()
+ result = {}
+ for country, city in countries_and_cities:
+ if city not in result:
+ result[city] = country
+ return result
+
+
+def check_dictionary(old_key, item_for_check):
+ new_keys_and_values = []
+ for key, value in item_for_check.items():
+ if type(value) is not dict:
+ new_keys_and_values.append((old_key + '.' + key, value))
+ else:
+ for x in check_dictionary((old_key + '.' + key), value):
+ new_keys_and_values.append(x)
+ return new_keys_and_values
+
+
+def flatten_dict(d):
+ result = {}
+ new_keys_and_values = []
+ for key, value in d.items():
+ if type(value) is not dict:
+ result[key] = value
+ else:
+ new_keys_and_values = check_dictionary(key, value)
+ for k, v in new_keys_and_values:
+ result[k] = v
+ new_keys_and_values = []
+ return result
+
+
+def reps(elements):
+ return [x for x in elements if elements.count(x) > 1]
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ context = result
+ for k in key.split('.')[:-1]:
+ if k not in context:
+ context[k] = {}
+ context = context[k]
+ context[key.split('.')[-1]] = value
+ return result