Калоян обнови решението на 23.03.2015 12:17 (преди над 9 години)
+tempo_output = []
+out = []
+
+
+def reps(input_items):
+ return [x for x in input_items if input_items.count(x) != 1]
+
+
+def reversed_dict(input_items):
+ return {input_items[key]: key for key in input_items.keys()}
+
+
+def extract_type(input_items, tpe):
+ return "".join([str(x[0])*x[1] for x in input_items if type(x[0]) is tpe])
+
+
+def helper(key, value):
+ if key.count('.') == 0:
+ return dict([(key, value)])
+ index = key.index('.')
+ return dict([(key[:index], helper(key[index + 1:], value))])
+
+
+def unflatten_helper(input_key, value, curr_output_unflatten):
+ point_count = input_key.count('.')
+ if point_count != 0:
+ point_index = input_key.index('.')
+ curr_key = input_key[:point_index]
+ else:
+ curr_key = input_key
+ curr_output_unflatten[curr_key] = value
+ return
+ if point_count == 0:
+ curr_output_unflatten[curr_key] = value
+ return
+ else:
+ if curr_key in curr_output_unflatten.keys():
+ unflatten_helper(input_key[point_index + 1:], value,
+ curr_output_unflatten[curr_key])
+ else:
+ curr_output_unflatten[curr_key] = helper(input_key[
+ point_index + 1:], value)
+
+
+def unflatten_dict(input_data):
+ output_unflatten = {}
+ for key in input_data.keys():
+ unflatten_helper(key, input_data[key], output_unflatten)
+ return output_unflatten
+
+
+def flatten_helper(input_data):
+ global tempo_output
+ global out
+ for key in input_data.keys():
+ if type(input_data[key]) is dict:
+ tempo_output.append(key)
+ flatten_helper(input_data[key])
+ else:
+ tempo_output.append(key)
+ out.append((".".join(tempo_output), input_data[key]))
+ if len(tempo_output) != 0:
+ tempo_output.pop()
+ return
+
+
+def flatten_dict(input_elems):
+ flatten_helper(input_elems)
+ return dict(out)
- Виж какви типове се очаква да върнеш
- Не искаме да виждаме
global
в решенията ви -
flatten_dict
не работи коректно, тествай го малко повече