Решение на Пет малки функции от Тачо Желев

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

Към профила на Тачо Желев

Резултати

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

Код

def extract_type(mylist, mytype):
result = ""
for pair in mylist:
if type(pair[0]) is mytype:
result += str(pair[0]) * pair[1]
return result
def reversed_dict(mydict):
return {value: key for key, value in mydict.items()}
def flatten_dict(mydict):
result = {}
fragments = []
def helper_flatten(mydict, fragments):
for key, value in mydict.items():
if isinstance(value, dict):
helper_flatten(value, fragments + [key])
else:
newkey = '.'.join(fragments + [key])
result[newkey] = value
helper_flatten(mydict, fragments)
return result
def unflatten_dict(mydict):
result = {}
for key, value in mydict.items():
fragments = key.split('.')
context = result
for fragment in fragments[:-1]:
if fragment not in context:
context[fragment] = {}
context = context[fragment]
context[fragments[-1]] = value
return result
def reps(mylist):
return tuple([n for n in mylist if(mylist.count(n) > 1)])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.132s

OK

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

Тачо обнови решението на 21.03.2015 16:32 (преди над 9 години)

+def extract_type(mylist, mytype):
+ result = ""
+ for pair in mylist:
+ if type(pair[0]) is mytype:
+ result += str(pair[0]) * pair[1]
+ return result
+
+
+def reversed_dict(mydict):
+ return {value: key for key, value in mydict.items()}
+
+
+def flatten_dict(mydict):
+ result = {}
+ fragments = []
+
+ def helper_flatten(mydict, fragments):
+ for key, value in mydict.items():
+ if isinstance(value, dict):
+ helper_flatten(value, fragments + [key])
+ else:
+ newkey = '.'.join(fragments + [key])
+ result[newkey] = value
+ helper_flatten(mydict, fragments)
+ return result
+
+
+def unflatten_dict(mydict):
+ result = {}
+ for key, value in mydict.items():
+ fragments = key.split('.')
+ context = result
+ for fragment in fragments[:-1]:
+ if fragment not in context:
+ context[fragment] = {}
+ context = context[fragment]
+ context[fragments[-1]] = value
+ return result
+
+
+def reps(mylist):
+ return tuple([n for n in mylist if(mylist.count(n) > 1)])