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

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

Към профила на Никола Монов

Резултати

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

Код

import collections
def extract_type(items, extracted_type):
result = ""
for i in items:
if type(i[0]) is extracted_type:
for x in range(0, i[1]):
result += str(i[0])
return result
def reversed_dict(dic):
return {x: y for y, x in dic.items()}
def flatten_dict(dictionary):
return flatten_helper(dictionary, "", dict())
def flatten_helper(dictionary, parent_key, result):
for key, val in dictionary.items():
if isinstance(val, dict):
flatten_helper(val, parent_key + key + ".", result)
else:
result.update({parent_key + key: val})
return result
def unflatten_dict(dictionary):
result = dict()
for key, value in dictionary.items():
levels = key.split(".")
temp = result
last_idx = len(levels)-1
for i in range(0, last_idx):
if levels[i] not in temp:
temp[levels[i]] = dict()
temp = temp[levels[i]]
temp[levels[last_idx]] = value
return result
def reps(array):
return tuple([x for x in array if array.count(x) > 1])

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

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

OK

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

Никола обнови решението на 23.03.2015 01:07 (преди над 9 години)

+import collections
+
+
+def extract_type(items, extracted_type):
+ result = ""
+ for i in items:
+ if type(i[0]) is extracted_type:
+ for x in range(0, i[1]):
+ result += str(i[0])
+ return result
+
+
+def reversed_dict(dic):
+ return {x: y for y, x in dic.items()}
+
+
+def flatten_dict(dictionary):
+ return flatten_helper(dictionary, "", dict())
+
+
+def flatten_helper(dictionary, parent_key, result):
+ for key, val in dictionary.items():
+ if isinstance(val, dict):
+ flatten_helper(val, parent_key + key + ".", result)
+ else:
+ result.update({parent_key + key: val})
+ return result
+
+
+def unflatten_dict(dictionary):
+ result = dict()
+ for key, value in dictionary.items():
+ levels = key.split(".")
+ temp = result
+ last_idx = len(levels)-1
+ for i in range(0, last_idx):
+ if levels[i] not in temp:
+ temp[levels[i]] = dict()
+ temp = temp[levels[i]]
+ temp[levels[last_idx]] = value
+ return result
+
+
+def reps(array):
+ return tuple([x for x in array if array.count(x) > 1])