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

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

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

Резултати

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

Код

def extract_type(list, type_of_element):
result = ''
for item in list:
if type(item[0]) is type_of_element:
for i in range(0, item[1]):
result += str(item[0])
return result
def reversed_dict(dictionary):
reversed_dictionary = {}
for key, value in dictionary.items():
reversed_dictionary[value] = key
return reversed_dictionary
def flatten_dict_helper(dictionary, flatten_dictionary):
for key, value in dictionary.items():
if type(value) is dict:
dict_inner={}
for key1, value1 in value.items():
dict_inner[key + '.' + key1] = value1
flatten_dict_helper(dict_inner, flatten_dictionary)
else:
flatten_dictionary[key] = value
return flatten_dictionary
def flatten_dict(dictionary):
return flatten_dict_helper(dictionary, {})
def unflatten_dict_helper(keys,value, index):
unflatten_dictionary = {}
if index == len(keys) - 1:
unflatten_dictionary[keys[index]] = value
else:
unflatten_dictionary[keys[index]] = unflatten_dict_helper(keys, value, index+1)
return unflatten_dictionary
def unflatten_dict(dictionary):
unflatten_dictionary = {}
for key, value in dictionary.items():
arr = key.split('.')
unflatten_dictionary.update(unflatten_dict_helper(arr, value, 0))
return unflatten_dictionary
def reps(list):
reps_list = []
i = 0
for item in list:
list.remove(item)
if item in list:
reps_list.append(item)
list.insert(i, item)
i += 1
return reps_list

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.167s

OK

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

Ивелина обнови решението на 23.03.2015 11:43 (преди над 9 години)

+def extract_type(list, type_of_element):
+ result = ''
+ for item in list:
+ if type(item[0]) is type_of_element:
+ for i in range(0, item[1]):
+ result += str(item[0])
+ return result
+
+
+def reversed_dict(dictionary):
+ reversed_dictionary = {}
+ for key, value in dictionary.items():
+ reversed_dictionary[value] = key
+ return reversed_dictionary
+
+
+def flatten_dict_helper(dictionary, flatten_dictionary):
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ dict_inner={}
+ for key1, value1 in value.items():
+ dict_inner[key + '.' + key1] = value1
+ flatten_dict_helper(dict_inner, flatten_dictionary)
+ else:
+ flatten_dictionary[key] = value
+ return flatten_dictionary
+
+
+def flatten_dict(dictionary):
+ return flatten_dict_helper(dictionary, {})
+
+
+def unflatten_dict_helper(keys,value, index):
+ unflatten_dictionary = {}
+ if index == len(keys) - 1:
+ unflatten_dictionary[keys[index]] = value
+ else:
+ unflatten_dictionary[keys[index]] = unflatten_dict_helper(keys, value, index+1)
+ return unflatten_dictionary
+
+
+def unflatten_dict(dictionary):
+ unflatten_dictionary = {}
+ for key, value in dictionary.items():
+ arr = key.split('.')
+ unflatten_dictionary.update(unflatten_dict_helper(arr, value, 0))
+ return unflatten_dictionary
+
+
+def reps(list):
+ reps_list = []
+ i = 0
+ for item in list:
+ list.remove(item)
+ if item in list:
+ reps_list.append(item)
+ list.insert(i, item)
+ i += 1
+ return reps_list