Решение на Пет малки функции от Александър Танков

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

Към профила на Александър Танков

Резултати

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

Код

def extract_type(from_tuples, type_for_result):
result = ''
for elem in from_tuples:
if type(elem[0]) == type_for_result:
for i in range(0, elem[1]):
result += str(elem[0])
return result
def reversed_dict(for_change):
result = {}
for key in for_change:
if for_change[key] not in result:
result[for_change[key]] = key
return result
def merge_two_dicts(left, right):
for elem in left:
if elem not in right:
right[elem] = left[elem]
def flatten_dict(for_change, moment=""):
result = {}
for key in for_change:
if type(for_change[key]) != dict:
new_key = moment + key
result[new_key] = for_change[key]
else:
temp = moment + key + '.'
merge_two_dicts(flatten_dict(for_change[key], temp), result)
return result
def get_all_before_point(for_change):
result = ""
for char in for_change:
for_change = for_change[1:]
if char == '.':
return (result, for_change)
else:
result += char
return (result, for_change)
def unflatten_dict(for_change, moment=""):
result = {}
for key in for_change:
if "." in key:
with_new_str = get_all_before_point(key)
moment = with_new_str[0]
new_key = with_new_str[1]
if moment not in result and "." not in moment:
result[moment] = {new_key: for_change[key]}
else:
result[moment][new_key] = for_change[key]
result[moment] = unflatten_dict(result[moment])
elif key in result:
temp = for_change[key]
result[key].update(temp)
else:
result[key] = for_change[key]
return result
def reps(for_change):
result = ()
for elem in for_change:
if for_change.count(elem) > 1:
result += (elem, )
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.136s

OK

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

Александър обнови решението на 19.03.2015 23:24 (преди над 9 години)

+def extract_type(from_tuples, type_for_result):
+ result = ''
+ for elem in from_tuples:
+ if type(elem[0]) == type_for_result:
+ for i in range(0, elem[1]):
+ result += str(elem[0])
+ return result
+
+
+def reversed_dict(for_change):
+ result = {}
+ for key in for_change:
+ if for_change[key] not in result:
+ result[for_change[key]] = key
+ return result
+
+
+def merge_two_dicts(left, right):
+ for elem in left:
+ if elem not in right:
+ right[elem] = left[elem]
+
+
+def flatten_dict(for_change, moment=""):
+ result = {}
+ for key in for_change:
+ if type(for_change[key]) != dict:
+ new_key = moment + key
+ result[new_key] = for_change[key]
+ else:
+ temp = moment + key + '.'
+ merge_two_dicts(flatten_dict(for_change[key], temp), result)
+ return result
+
+
+def get_all_before_point(for_change):
+ result = ""
+ for char in for_change:
+ for_change = for_change[1:]
+ if char == '.':
+ return (result, for_change)
+ else:
+ result += char
+ return (result, for_change)
+
+
+def unflatten_dict(for_change, moment=""):
+ result = {}
+ for key in for_change:
+ if "." in key:
+ with_new_str = get_all_before_point(key)
+ moment = with_new_str[0]
+ new_key = with_new_str[1]
+ if moment not in result and "." not in moment:
+ result[moment] = {new_key: for_change[key]}
+ else:
+ result[moment][new_key] = for_change[key]
+ result[moment] = unflatten_dict(result[moment])
+ elif key in result:
+ temp = for_change[key]
+ result[key].update(temp)
+ else:
+ result[key] = for_change[key]
+ return result
+
+
+def reps(for_change):
+ result = ()
+ for elem in for_change:
+ if for_change.count(elem) > 1:
+ result += (elem, )
+ return result