Решение на Пет малки функции от Божидар Иванов

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

Към профила на Божидар Иванов

Резултати

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

Код

import collections
def extract_type(elements, elements_type):
sub_elements = str()
for i in elements:
if type(i[0]) is elements_type:
sub_elements += str(i[0])*(i[1])
return sub_elements
def reversed_dict(default):
return dict((value,key) for key,value in default.items())
def flatten_dict(dictionary, parent_key='', separator='.'):
elements = []
for keys, values in dictionary.items():
new_key = parent_key + separator + keys if parent_key else keys
if isinstance(values, collections.MutableMapping):
elements.extend(flatten_dict(values, new_key, separator=separator).items())
else:
elements.append((new_key, values))
return dict(elements)
def unflatten_dict(dictionary):
unflatten_dictionary = {}
for keys, values in dictionary.items():
sub_dict = unflatten_dictionary
for sub_key in keys.split('.')[:-1]:
if sub_key not in sub_dict:
sub_dict[sub_key] = {}
sub_dict = sub_dict[sub_key]
sub_dict[keys.split('.')[-1]] = values
return unflatten_dictionary
def reps(elements):
sub_elements = []
for x in elements:
if elements.count(x) != 1:
sub_elements.append(x)
return tuple(sub_elements)

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

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

OK

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

Божидар обнови решението на 23.03.2015 13:00 (преди над 9 години)

+import collections
+
+
+def extract_type(elements, elements_type):
+ sub_elements = str()
+ for i in elements:
+ if type(i[0]) is elements_type:
+ sub_elements += str(i[0])*(i[1])
+ return sub_elements
+
+
+def reversed_dict(default):
+ return dict((value,key) for key,value in default.items())
+
+
+def flatten_dict(dictionary, parent_key='', separator='.'):
+ elements = []
+ for keys, values in dictionary.items():
+ new_key = parent_key + separator + keys if parent_key else keys
+ if isinstance(values, collections.MutableMapping):
+ elements.extend(flatten_dict(values, new_key, separator=separator).items())
+ else:
+ elements.append((new_key, values))
+ return dict(elements)
+
+
+def unflatten_dict(dictionary):
+ unflatten_dictionary = {}
+ for keys, values in dictionary.items():
+ sub_dict = unflatten_dictionary
+ for sub_key in keys.split('.')[:-1]:
+ if sub_key not in sub_dict:
+ sub_dict[sub_key] = {}
+ sub_dict = sub_dict[sub_key]
+ sub_dict[keys.split('.')[-1]] = values
+ return unflatten_dictionary
+
+
+def reps(elements):
+ sub_elements = []
+ for x in elements:
+ if elements.count(x) != 1:
+ sub_elements.append(x)
+ return tuple(sub_elements)