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

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

Към профила на Христо Петков

Резултати

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

Код

def extract_type(symbol_count, data_type):
return ''.join(list(map((lambda x: str(x[0]) * x[1]),
filter((lambda x: type(x[0]) == data_type),
symbol_count))))
def reversed_dict(dictionary):
return dict(map((lambda x: (dictionary[x], x)), dictionary))
def reps(elements):
return tuple(filter((lambda x: elements.count(x) > 1), elements))
def flatten_dict(dictionary):
return flatten(dictionary)
def unflatten_dict(dictionary):
unflattened = {}
for item in dictionary.items():
temp_dict = unflattened
keys = str(item[0]).split('.')
for key in keys[:-1]:
if temp_dict.get(key, False):
temp_dict = temp_dict[key]
else:
temp_dict.update({key: {}})
temp_dict = temp_dict[key]
temp_dict[keys[-1]] = item[1]
return unflattened
def flatten(dictionary, key='', current={}):
for i, j in dictionary.items():
temp_key = key + '.' + str(i) if key else str(i)
if isinstance(j, dict):
current.update(flatten(j, key + '.' + i if key else i,
current))
else:
current[key + '.' + i if key else i] = j
return current

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.137s

OK

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

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

+def extract_type(symbol_count, data_type):
+ return ''.join(list(map((lambda x: str(x[0]) * x[1]),
+ filter((lambda x: type(x[0]) == data_type),
+ symbol_count))))
+
+
+def reversed_dict(dictionary):
+ return dict(map((lambda x: (blah[x], x)), dictionary))
+
+
+def reps(elements):
+ return tuple(filter((lambda x: listt.count(x) > 1), elements))

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

def extract_type(symbol_count, data_type):
return ''.join(list(map((lambda x: str(x[0]) * x[1]),
filter((lambda x: type(x[0]) == data_type),
symbol_count))))
def reversed_dict(dictionary):
- return dict(map((lambda x: (blah[x], x)), dictionary))
+ return dict(map((lambda x: (dictionary[x], x)), dictionary))
def reps(elements):
- return tuple(filter((lambda x: listt.count(x) > 1), elements))
+ return tuple(filter((lambda x: elements.count(x) > 1), elements))

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

def extract_type(symbol_count, data_type):
return ''.join(list(map((lambda x: str(x[0]) * x[1]),
filter((lambda x: type(x[0]) == data_type),
symbol_count))))
def reversed_dict(dictionary):
return dict(map((lambda x: (dictionary[x], x)), dictionary))
def reps(elements):
return tuple(filter((lambda x: elements.count(x) > 1), elements))
+
+
+def flatten_dict(dictionary):
+ return flatten(dictionary)
+
+
+def unflatten_dict(dictionary):
+ unflattened = {}
+ for item in dictionary.items():
+ temp_dict = unflattened
+ keys = str(item[0]).split('.')
+ for key in keys[:-1]:
+ if temp_dict.get(key, False):
+ temp_dict = temp_dict[key]
+ else:
+ temp_dict.update({key: {}})
+ temp_dict = temp_dict[key]
+ temp_dict[keys[-1]] = item[1]
+ return unflattened
+
+
+def flatten(dictionary, key='', current={}):
+ for i, j in dictionary.items():
+ temp_key = key + '.' + str(i) if key else str(i)
+ if isinstance(j, dict):
+ current.update(flatten(j, key + '.' + i if key else i,
+ current))
+ else:
+ current[key + '.' + i if key else i] = j
+ return current