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

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

Към профила на Елена Денева

Резултати

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

Код

from functools import reduce
from itertools import groupby
def extract_type(pairs, type_):
return ''.join([str(k) * v for k, v in pairs if type(k) is type_])
def reversed_dict(dictionary):
return {v: k for k, v in dictionary.items()}
def flatten_dict(value, keys=[]):
if (isinstance(value, dict)):
return reduce(
lambda x, y: dict(flatten_dict(y[1], keys+[y[0]]), **x),
value.items(),
{})
return {".".join(keys): value}
def unflatten_dict(flat_dict):
def unflatten(values, i=0):
dictionary = {keys[i]: v for keys, v in values if len(keys) == i + 1}
for key, group in groupby(values, lambda pair: pair[0][i]):
if not(key in dictionary):
dictionary[key] = unflatten(list(group), i+1)
return dictionary
return unflatten([(k.split("."), v) for k, v in sorted(flat_dict.items())])
def reps(values):
return tuple(filter(lambda x: values.count(x) > 1, values))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.137s

OK

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

Елена обнови решението на 18.03.2015 20:48 (преди около 9 години)

+from functools import reduce
+from collections import defaultdict
+
+
+def extract_type(compressed_text, type_):
+ def to_string(pair):
+ return str(pair[0]) * pair[1] if type(pair[0]) is type_ else ""
+ return ''.join(map(to_string, compressed_text))
+
+
+def reversed_dict(dictionary):
+ return {v: k for k, v in dictionary.items()}
+
+
+def flatten_dict(dictionary):
+ def flatten(value, keys=[]):
+ if isinstance(value, dict):
+ return sum([flatten(v, keys + [k]) for k, v in value.items()], [])
+ return [(".".join(keys), value)]
+
+ return dict(flatten(dictionary))
+
+
+def unflatten_dict(dictionary):
+ def merge(source, value):
+ merged = source
+ keys = value[0].split(".")
+ for key in keys[0:-1]:
+ if not(isinstance(merged.get(key, None), dict)):
+ merged[key] = {}
+ merged = merged[key]
+ merged[keys[-1]] = value[1]
+ return source
+
+ return reduce(merge, dictionary.items(), {})
+
+
+def reps(values):
+ return tuple(filter(lambda x: values.count(x) > 1, values))

Елена обнови решението на 19.03.2015 21:37 (преди около 9 години)

from functools import reduce
-from collections import defaultdict
+from itertools import groupby
-def extract_type(compressed_text, type_):
- def to_string(pair):
- return str(pair[0]) * pair[1] if type(pair[0]) is type_ else ""
- return ''.join(map(to_string, compressed_text))
+def extract_type(pairs, type_):
+ return ''.join([str(k) * v for k, v in pairs if type(k) is type_])
def reversed_dict(dictionary):
return {v: k for k, v in dictionary.items()}
-def flatten_dict(dictionary):
- def flatten(value, keys=[]):
- if isinstance(value, dict):
- return sum([flatten(v, keys + [k]) for k, v in value.items()], [])
- return [(".".join(keys), value)]
+def flatten_dict(value, keys=[]):
+ if (isinstance(value, dict)):
+ return reduce(
+ lambda x, y: dict(flatten_dict(y[1], keys+[y[0]]), **x),
+ value.items(),
+ {})
+ return {".".join(keys): value}
- return dict(flatten(dictionary))
+def unflatten_dict(flat_dict):
+ def unflatten(values, i=0):
+ dictionary = {keys[i]: v for keys, v in values if len(keys) == i + 1}
+ for key, group in groupby(values, lambda pair: pair[0][i]):
+ if not(key in dictionary):
+ dictionary[key] = unflatten(list(group), i+1)
+ return dictionary
-def unflatten_dict(dictionary):
- def merge(source, value):
- merged = source
- keys = value[0].split(".")
- for key in keys[0:-1]:
- if not(isinstance(merged.get(key, None), dict)):
- merged[key] = {}
- merged = merged[key]
- merged[keys[-1]] = value[1]
- return source
-
- return reduce(merge, dictionary.items(), {})
+ return unflatten([(k.split("."), v) for k, v in sorted(flat_dict.items())])
def reps(values):
return tuple(filter(lambda x: values.count(x) > 1, values))