Решение на Пет малки функции от Кристиян Бъновски

Обратно към всички решения

Към профила на Кристиян Бъновски

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 19 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def extract_type(to_be_filtered, filter_type):
for item_to_filter in to_be_filtered:
if type(item_to_filter[0]) == filter_type:
return str(item_to_filter[0]) * item_to_filter[1] + str(extract_type(to_be_filtered[1:], filter_type))
else:
return extract_type(to_be_filtered[1:], filter_type)
return ""
def reversed_dict(to_be_reverse):
return dict(zip(to_be_reverse.values(), to_be_reverse.keys()))
def flatten_dict(to_be_flatten, agregated_key=''):
flattened = {}
for current_key, val in to_be_flatten.items():
new_key = agregated_key + current_key
if type(val) is not dict:
flattened[new_key] = val
else:
flattened.update(flatten_dict(val, new_key + '.'))
return flattened
def unflatten_dict(to_be_unflatten):
unflattened = {}
for key, value in to_be_unflatten.items():
agregated_keys = key.split(".")
temp_unflattened = unflattened
for agregated_key in agregated_keys[:-1]:
if agregated_key not in temp_unflattened:
temp_unflattened[agregated_key] = dict()
temp_unflattened = temp_unflattened[agregated_key]
temp_unflattened[agregated_keys[-1]] = value
return unflattened
def reps(to_be_repsed):
return tuple([x for x in to_be_repsed if to_be_repsed.count(x) > 1])

Лог от изпълнението

...................
----------------------------------------------------------------------
Ran 19 tests in 0.204s

OK

История (1 версия и 0 коментара)

Кристиян обнови решението на 23.03.2015 16:11 (преди над 9 години)

+def extract_type(to_be_filtered, filter_type):
+ for item_to_filter in to_be_filtered:
+ if type(item_to_filter[0]) == filter_type:
+ return str(item_to_filter[0]) * item_to_filter[1] + str(extract_type(to_be_filtered[1:], filter_type))
+ else:
+ return extract_type(to_be_filtered[1:], filter_type)
+ return ""
+
+
+def reversed_dict(to_be_reverse):
+ return dict(zip(to_be_reverse.values(), to_be_reverse.keys()))
+
+
+def flatten_dict(to_be_flatten, agregated_key=''):
+ flattened = {}
+ for current_key, val in to_be_flatten.items():
+ new_key = agregated_key + current_key
+ if type(val) is not dict:
+ flattened[new_key] = val
+ else:
+ flattened.update(flatten_dict(val, new_key + '.'))
+ return flattened
+
+
+def unflatten_dict(to_be_unflatten):
+ unflattened = {}
+ for key, value in to_be_unflatten.items():
+ agregated_keys = key.split(".")
+ temp_unflattened = unflattened
+ for agregated_key in agregated_keys[:-1]:
+ if agregated_key not in temp_unflattened:
+ temp_unflattened[agregated_key] = dict()
+ temp_unflattened = temp_unflattened[agregated_key]
+ temp_unflattened[agregated_keys[-1]] = value
+ return unflattened
+
+
+def reps(to_be_repsed):
+ return tuple([x for x in to_be_repsed if to_be_repsed.count(x) > 1])