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

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

Към профила на Ивайло Цанков

Резултати

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

Код

def extract_type(zipped_symbols, extract_type):
extracted = ""
for zipped_info in zipped_symbols:
if type(zipped_info[0]) is extract_type:
extracted += str(zipped_info[0]) * zipped_info[1]
return extracted
def reversed_dict(dictionary):
return {value: key for key, value in dictionary.items()}
def flatten_dict_rec(dictionary, flatten, parrent):
for key, value in dictionary.items():
if type(value) is dict:
flatten_dict_rec(value, flatten, parrent + key + ".")
else:
flatten[parrent + key] = value
def flatten_dict(dictionary):
flatten = {}
flatten_dict_rec(dictionary, flatten, "")
return flatten
def unflatten_dict(dictionary):
unflatten = {}
for key, value in dictionary.items():
key_parts = key.split(".")
# this loop starts with the root dictionary
d = unflatten
# add new dictionary and go deep in child
# dict if needed
for part in key_parts[:-1]:
if part not in d:
d[part] = dict()
d = d[part]
d[key_parts[-1]] = value
return unflatten
def reps(sequence):
return tuple(
[element for element in sequence if sequence.count(element) > 1]
)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.146s

OK

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

Ивайло обнови решението на 23.03.2015 12:27 (преди над 9 години)

+def extract_type(zipped_symbols, extract_type):
+ extracted = ""
+
+ for zipped_info in zipped_symbols:
+ if type(zipped_info[0]) is extract_type:
+ extracted += str(zipped_info[0]) * zipped_info[1]
+
+ return extracted
+
+
+def reversed_dict(dictionary):
+ return {value: key for key, value in dictionary.items()}
+
+
+def flatten_dict_rec(dictionary, flatten, parrent):
+ for key, value in dictionary.items():
+ if type(value) is dict:
+ flatten_dict_rec(value, flatten, parrent + key + ".")
+ else:
+ flatten[parrent + key] = value
+
+
+def flatten_dict(dictionary):
+ flatten = {}
+ flatten_dict_rec(dictionary, flatten, "")
+ return flatten
+
+
+def unflatten_dict(dictionary):
+ unflatten = {}
+
+ for key, value in dictionary.items():
+ key_parts = key.split(".")
+
+ # this loop starts with the root dictionary
+ d = unflatten
+
+ # add new dictionary and go deep in child
+ # dict if needed
+ for part in key_parts[:-1]:
+ if part not in d:
+ d[part] = dict()
+ d = d[part]
+
+ d[key_parts[-1]] = value
+
+ return unflatten
+
+
+def reps(sequence):
+ return tuple(
+ [element for element in sequence if sequence.count(element) > 1]
+ )