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

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

Към профила на Цвета Кандиларова

Резултати

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

Код

def extract_type(txt, txt_type):
return "".join([(str(x[0]) * x[1]) for x in txt if type(x[0]) == txt_type])
def reversed_dict(dictionary):
return {dictionary[key]: key for key in dictionary}
def flatten_dict(unflatten, p_key='', sep='.'):
result = []
for k, v in unflatten.items():
key = p_key + sep + k if p_key else k
if type(v) is dict:
result.extend(flatten_dict(v, key, sep=sep).items())
else:
result.append((key, v))
return dict(result)
def unflatten_dict(flatten):
result = dict()
for key, value in flatten.items():
key_parts = key.split(".")
new_dict = result
for k in key_parts[:-1]:
if k not in new_dict:
new_dict[k] = dict()
new_dict = new_dict[k]
new_dict[key_parts[-1]] = value
return result
def reps(lst):
return tuple([x for x in lst if lst.count(x) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.136s

OK

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

Цвета обнови решението на 19.03.2015 09:19 (преди над 9 години)

+def extract_type(txt, txt_type):
+ return "".join([(str(x[0]) * x[1]) for x in txt if type(x[0]) == txt_type])
+
+
+def reversed_dict(dictionary):
+ return {dictionary[key]: key for key in dictionary}
+
+
+def flatten_dict(dictionary, p_key='', sep='.'):
+ result = []
+
+ for k, v in dictionary.items():
+ key = p_key + sep + k if p_key else k
+ if type(v) is dict:
+ result.extend(flatten_dict(v, key, sep=sep).items())
+ else:
+ result.append((key, v))
+
+ return dict(result)
+
+
+def unflatten_dict(dictionary):
+ result = dict()
+
+ for key, value in dictionary.items():
+ key_parts = key.split(".")
+ new_dict = result
+
+ for k in key_parts[:-1]:
+ if k not in new_dict:
+ new_dict[k] = dict()
+ new_dict = new_dict[k]
+
+ new_dict[key_parts[-1]] = value
+
+ return result
+
+
+def reps(lst):
+ return tuple([x for x in lst if lst.count(x) > 1])

Цвета обнови решението на 21.03.2015 11:53 (преди над 9 години)

def extract_type(txt, txt_type):
return "".join([(str(x[0]) * x[1]) for x in txt if type(x[0]) == txt_type])
def reversed_dict(dictionary):
return {dictionary[key]: key for key in dictionary}
-def flatten_dict(dictionary, p_key='', sep='.'):
+def flatten_dict(unflatten, p_key='', sep='.'):
result = []
-
- for k, v in dictionary.items():
+ for k, v in unflatten.items():
key = p_key + sep + k if p_key else k
if type(v) is dict:
result.extend(flatten_dict(v, key, sep=sep).items())
else:
result.append((key, v))
-
return dict(result)
-def unflatten_dict(dictionary):
+def unflatten_dict(flatten):
result = dict()
-
- for key, value in dictionary.items():
+ for key, value in flatten.items():
key_parts = key.split(".")
new_dict = result
-
for k in key_parts[:-1]:
if k not in new_dict:
new_dict[k] = dict()
new_dict = new_dict[k]
-
new_dict[key_parts[-1]] = value
-
return result
def reps(lst):
return tuple([x for x in lst if lst.count(x) > 1])