Решение на Пет малки функции от Иванна Димитрова

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

Към профила на Иванна Димитрова

Резултати

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

Код

def extract_type2(text, typ):
new_list_tuples = [tup for tup in text if isinstance(tup[0], str)]
new_string = []
for tup in new_list_tuples:
new_string.append(tup[1]*tup[0])
result = ''.join(new_string)
return result
def reversed_dict(my_dict):
new_dict = dict([(my_dict[key], key) for key in my_dict.keys()])
return new_dict
def flatten_dict(my_dict):
def flatten(my_dict, result={}, prv_keys=[]):
for k, v in zip(my_dict.keys(), my_dict.values()):
if isinstance(v, dict):
flatten(v, result, prv_keys + [k])
else:
result['.'.join(prv_keys + [k])] = v
return result
print (flatten(my_dict, result={}, prv_keys=[]))
def unflatten_dict(my_dict):
resultDict = dict()
for key, value in zip(my_dict.keys(), my_dict.values()):
parts = key.split(".")
d = resultDict
for part in parts[:-1]:
if part not in d:
d[part] = dict()
d = d[part]
d[parts[-1]] = value
print (resultDict)
def reps(my_list):
new_list = [x for x in my_list if my_list.count(x) > 1]
print (new_list)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.139s

OK

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

Иванна обнови решението на 23.03.2015 12:07 (преди над 9 години)

+def extract_type2(text, typ):
+ new_list_tuples = [tup for tup in text if isinstance(tup[0], str)]
+ new_string = []
+ for tup in new_list_tuples:
+ new_string.append(tup[1]*tup[0])
+ result = ''.join(new_string)
+ return result
+
+def reversed_dict(my_dict):
+ new_dict = dict([(my_dict[key], key) for key in my_dict.keys()])
+ return new_dict
+
+def flatten_dict(my_dict):
+ def flatten(my_dict, result={}, prv_keys=[]):
+
+ for k, v in zip(my_dict.keys(), my_dict.values()):
+ if isinstance(v, dict):
+ flatten(v, result, prv_keys + [k])
+ else:
+ result['.'.join(prv_keys + [k])] = v
+
+ return result
+ print (flatten(my_dict, result={}, prv_keys=[]))
+
+def unflatten_dict(my_dict):
+ resultDict = dict()
+ for key, value in zip(my_dict.keys(), my_dict.values()):
+ parts = key.split(".")
+ d = resultDict
+ for part in parts[:-1]:
+ if part not in d:
+ d[part] = dict()
+ d = d[part]
+ d[parts[-1]] = value
+ print (resultDict)
+
+def reps(my_list):
+ new_list = [x for x in my_list if my_list.count(x) > 1]
+ print (new_list)