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

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

Към профила на Станислав Траилов

Резултати

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

Код

def reversed_dict(not_reversed_dict):
return {not_reversed_dict[i]: i for i in not_reversed_dict}
def flatten_dict(my_dict):
def flatten_dict_helper(my_dict, key_before = '', separator = '.' ):
list_of_dict_items = []
for k, v in my_dict.items():
new_key = key_before + separator + k if key_before else k
if isinstance(v, dict):
list_of_dict_items.extend(flatten_dict_helper(v, new_key, separator).items())
else:
list_of_dict_items.append((new_key, v))
return dict(list_of_dict_items)
return flatten_dict_helper(my_dict, '', '.')
def unflatten_dict(my_dict):
result_dict = {}
for k, v in my_dict.items():
key_parts = k.split('.')
temp_dict = result_dict
for part in key_parts[:-1]:
if part not in temp_dict:
temp_dict[part] = {}
temp_dict = temp_dict[part]
temp_dict[key_parts[-1]] = v
return result_dict
def reps(my_list):
return tuple([x for x in my_list if my_list.count(x) > 1])
def extract_type(list_of_tuples, your_type):
output_str=''
for x in list_of_tuples:
if isinstance(x[0], your_type):
output_str = output_str + x[0]*x[1]
return output_str

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.163s

OK

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

Станислав обнови решението на 23.03.2015 09:52 (преди над 9 години)

+def reversed_dict(not_reversed_dict):
+ return {not_reversed_dict[i]: i for i in not_reversed_dict}
+
+def flatten_dict(my_dict):
+ def flatten_dict_helper(my_dict, key_before = '', separator = '.' ):
+ list_of_dict_items = []
+ for k, v in my_dict.items():
+ new_key = key_before + separator + k if key_before else k
+ if isinstance(v, dict):
+ list_of_dict_items.extend(flatten_dict_helper(v, new_key, separator).items())
+ else:
+ list_of_dict_items.append((new_key, v))
+ return dict(list_of_dict_items)
+ return flatten_dict_helper(my_dict, '', '.')
+
+def unflatten_dict(my_dict):
+ result_dict = {}
+ for k, v in my_dict.items():
+ key_parts = k.split('.')
+ temp_dict = result_dict
+ for part in key_parts[:-1]:
+ if part not in temp_dict:
+ temp_dict[part] = {}
+ temp_dict = temp_dict[part]
+ temp_dict[key_parts[-1]] = v
+ return result_dict
+
+def reps(my_list):
+ return [x for x in my_list if my_list.count(x) > 1]
+
+def extract_type(list_of_tuples, your_type):
+ output_str=''
+ for x in list_of_tuples:
+ if isinstance(x[0], your_type):
+ output_str = output_str + x[0]*x[1]
+ return output_str

Станислав обнови решението на 23.03.2015 12:49 (преди над 9 години)

def reversed_dict(not_reversed_dict):
return {not_reversed_dict[i]: i for i in not_reversed_dict}
def flatten_dict(my_dict):
def flatten_dict_helper(my_dict, key_before = '', separator = '.' ):
list_of_dict_items = []
for k, v in my_dict.items():
new_key = key_before + separator + k if key_before else k
if isinstance(v, dict):
list_of_dict_items.extend(flatten_dict_helper(v, new_key, separator).items())
else:
list_of_dict_items.append((new_key, v))
return dict(list_of_dict_items)
return flatten_dict_helper(my_dict, '', '.')
def unflatten_dict(my_dict):
result_dict = {}
for k, v in my_dict.items():
key_parts = k.split('.')
temp_dict = result_dict
for part in key_parts[:-1]:
if part not in temp_dict:
temp_dict[part] = {}
temp_dict = temp_dict[part]
temp_dict[key_parts[-1]] = v
return result_dict
def reps(my_list):
- return [x for x in my_list if my_list.count(x) > 1]
+ return tuple([x for x in my_list if my_list.count(x) > 1])
def extract_type(list_of_tuples, your_type):
output_str=''
for x in list_of_tuples:
if isinstance(x[0], your_type):
output_str = output_str + x[0]*x[1]
return output_str