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

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

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

Резултати

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

Код

def extract_type(tupples, type_to_extract):
return_value = ""
for symbol, iterations in tupples:
if type(symbol) == type_to_extract:
return_value += str(symbol) * iterations
return return_value
def reversed_dict(hash_to_be_reversed):
return {key: value for value, key in hash_to_be_reversed.items()}
def reps(elements):
return tuple(x for x in elements if elements.count(x) > 1)
def flatten_dict(dictionary, level = ""):
result = {}
for key, value in dictionary.items():
current_key = level + key
if type(value) is dict:
result.update(flatten_dict(value, current_key + "."))
else:
result[current_key] = value
return result
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
if "." in key:
levels = key.split(".")
iter_dict = result
for level in levels[:-1]:
if level not in iter_dict:
iter_dict[level] = {}
iter_dict = iter_dict[level]
iter_dict[levels[-1]] = value
else:
result[key] = value
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Цветан обнови решението на 23.03.2015 15:01 (преди над 9 години)

+def extract_type(tupples, type_to_extract):
+ return_value = ""
+ for symbol, iterations in tupples:
+ if type(symbol) == type_to_extract:
+ return_value += str(symbol) * iterations
+ return return_value
+
+
+def reversed_dict(hash_to_be_reversed):
+ return {key: value for value, key in hash_to_be_reversed.items()}
+
+
+def reps(elements):
+ return tuple(x for x in elements if elements.count(x) > 1)
+
+
+def flatten_dict(dictionary, level = ""):
+ result = {}
+ for key, value in dictionary.items():
+ current_key = level + key
+ if type(value) is dict:
+ result.update(flatten_dict(value, current_key + "."))
+ else:
+ result[current_key] = value
+ return result
+
+def unflatten_dict(dictionary):
+ result = {}
+ for key, value in dictionary.items():
+ if "." in key:
+ levels = key.split(".")
+ iter_dict = result
+ for level in levels[:-1]:
+ if level not in iter_dict:
+ iter_dict[level] = {}
+ iter_dict = iter_dict[level]
+ iter_dict[levels[-1]] = value
+ else:
+ result[key] = value
+ return result

Цветан обнови решението на 23.03.2015 15:02 (преди над 9 години)

def extract_type(tupples, type_to_extract):
return_value = ""
for symbol, iterations in tupples:
if type(symbol) == type_to_extract:
return_value += str(symbol) * iterations
return return_value
def reversed_dict(hash_to_be_reversed):
return {key: value for value, key in hash_to_be_reversed.items()}
def reps(elements):
return tuple(x for x in elements if elements.count(x) > 1)
def flatten_dict(dictionary, level = ""):
result = {}
for key, value in dictionary.items():
current_key = level + key
if type(value) is dict:
result.update(flatten_dict(value, current_key + "."))
else:
result[current_key] = value
return result
+
def unflatten_dict(dictionary):
result = {}
for key, value in dictionary.items():
if "." in key:
levels = key.split(".")
iter_dict = result
for level in levels[:-1]:
if level not in iter_dict:
iter_dict[level] = {}
iter_dict = iter_dict[level]
iter_dict[levels[-1]] = value
else:
result[key] = value
return result