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

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

Към профила на Иван Вичев

Резултати

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

Код

def extract_type(zipped, type_):
if type_ == str:
sum_elem = ''
else:
sum_elem = 0
for element in zipped:
if isinstance(element[0], type_):
i = element[1]
while i > 0:
sum_elem += element[0]
i = i - 1
return sum_elem
def reversed_dict(dictionary):
new_dict = {}
for key, value in dictionary.items():
if value not in new_dict:
new_dict[value] = key
return new_dict
def flatten_dict(dictionary, level=''):
new_dict = {}
for key, value in dictionary.items():
if isinstance(value, dict):
new_dict.update(flatten_dict(value, level + key))
else:
new_dict['.'.join(level + key)] = value
return new_dict
def reps(elements):
new_elements = []
for element in elements:
if(elements.count(element) > 1):
new_elements.append(element)
return tuple(new_elements)
# Unflatten

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

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

OK

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

Иван обнови решението на 23.03.2015 15:02 (преди над 9 години)

+def extract_type(zipped, type_):
+ if type_ == str:
+ sum_elem = ''
+ else:
+ sum_elem = 0
+ for element in zipped:
+ if isinstance(element[0], type_):
+ i = element[1]
+ while i > 0:
+ sum_elem += element[0]
+ i = i - 1
+ return sum_elem
+
+
+def reversed_dict(dictionary):
+ new_dict = {}
+ for key, value in dictionary.items():
+ if value not in new_dict:
+ new_dict[value] = key
+ return new_dict
+
+
+def flatten_dict(dictionary, level=''):
+ new_dict = {}
+ for key, value in dictionary.items():
+ if isinstance(value, dict):
+ new_dict.update(flatten_dict(value, level + key))
+ else:
+ new_dict['.'.join(level + key)] = value
+ return new_dict
+
+
+def reps(elements):
+ new_elements = []
+ for element in elements:
+ if(elements.count(element) > 1):
+ new_elements.append(element)
+ return tuple(new_elements)
+# Unflatten