Решение на Пет малки функции от Иван Георгиев

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

Към профила на Иван Георгиев

Резултати

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

Код

def extract_type(sequence, some_type):
return ''.join([str(item[0]) * item[1] for item in sequence
if type(item[0]) == some_type])
def reversed_dict(dictionary):
return {value: key for (key, value) in dictionary.items()}
def reps(sequence):
return tuple([item for item in sequence if sequence.count(item) != 1])
def flatten(dictionary, result):
for key, value in dictionary.items():
if not isinstance(value, dict):
result[key] = value
else:
new_dict = {key + '.' + k: v for (k, v) in value.items()}
flatten(new_dict, result)
return result
def flatten_dict(dictionary):
return flatten(dictionary, {})
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
key_items = key.rsplit('.')
d = result
for key_item in key_items[:-1]:
if key_item not in d:
d[key_item] = {}
d = d[key_item]
d[key_items[-1]] = value
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.135s

OK

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

Иван обнови решението на 21.03.2015 02:11 (преди над 9 години)

+def extract_type(sequence, some_type):
+ return ''.join([str(item[0]) * item[1] for item in sequence
+ if type(item[0]) == some_type])
+
+
+def reversed_dict(dictionary):
+ return {value: key for (key, value) in dictionary.items()}
+
+
+def reps(sequence):
+ return tuple([item for item in sequence if sequence.count(item) != 1])
+
+
+def flatten(dictionary, result):
+ for key, value in dictionary.items():
+ if not isinstance(value, dict):
+ result[key] = value
+ else:
+ new_dict = {key + '.' + k: v for (k, v) in value.items()}
+ flatten(new_dict, result)
+ return result
+
+
+def flatten_dict(dictionary):
+ return flatten(dictionary, {})
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ key_items = key.rsplit('.')
+ d = result
+ for key_item in key_items[:-1]:
+ if key_item not in d:
+ d[key_item] = {}
+ d = d[key_item]
+ d[key_items[-1]] = value
+ return result