Решение на Пет малки функции от Мартин Филипов

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

Към профила на Мартин Филипов

Резултати

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

Код

def extract_type(symbols_reps_pair, wanted_type):
result = []
for (symbols, reps) in symbols_reps_pair:
if type(symbols) is wanted_type:
for _ in range(reps):
result.append(str(symbols))
return "".join(result)
def reversed_dict(dictionary):
return {value: key for key, value in dictionary.items()}
def reps(collection):
return tuple(element for element in collection
if collection.count(element) > 1)
def flatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
if type(value) is dict:
for sub_key, sub_value in flatten_dict(value).items():
result[key + "." + sub_key] = sub_value
else:
result[key] = value
return result
def get_all_elements_with_root(dictionary, root):
result = {}
for key, value in dictionary.items():
splitted_root_from_key = key.split(".", 1)
if splitted_root_from_key[0] == root:
result[splitted_root_from_key[1]] = value
return result
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
if "." in key:
splitted_root_from_key = key.split(".", 1)
root = splitted_root_from_key[0]
if root not in result:
result[root] = get_all_elements_with_root(dictionary, root)
result[root] = unflatten_dict(result[root])
else:
result[key] = value
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.132s

OK

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

Мартин обнови решението на 23.03.2015 01:07 (преди около 9 години)

+def extract_type(symbols_reps_pair, wanted_type):
+ result = []
+ for (symbols, reps) in symbols_reps_pair:
+ if type(symbols) is wanted_type:
+ for _ in range(reps):
+ result.append(str(symbols))
+ return "".join(result)
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def reps(collection):
+ return tuple(element for element in collection
+ if collection.count(element) > 1)
+
+
+def flatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ for sub_key, sub_value in flatten_dict(value).items():
+ result[key + "." + sub_key] = sub_value
+ else:
+ result[key] = value
+ return result
+
+
+def get_all_elements_with_root(dictionary, root):
+ result = {}
+ for key, value in dictionary.items():
+ splitted_root_from_key = key.split(".", 1)
+ if splitted_root_from_key[0] == root:
+ result[splitted_root_from_key[1]] = value
+ return result
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if "." in key:
+ splitted_root_from_key = key.split(".", 1)
+ root = splitted_root_from_key[0]
+ if root not in result:
+ result[root] = get_all_elements_with_root(dictionary, root)
+ result[root] = unflatten_dict(result[root])
+ else:
+ result[key] = value
+ return result