Решение на Пет малки функции от Явор Михайлов

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

Към профила на Явор Михайлов

Резултати

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

Код

def extract_type(letters_and_counts, type_name):
valid_pairs = [
pair for pair
in letters_and_counts
if isinstance(pair[0], type_name)]
return ''.join([
pair[1] * str(pair[0]) for pair
in valid_pairs
])
def reversed_dict(dict_for_reverse):
return(dict(zip(dict_for_reverse.values(), dict_for_reverse.keys())))
def reps(list_of_numbers):
return(tuple([
number for number
in list_of_numbers
if list_of_numbers.count(number) > 1]))
def flatten_dict(input_dict, left_key=''):
output_dict = {}
for rigth_key, value in input_dict.items():
key = left_key + rigth_key
if isinstance(value, dict):
output_dict.update(flatten_dict(value, key + '.'))
else:
output_dict[key] = value
return output_dict
def unflatten_dict(input_dict):
flatten_dict = {}
print(input_dict.items())
for key, value in input_dict.items():
context = flatten_dict
for sub_key in key.split('.')[:-1]:
if sub_key not in context:
context[sub_key] = {}
context = context[sub_key]
context[key.split('.')[-1]] = value
return(flatten_dict)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.160s

OK

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

Явор обнови решението на 19.03.2015 03:01 (преди около 9 години)

+def extract_type(letters_and_counts, str_type_name):
+ valid_pairs = [
+ pair for pair
+ in letters_and_counts
+ if isinstance(pair[0], str_type_name)]
+
+ return ''.join([
+ pair[1] * str(pair[0]) for pair
+ in valid_pairs
+ ])
+
+
+def reversed_dict(dict_fo_revers):
+ return(dict(zip(dict_fo_revers.values(), dict_fo_revers.keys())))
+
+
+def reps(list_of_numbers):
+ return(tuple([
+ number for number
+ in list_of_numbers
+ if list_of_numbers.count(number) > 1]))
+
+
+def flatten_dict(example_dict, left_key=''):
+ result_dict = {}
+
+ for rigth_key, value in example_dict.items():
+ key = left_key + rigth_key
+ if isinstance(value, dict):
+ result_dict.update(flatten_dict(value, key + '.'))
+ else:
+ result_dict[key] = value
+ return result_dict
+
+
+def unflatten_dict(example_dict):
+ flatten_dict = {}
+
+ for key, value in example_dict.items():
+ context = flatten_dict
+
+ for sub_key in key.split('.')[:-1]:
+ if sub_key not in context:
+ context[sub_key] = {}
+
+ context = context[sub_key]
+
+ context[key.split('.')[-1]] = value
+
+ return(flatten_dict)

Явор обнови решението на 22.03.2015 17:24 (преди около 9 години)

-def extract_type(letters_and_counts, str_type_name):
+def extract_type(letters_and_counts, type_name):
valid_pairs = [
pair for pair
in letters_and_counts
- if isinstance(pair[0], str_type_name)]
+ if isinstance(pair[0], type_name)]
return ''.join([
pair[1] * str(pair[0]) for pair
in valid_pairs
])
-def reversed_dict(dict_fo_revers):
- return(dict(zip(dict_fo_revers.values(), dict_fo_revers.keys())))
+def reversed_dict(dict_for_reverse):
+ return(dict(zip(dict_for_reverse.values(), dict_for_reverse.keys())))
def reps(list_of_numbers):
return(tuple([
number for number
in list_of_numbers
if list_of_numbers.count(number) > 1]))
-def flatten_dict(example_dict, left_key=''):
- result_dict = {}
+def flatten_dict(input_dict, left_key=''):
+ output_dict = {}
- for rigth_key, value in example_dict.items():
+ for rigth_key, value in input_dict.items():
+
key = left_key + rigth_key
+
if isinstance(value, dict):
- result_dict.update(flatten_dict(value, key + '.'))
+ output_dict.update(flatten_dict(value, key + '.'))
else:
- result_dict[key] = value
- return result_dict
+ output_dict[key] = value
+ return output_dict
-def unflatten_dict(example_dict):
+
+def unflatten_dict(input_dict):
flatten_dict = {}
- for key, value in example_dict.items():
+ print(input_dict.items())
+
+ for key, value in input_dict.items():
+
context = flatten_dict
for sub_key in key.split('.')[:-1]:
if sub_key not in context:
context[sub_key] = {}
context = context[sub_key]
context[key.split('.')[-1]] = value
- return(flatten_dict)
+ return(flatten_dict)