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

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

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

Резултати

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

Код

import collections
def extract_type(list, element_type):
result = ''
result_elements = [element for element in list
if str(element_type) in str(type(element[0]))]
for element in result_elements:
result += ''.join([str(element[0])] * element[1])
return result
def reversed_dict(dict):
return {dict[x]: x for x in dict}
def flatten_dict(dict):
def flatten_dict_helper(dict, seperator=''):
result = {}
for key in dict:
new_key = seperator + key
if isinstance(dict[key], type({})):
result.update(flatten_dict_helper(dict[key], new_key + '.'))
else:
result[new_key] = dict[key]
return result
return flatten_dict_helper(dict)
def unflatten_dict(dict):
result = {}
for key in dict:
parent_dict = result
for sub_key in key.split('.')[:-1]:
if sub_key not in parent_dict:
parent_dict[sub_key] = {}
parent_dict = parent_dict[sub_key]
parent_dict[key.split('.')[-1]] = dict[key]
return result
def reps(list):
counts = collections.Counter(list)
return tuple([x for x in list if counts[x] > 1])

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

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

OK

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

Десислава обнови решението на 22.03.2015 22:39 (преди над 9 години)

+import collections
+
+
+def extract_type(list, element_type):
+ result = ''
+ result_elements = [element for element in list
+ if str(element_type) in str(type(element[0]))]
+ for element in result_elements:
+ result += ''.join([str(element[0])] * element[1])
+ return result
+
+
+def reversed_dict(dict):
+ return {dict[x]: x for x in dict}
+
+
+def flatten_dict(dict):
+ def flatten_dict_helper(dict, seperator=''):
+ result = {}
+ for key in dict:
+ new_key = seperator + key
+ if isinstance(dict[key], type({})):
+ result.update(flatten_dict_helper(dict[key], new_key + '.'))
+ else:
+ result[new_key] = dict[key]
+ return result
+
+ return flatten_dict_helper(dict)
+
+
+def unflatten_dict(dict):
+ result = {}
+ for key in dict:
+ parent_dict = result
+ for sub_key in key.split('.')[:-1]:
+ if sub_key not in parent_dict:
+ parent_dict[sub_key] = {}
+ parent_dict = parent_dict[sub_key]
+ parent_dict[key.split('.')[-1]] = dict[key]
+ return result
+
+
+def reps(list):
+ counts = collections.Counter(list)
+ return tuple([x for x in list if counts[x] > 1])