Решение на Пет малки функции от Христиан Димитров

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

Към профила на Христиан Димитров

Резултати

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

Код

def reps(array):
new_array = list(array)
return tuple(x for x in new_array if new_array.count(x) != 1)
def reversed_dict(key_value_sequence):
return {key_value_sequence[k]: k for k in key_value_sequence}
def extract_type(array, some_type):
string = ""
for item in array:
if type(item[0]) is some_type:
string += "".join([str(item[0]) for i in range(item[1])])
return string
def create_flatten_dict(flatten):
for key, value in flatten.items():
if isinstance(value, dict):
flatten = create_flatten_dict(value)
for new_key, new_value in flatten:
yield (key + "." + new_key, new_value)
else:
yield (key, value)
def flatten_dict(flatten):
return dict(create_flatten_dict(flatten))
def unflatten_dict(unflatten):
flatten = dict()
inner_flatten = dict()
for key, value in unflatten.items():
if "." not in key:
flatten[key] = value
else:
sub_key_values = key.split(".")
inner_flatten = flatten
for item in sub_key_values[0:len(sub_key_values) - 1]:
if item not in inner_flatten:
inner_flatten[item] = dict()
inner_flatten = inner_flatten[item]
inner_flatten[sub_key_values[len(sub_key_values) - 1]] = value
return flatten

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.131s

OK

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

Христиан обнови решението на 21.03.2015 02:49 (преди над 9 години)

+def reps(array):
+ new_array = list(array)
+ return tuple(x for x in new_array if new_array.count(x) != 1)
+
+
+def reversed_dict(key_value_sequence):
+ return {key_value_sequence[k]: k for k in key_value_sequence}
+
+
+def extract_type(array, some_type):
+ string = ""
+ for item in array:
+ if type(item[0]) is some_type:
+ string += "".join([str(item[0]) for i in range(item[1])])
+ return string
+
+
+def create_flatten_dict(flatten):
+ for key, value in flatten.items():
+ if isinstance(value, dict):
+ flatten = create_flatten_dict(value)
+ for new_key, new_value in flatten:
+ yield (key + "." + new_key, new_value)
+ else:
+ yield (key, value)
+
+
+def flatten_dict(flatten):
+ return dict(create_flatten_dict(flatten))
+
+
+def unflatten_dict(unflatten):
+ flatten = dict()
+ inner_flatten = dict()
+ for key, value in unflatten.items():
+ if "." not in key:
+ flatten[key] = value
+ else:
+ sub_key_values = key.split(".")
+ inner_flatten = flatten
+ for item in sub_key_values[0:len(sub_key_values) - 1]:
+ if item not in inner_flatten:
+ inner_flatten[item] = dict()
+ inner_flatten = inner_flatten[item]
+ inner_flatten[sub_key_values[len(sub_key_values) - 1]] = value
+ return flatten