Решение на Пет малки функции от Атанас Янчевски

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

Към профила на Атанас Янчевски

Резултати

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

Код

def extract_type(list_, type_):
result = ""
for element in list_:
if type(element[0]) == type_:
for i in range(element[1]):
result += str(element[0])
return result
def reversed_dict(dict_):
result_dict = {}
for element in dict_.items():
result_dict[element[1]] = element[0]
return result_dict
def reps(list_):
return tuple([x for x in list_ if list_.count(x) > 1])
def flatten_dict_helper(dict_, result_key=''):
result_dict = {}
for key, value in dict_.items():
key = result_key + '.' + key if result_key else key
if(type(value) != dict):
result_dict[key] = value
else:
result_dict.update(flatten_dict_helper(value, key))
return result_dict
def flatten_dict(dict_):
return flatten_dict_helper(dict_, '')
def unflatten_dict(dict_):
result_dict = {}
for key, value in dict_.items():
key = key.split('.')
helper_dict = result_dict
for item in key[:-1]:
if item not in helper_dict:
helper_dict[item] = {}
helper_dict = helper_dict[item]
helper_dict[key[-1]] = value
return result_dict

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Атанас обнови решението на 22.03.2015 23:56 (преди над 9 години)

+def extract_type(list_, type_):
+ result = ""
+ for element in list_:
+ if type(element[0]) == type_:
+ for i in range(element[1]):
+ result += str(element[0])
+ return result
+
+
+def reversed_dict(dict_):
+ result_dict = {}
+ for element in dict_.items():
+ result_dict[element[1]] = element[0]
+ return result_dict
+
+
+def reps(list_):
+ return tuple([x for x in list_ if list_.count(x) > 1])
+
+
+def flatten_dict_helper(dict_, result_key=''):
+ result_dict = {}
+ for key, value in dict_.items():
+ key = result_key + '.' + key if result_key else key
+ if(type(value) != dict):
+ result_dict[key] = value
+ else:
+ result_dict.update(flatten_dict_helper(value, key))
+ return result_dict
+
+
+def flatten_dict(dict_):
+ return flatten_dict_helper(dict_, '')
+
+
+def unflatten_dict(dict_):
+ result_dict = {}
+ for key, value in dict_.items():
+ key = key.split('.')
+ helper_dict = result_dict
+ for item in key[:-1]:
+ if item not in helper_dict:
+ helper_dict[item] = {}
+ helper_dict = helper_dict[item]
+ helper_dict[key[-1]] = value
+ return result_dict