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

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

Към профила на Антоан Ангелов

Резултати

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

Код

from functools import reduce
def extract_type(archive, symbol_type):
return "".join([str(symbol) * count
for symbol, count in archive
if type(symbol) is symbol_type])
def reversed_dict(dictionary):
return {dictionary[key]: key for key in dictionary}
def flatten_dict(dictionary):
copy = dict(dictionary)
for key in dictionary:
if type(dictionary[key]) is dict:
flatten_dict_recursive(copy, copy[key], key)
del copy[key]
return copy
def flatten_dict_recursive(original, sub, global_key):
for key in sub:
if type(sub[key]) is dict:
flatten_dict_recursive(
original, sub[key],
global_key + "." + key)
else:
original[
global_key + "." + key] = sub[key]
def unflatten_dict(dictionary):
result = {}
for key in dictionary:
keys = key.split(".")
inner_most = reduce(get_dict_element, keys[:len(keys) - 1], result)
inner_most[keys[-1]] = dictionary[key]
return result
def get_dict_element(dictionary, key):
dictionary[key] = dictionary[key] if key in dictionary else {}
return dictionary[key]
def reps(list):
return tuple(item for item in list if list.count(item) > 1)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.347s

OK

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

Антоан обнови решението на 21.03.2015 00:51 (преди над 9 години)

+from functools import reduce
+
+
+def extract_type(archive, symbol_type):
+ return "".join([str(symbol) * count
+ for symbol, count in archive
+ if type(symbol) is symbol_type])
+
+
+def reversed_dict(dictionary):
+ return {dictionary[key]: key for key in dictionary}
+
+
+def flatten_dict(dictionary):
+
+ dict_copy = dict(dictionary)
+
+ for key in dictionary:
+ if type(dictionary[key]) is dict:
+ flatten_dict_recursive(dict_copy, dict_copy[key], key)
+ del dict_copy[key]
+ return dict_copy
+
+
+def flatten_dict_recursive(original_dictionary, sub_dictionary, recursion_key):
+ for key in sub_dictionary:
+ if type(sub_dictionary[key]) is dict:
+ flatten_dict_recursive(
+ original_dictionary, sub_dictionary[key],
+ recursion_key + "." + key)
+ else:
+ original_dictionary[
+ recursion_key + "." + key] = sub_dictionary[key]
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key in dictionary:
+ keys = key.split(".")
+ inner_most = reduce(get_dict_element, keys[:len(keys) - 1], result)
+ inner_most[keys[-1]] = dictionary[key]
+
+ return result
+
+
+def get_dict_element(dictionary, key):
+ dictionary[key] = dictionary[key] if key in dictionary else {}
+ return dictionary[key]
+
+
+def reps(list):
+ return tuple(item for item in list if list.count(item) > 1)

Антоан обнови решението на 21.03.2015 00:55 (преди над 9 години)

from functools import reduce
def extract_type(archive, symbol_type):
return "".join([str(symbol) * count
for symbol, count in archive
if type(symbol) is symbol_type])
def reversed_dict(dictionary):
return {dictionary[key]: key for key in dictionary}
def flatten_dict(dictionary):
-
- dict_copy = dict(dictionary)
-
+ copy = dict(dictionary)
for key in dictionary:
if type(dictionary[key]) is dict:
- flatten_dict_recursive(dict_copy, dict_copy[key], key)
- del dict_copy[key]
- return dict_copy
+ flatten_dict_recursive(copy, copy[key], key)
+ del copy[key]
+ return copy
-def flatten_dict_recursive(original_dictionary, sub_dictionary, recursion_key):
- for key in sub_dictionary:
- if type(sub_dictionary[key]) is dict:
+def flatten_dict_recursive(original, sub, global_key):
+ for key in sub:
+ if type(sub[key]) is dict:
flatten_dict_recursive(
- original_dictionary, sub_dictionary[key],
- recursion_key + "." + key)
+ original, sub[key],
+ global_key + "." + key)
else:
- original_dictionary[
- recursion_key + "." + key] = sub_dictionary[key]
+ original[
+ global_key + "." + key] = sub[key]
def unflatten_dict(dictionary):
result = {}
for key in dictionary:
keys = key.split(".")
inner_most = reduce(get_dict_element, keys[:len(keys) - 1], result)
inner_most[keys[-1]] = dictionary[key]
return result
def get_dict_element(dictionary, key):
dictionary[key] = dictionary[key] if key in dictionary else {}
return dictionary[key]
def reps(list):
- return tuple(item for item in list if list.count(item) > 1)
+ return tuple(item for item in list if list.count(item) > 1)