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

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

Към профила на Теодор Климентов

Резултати

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

Код

from functools import reduce
def extract_type(input_list, needed_type):
result = list(filter(lambda x: type(x[0]) == needed_type, input_list))
if not result:
return ""
result = map(lambda x: str(x[0]) * x[1], result)
return reduce(lambda x, y: x + y, result)
def reversed_dict(input_dict):
return dict(map(lambda x: (input_dict[x], x), input_dict))
def flatten_dict(input_dict, prefix=""):
result = {}
for key in input_dict:
elem = input_dict[key]
if type(elem) is dict:
result.update(flatten_dict(elem, prefix + key + "."))
else:
result[prefix + key] = elem
return result
def insert_deep(target_dict, key, item):
keys = key.split('.', 1)
if len(keys) == 1:
target_dict[key] = item
return
if keys[0] not in target_dict:
target_dict[keys[0]] = {}
insert_deep(target_dict[keys[0]], keys[1], item)
def unflatten_dict(input_dict):
result = {}
for item in input_dict.items():
insert_deep(result, item[0], item[1])
return result
def reps(elements):
return tuple(filter(lambda x: elements.count(x) > 1, elements))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.175s

OK

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

Теодор обнови решението на 18.03.2015 22:59 (преди над 9 години)

+from functools import reduce
+
+
+def extract_type(input_list, needed_type):
+ result = filter(lambda x: type(x[0]) == needed_type, input_list)
+ result = map(lambda x: str(x[0]) * x[1], result)
+ return reduce(lambda x, y: x + y, result)
+
+
+def reversed_dict(input_dict):
+ return dict(map(lambda x: (input_dict[x], x), input_dict))
+
+
+def flatten_dict(input_dict, prefix=""):
+ result = {}
+ for key in input_dict:
+ elem = input_dict[key]
+ if type(elem) is dict:
+ result.update(flatten_dict(elem, prefix + key + "."))
+ else:
+ result[prefix + key] = elem
+ return result
+
+
+def insert_deep(target_dict, key, item):
+ keys = key.split('.', 1)
+ if len(keys) == 1:
+ target_dict[key] = item
+ return
+ if keys[0] not in target_dict:
+ target_dict[keys[0]] = {}
+ insert_deep(target_dict[keys[0]], keys[1], item)
+
+
+def unflatten_dict(input_dict):
+ result = {}
+ for item in input_dict.items():
+ insert_deep(result, item[0], item[1])
+ return result
+
+
+def reps(elements):
+ return tuple(filter(lambda x: elements.count(x) > 1, elements))

Теодор обнови решението на 21.03.2015 15:40 (преди над 9 години)

from functools import reduce
def extract_type(input_list, needed_type):
- result = filter(lambda x: type(x[0]) == needed_type, input_list)
+ result = list(filter(lambda x: type(x[0]) == needed_type, input_list))
+ if not result:
+ return ""
result = map(lambda x: str(x[0]) * x[1], result)
return reduce(lambda x, y: x + y, result)
def reversed_dict(input_dict):
return dict(map(lambda x: (input_dict[x], x), input_dict))
def flatten_dict(input_dict, prefix=""):
result = {}
for key in input_dict:
elem = input_dict[key]
if type(elem) is dict:
result.update(flatten_dict(elem, prefix + key + "."))
else:
result[prefix + key] = elem
return result
def insert_deep(target_dict, key, item):
keys = key.split('.', 1)
if len(keys) == 1:
target_dict[key] = item
return
if keys[0] not in target_dict:
target_dict[keys[0]] = {}
insert_deep(target_dict[keys[0]], keys[1], item)
def unflatten_dict(input_dict):
result = {}
for item in input_dict.items():
insert_deep(result, item[0], item[1])
return result
def reps(elements):
- return tuple(filter(lambda x: elements.count(x) > 1, elements))
+ return tuple(filter(lambda x: elements.count(x) > 1, elements))