Решение на Пет малки функции от Слави Кадиев

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

Към профила на Слави Кадиев

Резултати

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

Код

def extract_type(objects, object_type):
return "".join([
str(elements[0]) * elements[1] for elements in objects
if type(elements[0]) is object_type])
def reversed_dict(dictionary):
return {key: value for key in dictionary.values()
for value in dictionary.keys() if key == dictionary[value]}
def reps(items):
return tuple([element for element in list(items)
if items.count(element) != 1])
def check_value(key, value_dictionary):
return {key + '.' + element: value_dictionary[element]
for element in value_dictionary.keys()}
def flatten_dict(dictionary):
if all(type(value) is not dict for value in dictionary.values()):
return dictionary
else:
result = {}
for element, value in dictionary.items():
if type(value) is not dict:
result[element] = value
else:
for item in check_value(element, value).items():
result[item[0]] = item[1]
return flatten_dict(result)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.135s

OK

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

Слави обнови решението на 23.03.2015 16:31 (преди над 9 години)

+def extract_type(objects, object_type):
+ return "".join([
+ str(elements[0]) * elements[1] for elements in objects
+ if type(elements[0]) is object_type])
+
+
+def reversed_dict(dictionary):
+ return {key: value for key in dictionary.values()
+ for value in dictionary.keys() if key == dictionary[value]}
+
+
+def reps(items):
+ return tuple([element for element in list(items)
+ if items.count(element) != 1])
+
+
+def check_value(key, value_dictionary):
+ return {key + '.' + element: value_dictionary[element]
+ for element in value_dictionary.keys()}
+
+
+def flatten_dict(dictionary):
+ if all(type(value) is not dict for value in dictionary.values()):
+ return dictionary
+ else:
+ result = {}
+ for element, value in dictionary.items():
+ if type(value) is not dict:
+ result[element] = value
+ else:
+ for item in check_value(element, value).items():
+ result[item[0]] = item[1]
+ return flatten_dict(result)