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

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

Към профила на Цветан Мадански

Резултати

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

Код

def extract_type(text, result_type):
result_string = ""
for element in text:
if type(element[0]) is result_type:
result_string += str(element[0]) * element[1]
return result_string
def reversed_dict(elements):
keys = []
new_items = []
items = list(elements.items())
for item in items:
if not item[1] in keys:
keys.append(item[1])
new_items.append((item[1], item[0]))
return dict(new_items)
def flatten_dict_help(elements, flatten_elements, previous_key=''):
for key, value in elements.items():
if previous_key:
new_key = previous_key + '.' + key
else:
new_key = key
if type(value) is not dict:
flatten_elements.append((new_key, value))
else:
flatten_dict_help(value, flatten_elements, new_key)
return dict(flatten_elements)
def flatten_dict(elements):
flatten_elements = []
return flatten_dict_help(elements, flatten_elements)
def unflatten_dict(elements):
unflatten_elements = dict()
for key, value in elements.items():
current_elements = unflatten_elements
if len(key) > 1:
keys = key.split('.')
while len(keys) > 1:
if keys[0] not in current_elements:
current_elements[keys[0]] = dict()
current_elements = current_elements[keys[0]]
keys.pop(0)
current_elements[keys[0]] = value
else:
current_elements[key] = value
return unflatten_elements
def counter_of_element(element, elements):
return len([x for x in elements if x == element])
def reps(elements):
return tuple([element for element in elements
if counter_of_element(element, elements) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.132s

OK

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

Цветан обнови решението на 21.03.2015 21:17 (преди над 9 години)

+def extract_type(text, result_type):
+ result_string = ""
+ for element in text:
+ if type(element[0]) is result_type:
+ result_string += str(element[0]) * element[1]
+ return result_string
+
+
+def reversed_dict(elements):
+ keys = []
+ new_items = []
+ items = list(elements.items())
+ for item in items:
+ if not item[1] in keys:
+ keys.append(item[1])
+ new_items.append((item[1], item[0]))
+ return dict(new_items)
+
+
+def flatten_dict_help(elements, flatten_elements, previous_key=''):
+ for key, value in elements.items():
+ if previous_key:
+ new_key = previous_key + '.' + key
+ else:
+ new_key = key
+ if type(value) is not dict:
+ flatten_elements.append((new_key, value))
+ else:
+ flatten_dict_help(value, flatten_elements, new_key)
+ return dict(flatten_elements)
+
+
+def flatten_dict(elements):
+ flatten_elements = []
+ return flatten_dict_help(elements, flatten_elements)
+
+
+def unflatten_dict(elements):
+ unflatten_elements = dict()
+ for key, value in elements.items():
+ current_elements = unflatten_elements
+ if len(key) > 1:
+ keys = key.split('.')
+ while len(keys) > 1:
+ if keys[0] not in current_elements:
+ current_elements[keys[0]] = dict()
+ current_elements = current_elements[keys[0]]
+ keys.pop(0)
+ current_elements[keys[0]] = value
+ else:
+ current_elements[key] = value
+ return unflatten_elements
+
+
+def counter_of_element(element, elements):
+ return len([x for x in elements if x == element])
+
+
+def reps(elements):
+ return tuple([element for element in elements
+ if counter_of_element(element, elements) > 1])