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

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

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

Резултати

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

Код

def extract_type(elements, type_arg):
string = ''
for element in elements:
if type(element[0]) == type_arg:
string += str(element[0]) * element[1]
return string
def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def flatten_dict(dictionary):
flatt_dict = {}
def make_flatten_dict(dictionary):
for key, value in dictionary.items():
if type(value) is dict:
make_flatten_dict(dict([(key + '.' + k, v)
for k, v in value.items()]))
else:
flatt_dict.update({key: value})
return flatt_dict
return make_flatten_dict(dictionary)
def unflatten_dict(dictionary):
unflatten_dictionary = {}
for key, value in dictionary.items():
split_keys = key.split(".")
temporary_dictionary = unflatten_dictionary
for element in split_keys[:-1]:
if element not in temporary_dictionary:
temporary_dictionary[element] = {}
temporary_dictionary = temporary_dictionary[element]
temporary_dictionary[split_keys[-1]] = value
return unflatten_dictionary
def reps(items):
return tuple(filter(lambda x: items.count(x) >= 2, items))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.169s

OK

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

Христо обнови решението на 23.03.2015 15:47 (преди над 9 години)

+def extract_type(arg, type_arg):
+ string = ''
+ for element in arg:
+ if type(element[0]) == type_arg:
+ string += str(element[0]) * element[1]
+ return string
+
+
+def reversed_dict(dictionary):
+ return dict(zip(dictionary.values(), dictionary.keys()))
+
+
+def flatten_dict(dictionary):
+ flatt_dict = {}
+
+ def make_flatten_dict(dictionary):
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ make_flatten_dict(dict([(key + '.' + k, v)
+ for k, v in value.items()]))
+ else:
+ flatt_dict.update({key: value})
+ return flatt_dict
+ return make_flatten_dict(dictionary)
+
+
+def unflatten_dict(dictionary):
+ unflatten_dictionary = {}
+ for key, value in dictionary.items():
+ split_keys = key.split(".")
+ temporary_dictionary = unflatten_dictionary
+ for element in split_keys[:-1]:
+ if element not in temporary_dictionary:
+ temporary_dictionary[element] = {}
+ temporary_dictionary = temporary_dictionary[element]
+ temporary_dictionary[split_keys[-1]] = value
+ return unflatten_dictionary
+
+
+def reps(items):
+ return tuple(filter(lambda x: items.count(x) >= 2, items))

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

-def extract_type(arg, type_arg):
+def extract_type(elements, type_arg):
string = ''
- for element in arg:
+ for element in elements:
if type(element[0]) == type_arg:
string += str(element[0]) * element[1]
return string
def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def flatten_dict(dictionary):
flatt_dict = {}
def make_flatten_dict(dictionary):
for key, value in dictionary.items():
if type(value) is dict:
make_flatten_dict(dict([(key + '.' + k, v)
for k, v in value.items()]))
else:
flatt_dict.update({key: value})
return flatt_dict
return make_flatten_dict(dictionary)
def unflatten_dict(dictionary):
unflatten_dictionary = {}
for key, value in dictionary.items():
split_keys = key.split(".")
temporary_dictionary = unflatten_dictionary
for element in split_keys[:-1]:
if element not in temporary_dictionary:
temporary_dictionary[element] = {}
temporary_dictionary = temporary_dictionary[element]
temporary_dictionary[split_keys[-1]] = value
return unflatten_dictionary
def reps(items):
return tuple(filter(lambda x: items.count(x) >= 2, items))