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

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

Към профила на Красимир Атанасов

Резултати

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

Код

def extract_type(symbol_reps, t):
return ''.join([str(x[0]) * x[1] for x in symbol_reps if type(x[0]) is t])
def reversed_dict(regular):
return {regular[k]: k for k in regular}
def reps(l):
return tuple([x for i, x in enumerate(l) if x in l[:i] + l[i + 1:]])
def flatten_dict(nested):
return flatten_dict_internal(nested)
def flatten_dict_internal(unflatten, nested_key=''):
flatten = {}
for k, v in unflatten.items():
key = nested_key + k
if type(v) is dict:
flatten.update(flatten_dict_internal(v, key + '.'))
else:
flatten[key] = v
return flatten
def unflatten_dict(flatten):
unflatten = {}
for k, v in flatten.items():
keys = k.split('.')
current = unflatten
for key in keys[:-1]:
if key not in current:
current[key] = {}
current = current[key]
current[keys[-1]] = v
return unflatten

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.138s

OK

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

Красимир обнови решението на 23.03.2015 16:33 (преди над 9 години)

+def extract_type(symbol_reps, t):
+ return ''.join([str(x[0]) * x[1] for x in symbol_reps if type(x[0]) is t])
+
+
+def reversed_dict(regular):
+ return {regular[k]: k for k in regular}
+
+
+def reps(l):
+ return tuple([x for i, x in enumerate(l) if x in l[:i] + l[i + 1:]])
+
+
+def flatten_dict(nested):
+ return flatten_dict_internal(nested)
+
+
+def flatten_dict_internal(unflatten, nested_key=''):
+ flatten = {}
+ for k, v in unflatten.items():
+ key = nested_key + k
+ if type(v) is dict:
+ flatten.update(flatten_dict_internal(v, key + '.'))
+ else:
+ flatten[key] = v
+
+ return flatten
+
+
+def unflatten_dict(flatten):
+ unflatten = {}
+ for k, v in flatten.items():
+ keys = k.split('.')
+ current = unflatten
+ for key in keys[:-1]:
+ if key not in current:
+ current[key] = {}
+ current = current[key]
+ current[keys[-1]] = v
+ return unflatten