Решение на Пет малки функции от Любослав Кънев

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

Към профила на Любослав Кънев

Резултати

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

Код

def extract_type(symbols, symbol_type):
result = ""
for symbol in symbols:
if type(symbol[0]) is symbol_type:
for _ in range(symbol[1]):
result += str(symbol[0])
return result
def reversed_dict(original):
result = {}
for key, value in original.items():
result[value] = key
return result
def reps(original):
return tuple(filter(lambda x: original.count(x) > 1, original))
def flatten_dict(original):
result = {}
for key in original.keys():
if type(original[key]) is dict:
for key2, value2 in flatten_dict(original[key]).items():
result[key + '.' + key2] = value2
else:
result[key] = original[key]
return result
def smart_update(update_to, update_from):
for key in update_from.keys():
if key in update_to:
if type(update_from[key]) is dict:
smart_update(update_to[key], update_from[key])
else:
update_to[key].update(update_from[key])
else:
update_to[key] = update_from[key]
def unflatten_dict(original):
result = {}
for key in original.keys():
if "." in key:
temp = {key[key.index(".") + 1:]: original[key]}
new_key = key[:key.index(".")]
if new_key not in result:
result[new_key] = {}
smart_update(result[new_key], unflatten_dict(temp))
else:
result[key] = original[key]
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.133s

OK

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

Любослав обнови решението на 22.03.2015 23:21 (преди над 9 години)

+def extract_type(symbols, symbol_type):
+ result = ""
+
+ for symbol in symbols:
+ if type(symbol[0]) is symbol_type:
+ for _ in range(symbol[1]):
+ result += str(symbol[0])
+
+ return result
+
+
+def reversed_dict(original):
+ result = {}
+
+ for key, value in original.items():
+ result[value] = key
+
+ return result
+
+
+def reps(original):
+ return tuple(filter(lambda x: original.count(x) > 1, original))
+
+
+def flatten_dict(original):
+ result = {}
+
+ for key in original.keys():
+ if type(original[key]) is dict:
+ for key2, value2 in flatten_dict(original[key]).items():
+ result[key + '.' + key2] = value2
+ else:
+ result[key] = original[key]
+
+ return result
+
+
+def smart_update(update_to, update_from):
+ for key in update_from.keys():
+ if key in update_to:
+ if type(update_from[key]) is dict:
+ smart_update(update_to[key], update_from[key])
+ else:
+ update_to[key].update(update_from[key])
+ else:
+ update_to[key] = update_from[key]
+
+
+def unflatten_dict(original):
+ result = {}
+
+ for key in original.keys():
+ if "." in key:
+ temp = {key[key.index(".") + 1:]: original[key]}
+ new_key = key[:key.index(".")]
+
+ if new_key not in result:
+ result[new_key] = {}
+
+ smart_update(result[new_key], unflatten_dict(temp))
+ else:
+ result[key] = original[key]
+
+ return result