Решение на Пет малки функции от Минх Нгуен

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

Към профила на Минх Нгуен

Резултати

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

Код

def extract_type(archived_text, text_type):
a = str()
for text_descriptor in archived_text:
if type(text_descriptor[0]) is text_type:
a = a + str(text_descriptor[0]) * text_descriptor[1]
return a
def reversed_dict(countries_capitals):
capitals_countries = {}
for country, capital in countries_capitals.items():
capitals_countries[capital] = country
return capitals_countries
def flatten_dict(uncompressed_elements):
compressed_elements = {}
for key, value in uncompressed_elements.items():
if type(value) is dict:
for inner_key, inner_value in flatten_dict(value).items():
compressed_elements[key + '.' + inner_key] = inner_value
else:
compressed_elements[key] = value
return compressed_elements
def unflatten_dict(compressed_elements):
uncompressed_elements = {}
for key, value in compressed_elements.items():
if '.' not in key:
uncompressed_elements[key] = value
else:
keys = key.partition('.')
new_value = {}
new_value = unflatten_dict({keys[2]: value})
if keys[0] not in uncompressed_elements:
uncompressed_elements[keys[0]] = new_value
else:
inner_keys = keys[2].partition('.')
if inner_keys[0] not in uncompressed_elements[keys[0]]:
uncompressed_elements[keys[0]].update(new_value)
else:
uncompressed_elements[keys[0]][inner_keys[0]].update(
new_value[inner_keys[0]])
return uncompressed_elements
def reps(elements):
new_elements = []
for element in elements:
if elements.count(element) > 1:
new_elements.append(element)
return tuple(new_elements)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.131s

OK

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

Минх обнови решението на 22.03.2015 00:50 (преди над 9 години)

+def extract_type(archived_text, text_type):
+ a = str()
+ for text_descriptor in archived_text:
+ if type(text_descriptor[0]) is text_type:
+ a = a + str(text_descriptor[0]) * text_descriptor[1]
+ return a
+
+
+def reversed_dict(countries_capitals):
+ capitals_countries = {}
+ for country, capital in countries_capitals.items():
+ capitals_countries[capital] = country
+ return capitals_countries
+
+
+def flatten_dict(uncompressed_elements):
+ compressed_elements = {}
+
+ for key, value in uncompressed_elements.items():
+ if type(value) is dict:
+ for inner_key, inner_value in flatten_dict(value).items():
+ compressed_elements[key + '.' + inner_key] = inner_value
+ else:
+ compressed_elements[key] = value
+
+ return compressed_elements
+
+
+def unflatten_dict(compressed_elements):
+ uncompressed_elements = {}
+
+ for key, value in compressed_elements.items():
+ if '.' not in key:
+ uncompressed_elements[key] = value
+ else:
+ keys = key.partition('.')
+ new_value = {}
+ new_value = unflatten_dict({keys[2]: value})
+
+ if keys[0] not in uncompressed_elements:
+ uncompressed_elements[keys[0]] = new_value
+ else:
+ inner_keys = keys[2].partition('.')
+ if inner_keys[0] not in uncompressed_elements[keys[0]]:
+ uncompressed_elements[keys[0]].update(new_value)
+ else:
+ uncompressed_elements[keys[0]][inner_keys[0]].update(
+ new_value[inner_keys[0]])
+
+ return uncompressed_elements
+
+
+def reps(elements):
+ new_elements = []
+ for element in elements:
+ if elements.count(element) > 1:
+ new_elements.append(element)
+ return tuple(new_elements)