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

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

Към профила на Станислав Димитров

Резултати

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

Код

from functools import reduce
# import operator
def extract_type(list_, type_):
extract = list(filter((lambda x: type(x[0]) == type_), list_))
extract = list(map((lambda x: str(x[0]) * x[1]), extract))
extract = reduce((lambda x, y: x + y), extract)
return extract
def reversed_dict(dictionary):
reverse = {}
for key in dictionary:
reverse[dictionary[key]] = key
return reverse
def flatten_dict(dictionary):
new_dic = {}
for x in dictionary:
if type(dictionary[x]) is type({}):
for y in dictionary[x]:
if type(dictionary[x][y]) is not type({}):
new_key = str(x + "." + y)
new_dic[new_key] = dictionary[x][y]
else:
for z in dictionary[x][y]:
new_key = str(x + "." + y + "." + z)
new_dic[new_key] = dictionary[x][y][z]
else:
new_dic[x] = dictionary[x]
return new_dic
def unflatten_dict(dictionary):
# Грип ме тръшна, едвам направих другите :)
pass
def reps(elements):
return tuple([item for item in elements if elements.count(item) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.169s

OK

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

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

+from functools import reduce
+# import operator
+
+
+def extract_type(list_, type_):
+
+ extract = list(filter((lambda x: type(x[0]) == type_), list_))
+ extract = list(map((lambda x: str(x[0]) * x[1]), extract))
+ extract = reduce((lambda x, y: x + y), extract)
+ return extract
+
+
+def reversed_dict(dictionary):
+
+ reverse = {}
+ for key in dictionary:
+ reverse[dictionary[key]] = key
+ return reverse
+
+
+def flatten_dict(dictionary):
+
+ new_dic = {}
+
+ for x in dictionary:
+ if type(dictionary[x]) is type({}):
+ for y in dictionary[x]:
+ if type(dictionary[x][y]) is not type({}):
+ new_key = str(x + "." + y)
+ new_dic[new_key] = dictionary[x][y]
+ else:
+ for z in dictionary[x][y]:
+ new_key = str(x + "." + y + "." + z)
+ new_dic[new_key] = dictionary[x][y][z]
+ else:
+ new_dic[x] = dictionary[x]
+ return new_dic
+
+
+def unflatten_dict(dictionary):
+ # Грип ме тръшна, едвам направих другите :)
+ pass
+
+
+def reps(elements):
+
+ return tuple([item for item in elements if elements.count(item) > 1])