Решение на Пет малки функции от Божидар Григоров

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

Към профила на Божидар Григоров

Резултати

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

Код

def unflatten(dictionary):
resultDict = dict()
for key, value in dictionary.items():
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
return resultDict
def extract_type(collection, kind):
return (''.join([str(elem[0]) * elem[1]
for elem in collection if type(elem[0]) is kind]))
def reversed_dict(collection):
return {collection[key]: key for key in collection}
def reps(collection):
return (tuple(filter(lambda x: collection.count(x) > 1, collection)))
def flatten_dict(collection, prefix=''):
return {prefix + '.' + k if prefix else k: v
for kk, vv in collection.items()
for k, v in flatten_dict(vv, kk).items()
} if isinstance(collection, dict) else {prefix: collection}

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.161s

OK

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

Божидар обнови решението на 23.03.2015 11:19 (преди над 9 години)

+def unflatten(dictionary):
+ resultDict = dict()
+ for key, value in dictionary.items():
+ 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
+ return resultDict
+
+
+def extract_type(collection, typ):
+ return (''.join([str(elem[0]) * elem[1]
+ for elem in collection if type(elem[0]) is typ]))
+
+
+def reversed_dict(collection):
+ return {collection[key]: key for key in collection}
+
+
+def reps(collection):
+ return (tuple(filter(lambda x: collection.count(x) > 1, collection)))
+
+
+def flatten_dict(collection, prefix=''):
+ return {prefix + '.' + k if prefix else k: v
+ for kk, vv in collection.items()
+ for k, v in flatten_dict(vv, kk).items()
+ } if isinstance(collection, dict) else {prefix: collection}

Божидар обнови решението на 23.03.2015 15:08 (преди над 9 години)

def unflatten(dictionary):
resultDict = dict()
for key, value in dictionary.items():
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
return resultDict
-def extract_type(collection, typ):
+def extract_type(collection, kind):
return (''.join([str(elem[0]) * elem[1]
- for elem in collection if type(elem[0]) is typ]))
+ for elem in collection if type(elem[0]) is kind]))
def reversed_dict(collection):
return {collection[key]: key for key in collection}
def reps(collection):
return (tuple(filter(lambda x: collection.count(x) > 1, collection)))
def flatten_dict(collection, prefix=''):
return {prefix + '.' + k if prefix else k: v
for kk, vv in collection.items()
for k, v in flatten_dict(vv, kk).items()
} if isinstance(collection, dict) else {prefix: collection}