Решение на Пет малки функции от Васил Пачеджиев

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

Към профила на Васил Пачеджиев

Резултати

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

Код

def extract_type(couples, input_t):
result = str()
for couple in couples:
if type(couple[0]) is input_t:
result += couple[1] * str(couple[0])
return result
def reversed_dict(input_d):
res = dict()
for key, value in input_d.items():
res[value] = key
return res
def reps(elements):
return tuple([x for x in elements if elements.count(x) > 1])
def flatten_dict(input_d, left=''):
res = dict()
for right, value in input_d.items():
res_key = left + right
if type(value) is dict:
res.update(flatten_dict(value, res_key + '.'))
#flat = flatten_dict(value, res_key + '.')
# for k in flat.keys():
# res[k] = flat[k]
else:
res[res_key] = value
return res
def unflatten_dict(input_d):
returned = dict()
for key, value in input_d.items():
sub = returned
splited_keys = key.split('.')
for s_key in splited_keys[:-1]:
if s_key not in sub.keys():
sub[s_key] = dict()
sub = sub[s_key]
sub[splited_keys[-1]] = value
return returned

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Васил обнови решението на 20.03.2015 18:57 (преди над 9 години)

+def extract_type(couples, input_t):
+
+ result = str()
+ for couple in couples:
+ if type(couple[0]) is input_t:
+ result += couple[1] * str(couple[0])
+
+ return result
+
+
+def reversed_dict(input_d):
+
+ res = dict()
+ for key, value in input_d.items():
+ res[value] = key
+ return res
+
+
+def reps(elements):
+
+ return tuple([x for x in elements if elements.count(x) > 1])
+
+
+def flatten_dict(input_d, left=''):
+
+ res = dict()
+ for right, value in input_d.items():
+ res_key = left + right
+ if type(value) is dict:
+ res.update(flatten_dict(value, res_key + '.'))
+ #flat = flatten_dict(value, res_key + '.')
+ # for k in flat.keys():
+ # res[k] = flat[k]
+ else:
+ res[res_key] = value
+ return res
+
+
+def unflatten_dict(input_d):
+
+ returned = dict()
+ for key, value in input_d.items():
+ sub = returned
+ splited_keys = key.split('.')
+ for s_key in splited_keys[:-1]:
+ if s_key not in sub.keys():
+ sub[s_key] = dict()
+ sub = sub[s_key]
+ sub[splited_keys[-1]] = value
+
+ return returned