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

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

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

Резултати

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

Код

import collections
def extract_type(archived, type_):
extracted = [str(val) * rep for val, rep in archived if type(val) is type_]
return "".join(extracted)
def reversed_dict(dictionary):
return {value: key for key, value in dictionary.items()}
def reps(collection):
counter = collections.Counter(collection)
return tuple([item for item in collection if counter[item] > 1])
def flatten_dict(dictionary):
flattened = {}
for key, value in dictionary.items():
if type(value) is dict:
for k, v in value.items():
flattened[key + '.' + k] = v
else:
flattened[key] = value
if any(type(value) is dict for key, value in flattened.items()):
return flatten_dict(flattened)
else:
return flattened

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

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

OK

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

Кристиан обнови решението на 22.03.2015 22:18 (преди над 9 години)

+import collections
+
+
+def extract_type(archived, type_):
+ extracted = [str(val) * rep for val, rep in archived if type(val) is type_]
+ return "".join(extracted)
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def reps(collection):
+ counter = collections.Counter(collection)
+ return tuple([item for item in collection if counter[item] > 1])
+
+
+def flatten_dict(dictionary):
+ flattened = {}
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ for k, v in value.items():
+ flattened[key + '.' + k] = v
+ else:
+ flattened[key] = value
+ if any(type(value) is dict for key, value in flattened.items()):
+ return flatten_dict(flattened)
+ else:
+ return flattened