Николай обнови решението на 20.03.2015 22:45 (преди над 9 години)
+
+def extract_type(pairs, actual_type):
+ return ''.join([''.join([str(pair[0]) for _ in range(0, pair[1])])
+ for pair in pairs if type(pair[0]) is actual_type])
+
+
+def flatten_element(element, pairs):
+ if type(pairs[element]) is not dict:
+ return [(element, pairs[element])]
+ else:
+ result = []
+
+ for elem in pairs[element]:
+ flat_child = flatten_element(elem, pairs[element])
+ for flat_child in flat_child:
+ result.append(("{}.{}".format(element,
+ flat_child[0]), flat_child[1]))
+
+ return result
+
+
+def flatten_dict(non_flat):
+ flat_elements = [pair for pair in
+ map(lambda x: flatten_element(x, non_flat), non_flat)]
+
+ flat = []
+
+ for x in flat_elements:
+ flat.extend(x)
+
+ return {elem[0]: elem[1] for elem in flat}
+
+
+def unflatten_dict(flat):
+ result = {}
+
+ for elem in flat:
+ if elem.find('.') == -1:
+ result[elem] = flat[elem]
+ else:
+ sub_dict = filter(lambda x: x[0] == elem[0]
+ and x.find('.') != -1, flat)
+ dot_erased = {x: flat["{}.{}".format(elem[0], x)] for
+ x in map(lambda x: x.partition('.')[2], sub_dict)}
+ result[elem[0]] = unflatten_dict(dot_erased)
+
+ return result
+
+
+def reversed_dict(original):
+ return {original[pair]: pair for pair in original}
+
+
+def reps(elements):
+ return tuple([x for x in
+ filter(lambda x: elements.count(x) > 1, elements)])