Решение на Пет малки функции от Спас Методиев

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

Към профила на Спас Методиев

Резултати

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

Код

def extract_type(archived_text, type_name):
output = ""
for symbol, number in archived_text:
if type_name == type(symbol):
while number != 0:
output += str(symbol)
number -= 1
return output
def reversed_dict(country_capital):
capital_country = {}
for country, capital in country_capital.items():
capital_country[capital] = country
return capital_country
def flatten_dict(input_data):
output = {}
for k, v in input_data.items():
if type(v) != dict:
output[k] = v
else:
rec_output = flatten_dict(v)
for k_ in rec_output:
output[k + '.' + k_] = rec_output[k_]
return output
def unflatten_dict(input_data):
output = {}
for k, v in input_data.items():
if '.' not in k:
output[k] = v
else:
separate_keys = k.split('.')
first_key_length = len(separate_keys[0])
next_level = {i[first_key_length + 1:]: input_data[i]
for i in input_data
if i[:first_key_length] == k[:first_key_length]}
output[k[:first_key_length]] = unflatten_dict(next_level)
return output
def reps(input_data):
output = []
for i in input_data:
if input_data.count(i) > 1:
output.append(i)
return tuple(output)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.174s

OK

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

Спас обнови решението на 22.03.2015 18:22 (преди над 9 години)

+def extract_type(archived_text, type_name):
+ output = ""
+ for symbol, number in archived_text:
+ if type_name == type(symbol):
+ while number != 0:
+ output += str(symbol)
+ number -= 1
+ return output
+
+
+def reversed_dict(country_capital):
+ capital_country = {}
+ for country, capital in country_capital.items():
+ capital_country[capital] = country
+ return capital_country
+
+
+def flatten_dict(input_data):
+ output = {}
+ for k, v in input_data.items():
+ if type(v) != dict:
+ output[k] = v
+ else:
+ rec_output = flatten_dict(v)
+ for k_ in rec_output:
+ output[k + '.' + k_] = rec_output[k_]
+ return output
+
+
+def unflatten_dict(input_data):
+ output = {}
+ for k, v in input_data.items():
+ if '.' not in k:
+ output[k] = v
+ else:
+ separate_keys = k.split('.')
+ first_key_length = len(separate_keys[0])
+ next_level = {i[first_key_length + 1:]: input_data[i]
+ for i in input_data
+ if i[:first_key_length] == k[:first_key_length]}
+ output[k[:first_key_length]] = unflatten_dict(next_level)
+ return output
+
+
+def reps(input_data):
+ output = []
+ for i in input_data:
+ if input_data.count(i) > 1:
+ output.append(i)
+ return tuple(output)