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

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

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

Резултати

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

Код

def extract_type(pairs, filter_type):
pairs_of_type = filter(lambda pair: type(pair[0]) is filter_type, pairs)
return str.join('', [str(pair[0]) * pair[1] for pair in pairs_of_type])
def reversed_dict(dictionary):
return dict([(value, key) for key, value in dictionary.items()])
KEYS_SEPARATOR = '.'
def flatten_dict(dictionary, parent_name=''):
flat_dictionary = []
for key, value in dictionary.items():
key_name = parent_name + KEYS_SEPARATOR + key if parent_name else key
if isinstance(value, dict):
flat_dictionary.extend(flatten_dict(value, key_name).items())
else:
flat_dictionary.append((key_name, value))
return dict(flat_dictionary)
def unflatten_dict(dictionary):
unflat_dictionary = {}
for key, value in dictionary.items():
nested_dictionary = unflat_dictionary
parent_keys = key.split(KEYS_SEPARATOR)
for parent_key in parent_keys[:-1]:
nested_dictionary = nested_dictionary.setdefault(parent_key, {})
nested_dictionary[parent_keys[-1]] = value
return unflat_dictionary
def reps(list):
return tuple([element for element in list if list.count(element) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Марио обнови решението на 23.03.2015 15:00 (преди над 9 години)

+def extract_type(list, type):
+ list_of_types = [str(e[0]) * e[1] for e in list if isinstance(e[0], type)]
+ return str.join('', list_of_types)
+
+
+def reversed_dict(dictionary):
+ return dict([(value, key) for key, value in dictionary.items()])
+
+
+KEYS_SEPARATOR = '.'
+
+
+def flatten_dict(dictionary, parent_name=''):
+ flat_dictionary = []
+
+ for key, value in dictionary.items():
+ key_name = parent_name + KEYS_SEPARATOR + key if parent_name else key
+ if isinstance(value, dict):
+ flat_dictionary.extend(flatten_dict(value, key_name).items())
+ else:
+ flat_dictionary.append((key_name, value))
+
+ return dict(flat_dictionary)
+
+
+def unflatten_dict(dictionary):
+ unflat_dictionary = {}
+
+ for key, value in dictionary.items():
+ nested_dictionary = unflat_dictionary
+
+ parent_keys = key.split(KEYS_SEPARATOR)
+ for parent_key in parent_keys[:-1]:
+ nested_dictionary = nested_dictionary.setdefault(parent_key, {})
+
+ nested_dictionary[parent_keys[-1]] = value
+
+ return unflat_dictionary
+
+
+def reps(list):
+ return tuple([element for element in list if list.count(element) > 1])

Марио обнови решението на 23.03.2015 15:09 (преди над 9 години)

-def extract_type(list, type):
- list_of_types = [str(e[0]) * e[1] for e in list if isinstance(e[0], type)]
- return str.join('', list_of_types)
+def extract_type(pairs, filter_type):
+ pairs_of_type = filter(lambda pair: type(pair[0]) is filter_type, pairs)
+ return str.join('', [str(pair[0]) * pair[1] for pair in pairs_of_type])
def reversed_dict(dictionary):
return dict([(value, key) for key, value in dictionary.items()])
KEYS_SEPARATOR = '.'
def flatten_dict(dictionary, parent_name=''):
flat_dictionary = []
for key, value in dictionary.items():
key_name = parent_name + KEYS_SEPARATOR + key if parent_name else key
if isinstance(value, dict):
flat_dictionary.extend(flatten_dict(value, key_name).items())
else:
flat_dictionary.append((key_name, value))
return dict(flat_dictionary)
def unflatten_dict(dictionary):
unflat_dictionary = {}
for key, value in dictionary.items():
nested_dictionary = unflat_dictionary
parent_keys = key.split(KEYS_SEPARATOR)
for parent_key in parent_keys[:-1]:
nested_dictionary = nested_dictionary.setdefault(parent_key, {})
nested_dictionary[parent_keys[-1]] = value
return unflat_dictionary
def reps(list):
return tuple([element for element in list if list.count(element) > 1])