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

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

Към профила на Валентина Жекова

Резултати

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

Код

def extract_type(data, kind):
return "".join([str(item[0]) * item[1] for item in data
if type(item[0]) is kind])
def reversed_dict(original):
return {original[key]: key for key in original}
def flatten_dict(data):
flatten_by_one_level = {}
for key, value in data.items():
if isinstance(value, dict):
item = {".".join([key, key1]): value[key1] for key1 in value}
else:
item = {key: value}
flatten_by_one_level.update(item)
yet_remain = {key for key in flatten_by_one_level
if isinstance(flatten_by_one_level[key], dict)}
if not yet_remain:
result = flatten_by_one_level
else:
result = flatten_dict(flatten_by_one_level)
return result
def unflatten_dict(data):
result = {}
for key, value in data.items():
parts = key.split(".")
inner_level = result
for part in parts[:-1]:
if part not in inner_level:
inner_level[part] = {}
inner_level = inner_level[part]
inner_level[parts[-1]] = value
return result
def reps(data):
return tuple([item for item in data if data.count(item) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.132s

OK

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

Валентина обнови решението на 21.03.2015 00:08 (преди над 9 години)

+def extract_type(data, kind):
+ return "".join([str(item[0]) * item[1] for item in data
+ if type(item[0]) is kind])
+
+
+def reversed_dict(original):
+ return {original[key]: key for key in original}
+
+
+def flatten_dict(data):
+ flatten_by_one_level = {}
+ for key, value in data.items():
+ if isinstance(value, dict):
+ item = {".".join([key, key1]): value[key1] for key1 in value}
+ else:
+ item = {key: value}
+ flatten_by_one_level.update(item)
+
+ yet_remain = {key for key in flatten_by_one_level
+ if isinstance(flatten_by_one_level[key], dict)}
+ if not yet_remain:
+ result = flatten_by_one_level
+ else:
+ result = flatten_dict(flatten_by_one_level)
+ return result
+
+
+def unflatten_dict(data):
+ result = {}
+ for key, value in data.items():
+ parts = key.split(".")
+ inner_level = result
+ for part in parts[:-1]:
+ if part not in inner_level:
+ inner_level[part] = {}
+ inner_level = inner_level[part]
+ inner_level[parts[-1]] = value
+ return result
+
+
+def reps(data):
+ return tuple([item for item in data if data.count(item) > 1])