Решение на Пет малки функции от Калоян Витанов

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

Към профила на Калоян Витанов

Резултати

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

Код

def extract_type(code, the_type):
decoded = ''
for i in range(len(code)):
if type(code[i][0]) is the_type:
times = code[i][1]
for j in range(times):
decoded = decoded + str(code[i][0])
return decoded
def reversed_dict(dictionary):
new_dictionary = {}
for key, value in dictionary.items():
new_dictionary[value] = key
return new_dictionary
def flatten_dict(dictionary):
rec_indicator = 0
new_dict = dict(dictionary)
for key, value in dictionary.items():
if type(value) is dict:
for key1, value1 in value.items():
new_dict[key + '.' + key1] = value1
del new_dict[key]
for value in new_dict.values():
if type(value) is dict:
rec_indicator = 5
break
if rec_indicator != 0:
new_dict = flatten_dict(new_dict)
return new_dict
def unflatten_dict(dictionary):
rec_indicator = 0
new_dict = dict(dictionary)
for key, value in dictionary.items():
variable = key.rsplit('.', maxsplit=1)
if (type(variable) is list) and (len(variable) != 1):
if type(new_dict.get(variable[0])) is dict:
(new_dict.get(variable[0])).update({variable[1]: value})
else:
new_dict[variable[0]] = {variable[1]: value}
del new_dict[key]
for key in new_dict.keys():
variable = key.rsplit('.', maxsplit=1)
if(type(variable) is list) and (len(variable) != 1):
rec_indicator = 5
break
if rec_indicator != 0:
new_dict = unflatten_dict(new_dict)
return new_dict
def reps(elements):
reduced_elements = ()
for i in range(len(elements)):
k = elements.count(elements[i])
if k != 1:
reduced_elements = reduced_elements + (elements[i],)
return reduced_elements

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.162s

OK

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

Калоян обнови решението на 22.03.2015 17:35 (преди над 9 години)

+def extract_type(code, the_type):
+ decoded = ''
+
+ for i in range(len(code)):
+ if type(code[i][0]) is the_type:
+ times = code[i][1]
+ for j in range(times):
+ decoded = decoded + str(code[i][0])
+
+ return decoded
+
+
+def reversed_dict(dictionary):
+ new_dictionary = {}
+
+ for key, value in dictionary.items():
+ new_dictionary[value] = key
+
+ return new_dictionary
+
+
+def flatten_dict(dictionary):
+ rec_indicator = 0
+ new_dict = dict(dictionary)
+
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ for key1, value1 in value.items():
+ new_dict[key + '.' + key1] = value1
+ del new_dict[key]
+
+ for value in new_dict.values():
+ if type(value) is dict:
+ rec_indicator = 5
+ break
+
+ if rec_indicator != 0:
+ new_dict = flatten_dict(new_dict)
+
+ return new_dict
+
+
+def unflatten_dict(dictionary):
+ rec_indicator = 0
+ new_dict = dict(dictionary)
+
+ for key, value in dictionary.items():
+ variable = key.rsplit('.', maxsplit=1)
+ if (type(variable) is list) and (len(variable) != 1):
+ if type(new_dict.get(variable[0])) is dict:
+ (new_dict.get(variable[0])).update({variable[1]: value})
+ else:
+ new_dict[variable[0]] = {variable[1]: value}
+ del new_dict[key]
+
+ for key in new_dict.keys():
+ variable = key.rsplit('.', maxsplit=1)
+ if(type(variable) is list) and (len(variable) != 1):
+ rec_indicator = 5
+ break
+
+ if rec_indicator != 0:
+ new_dict = unflatten_dict(new_dict)
+
+ return new_dict
+
+
+def reps(elements):
+ reduced_elements = ()
+
+ for i in range(len(elements)):
+ k = elements.count(elements[i])
+ if k != 1:
+ reduced_elements = reduced_elements + (elements[i],)
+
+ return reduced_elements