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

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

Към профила на Деница Петрова

Резултати

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

Код

def extract_type(dictionary, the_type):
result = [str(x[0])*x[1] for x in dictionary if type(x[0]) is the_type]
return ''.join(result)
def reversed_dict(dictionary):
return dict((value, key) for key, value in dictionary.items())
def helper(dictionary, keys):
result = {}
for key, value in dictionary.items():
if isinstance(value, dict):
result.update(helper(value, keys + [key]))
else:
result['.'.join(keys + [key])] = value
return result
def flatten_dict(dictionary):
return helper(dictionary, [])
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
segments = key.split('.')
current_content = result
for i in segments[:-1]:
if i not in current_content:
current_content[i] = {}
current_content = current_content[i]
current_content[segments[-1]] = value
return result
def reps(collection):
return tuple([item for item in collection if collection.count(item) > 1])

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

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

OK

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

Деница обнови решението на 21.03.2015 17:35 (преди над 9 години)

+def extract_type(dictionary, the_type):
+ result = [str(x[0])*x[1] for x in dictionary if type(x[0]) is the_type]
+ return ''.join(result)
+
+
+def reversed_dict(dictionary):
+ return dict((value, key) for key, value in dictionary.items())
+
+
+def helper(dictionary, keys):
+ result = {}
+ for key, value in dictionary.items():
+ if isinstance(value, dict):
+ result.update(helper(value, keys + [key]))
+ else:
+ result['.'.join(keys + [key])] = value
+ return result
+
+
+def flatten_dict(dictionary):
+ return helper(dictionary, [])
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ segments = key.split('.')
+ current_content = result
+ for i in segments[:-1]:
+ if i not in current_content:
+ current_content[i] = {}
+ current_content = current_content[i]
+ current_content[segments[-1]] = value
+ return result
+
+
+def reps(collection):
+ return tuple([item for item in collection if collection.count(item) > 1])