Решение на Пет малки функции от Емине Башева

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

Към профила на Емине Башева

Резултати

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

Код

def extract_type(arch_text, text_type):
return "".join([str(elem[0]) * elem[1]
for elem in arch_text if type(elem[0]) is text_type])
def reversed_dict(to_be_reverse):
return {to_be_reverse[key]: key for key in to_be_reverse}
def has_value_dict(elem):
return any(type(elem[key]) is dict for key in elem)
def flatten_dict(unflatten):
result = {}
for key in unflatten:
value = unflatten[key]
if type(value) is dict:
for new_key in value:
result[key + "." + new_key] = value[new_key]
else:
result[key] = value
if has_value_dict(result):
return flatten_dict(result)
return result
def unflatten_dict(flatten):
result = {}
for key in flatten:
if '.' in key:
inversed_key = key[::-1]
junior_key = inversed_key[:key.index('.')]
senior_key = inversed_key[key.index('.') + 1:][::-1]
if senior_key in result:
result[senior_key][junior_key] = flatten[key]
else:
result[senior_key] = {junior_key: flatten[key]}
else:
if key in result:
result[key].update(result[key])
else:
result[key] = flatten[key]
if any('.' in key for key in result):
return unflatten_dict(result)
return result
def reps(elements):
return tuple([x for x in elements if elements.count(x) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.166s

OK

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

Емине обнови решението на 22.03.2015 01:58 (преди над 9 години)

+def extract_type(arch_text, text_type):
+ return "".join([str(elem[0]) * elem[1]
+ for elem in arch_text if type(elem[0]) is text_type])
+
+
+def reversed_dict(to_be_reverse):
+ return {to_be_reverse[key]: key for key in to_be_reverse}
+
+
+def has_value_dict(elem):
+ return any(type(elem[key]) is dict for key in elem)
+
+
+def flatten_dict(unflatten):
+ result = {}
+ for key in unflatten:
+ value = unflatten[key]
+ if type(value) is dict:
+ for new_key in value:
+ result[key + "." + new_key] = value[new_key]
+ else:
+ result[key] = value
+
+ if has_value_dict(result):
+ return flatten_dict(result)
+
+ return result
+
+
+def unflatten_dict(flatten):
+ result = {}
+
+ for key in flatten:
+ if '.' in key:
+ inversed_key = key[::-1]
+ junior_key = inversed_key[:key.index('.')]
+ senior_key = inversed_key[key.index('.') + 1:][::-1]
+
+ if senior_key in result:
+ result[senior_key][junior_key] = flatten[key]
+ else:
+ result[senior_key] = {junior_key: flatten[key]}
+ else:
+ if key in result:
+ result[key].update(result[key])
+ else:
+ result[key] = flatten[key]
+
+ if any('.' in key for key in result):
+ return unflatten_dict(result)
+
+ return result
+
+
+def reps(elements):
+ return tuple([x for x in elements if elements.count(x) > 1])