Решение на Пет малки функции от Станислав Гатев

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

Към профила на Станислав Гатев

Резултати

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

Код

from collections import Counter
def extract_type(encoded, allowed_type):
return ''.join(str(x) * y for x, y in encoded if type(x) == allowed_type)
def reversed_dict(dictionary):
return {value: key for key, value in dictionary.items()}
def prefix_keys(dictionary, prefix):
return {prefix + "." + key: value for key, value in dictionary.items()}
def flatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
if isinstance(value, dict):
result.update(prefix_keys(flatten_dict(value), key))
else:
result[key] = value
return result
def add_nested(dictionary, key_components, value):
nested_dictionary = dictionary
for component in key_components[:-1]:
if component not in nested_dictionary:
nested_dictionary[component] = {}
nested_dictionary = nested_dictionary[component]
nested_dictionary[key_components[-1]] = value
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
add_nested(result, key.split('.'), value)
return result
def reps(items):
counts = Counter(items)
return tuple(item for item in items if counts[item] > 1)

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

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

OK

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

Станислав обнови решението на 18.03.2015 00:25 (преди над 9 години)

+from collections import defaultdict, Counter
+
+
+def extract_type(encoded, kind):
+ return ''.join(str(x) * y for x, y in encoded if isinstance(x, kind))
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def prefix_keys(dictionary, prefix):
+ return {prefix + "." + key: value for key, value in dictionary.items()}
+
+
+def flatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if isinstance(value, dict):
+ result.update(prefix_keys(flatten_dict(value), key))
+ else:
+ result[key] = value
+ return result
+
+
+def add_nested(dictionary, key_components, value):
+ nested_dictionary = dictionary
+ for component in key_components[:-1]:
+ if component not in nested_dictionary:
+ nested_dictionary[component] = {}
+ nested_dictionary = nested_dictionary[component]
+ nested_dictionary[key_components[-1]] = value
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ add_nested(result, key.split('.'), value)
+ return result
+
+
+def reps(items):
+ counts = Counter(items)
+ return tuple(item for item in items if counts[item] > 1)

Станислав обнови решението на 23.03.2015 15:29 (преди над 9 години)

-from collections import defaultdict, Counter
+from collections import Counter
-def extract_type(encoded, kind):
- return ''.join(str(x) * y for x, y in encoded if isinstance(x, kind))
+def extract_type(encoded, allowed_type):
+ return ''.join(str(x) * y for x, y in encoded if type(x) == allowed_type)
def reversed_dict(dictionary):
return {value: key for key, value in dictionary.items()}
def prefix_keys(dictionary, prefix):
return {prefix + "." + key: value for key, value in dictionary.items()}
def flatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
if isinstance(value, dict):
result.update(prefix_keys(flatten_dict(value), key))
else:
result[key] = value
return result
def add_nested(dictionary, key_components, value):
nested_dictionary = dictionary
for component in key_components[:-1]:
if component not in nested_dictionary:
nested_dictionary[component] = {}
nested_dictionary = nested_dictionary[component]
nested_dictionary[key_components[-1]] = value
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
add_nested(result, key.split('.'), value)
return result
def reps(items):
counts = Counter(items)
return tuple(item for item in items if counts[item] > 1)