Решение на Пет малки функции от Йоан Динков

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

Към профила на Йоан Динков

Резултати

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

Код

def extract_type(list, filter_type):
return ''.join([str(element) * count for element, count
in list if type(element) is filter_type])
def reversed_dict(dict):
return {value: key for (key, value) in dict.items()}
def flatten_dict(unflatten):
def items():
for key, value in unflatten.items():
if isinstance(value, dict):
for subkey, subvalue in flatten_dict(value).items():
yield key + "." + subkey, subvalue
else:
yield key, value
return dict(items())
def unflatten_dict(flatten):
result_dict = dict()
for key, value in flatten.items():
subkeys = key.split(".")
current_dict = result_dict
for subkey in subkeys[:-1]:
if subkey not in current_dict:
current_dict[subkey] = dict()
current_dict = current_dict[subkey]
current_dict[subkeys[-1]] = value
return result_dict
def reps(list):
return tuple(x for x in list if list.count(x) != 1)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.138s

OK

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

Йоан обнови решението на 22.03.2015 23:13 (преди над 9 години)

+def extract_type(list, filter_type):
+ return ''.join([str(element) * count for element, count
+ in list if type(element) is filter_type])
+
+
+def reversed_dict(dict):
+ return {value: key for (key, value) in dict.items()}
+
+
+def flatten_dict(unflatten):
+ def items():
+ for key, value in unflatten.items():
+ if isinstance(value, dict):
+ for subkey, subvalue in flatten_dict(value).items():
+ yield key + "." + subkey, subvalue
+ else:
+ yield key, value
+
+ return dict(items())
+
+
+def unflatten_dict(flatten):
+ result_dict = dict()
+ for key, value in flatten.items():
+ subkeys = key.split(".")
+ current_dict = result_dict
+ for subkey in subkeys[:-1]:
+ if subkey not in current_dict:
+ current_dict[subkey] = dict()
+ current_dict = current_dict[subkey]
+ current_dict[subkeys[-1]] = value
+
+ return result_dict
+
+
+def reps(list):
+ return tuple(x for x in list if list.count(x) != 1)