Решение на Пет малки функции от Александър Ваканин

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

Към профила на Александър Ваканин

Резултати

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

Код

def extract_type(symbols, type_):
return ''.join([str(symbol) for symbol, times in symbols
for _ in range(times) if type(symbol) is type_])
def reversed_dict(input_dict):
return {input_dict[key]: key for key in input_dict}
def change_dict_keys(input_dict, prefix):
return dict((prefix + key, change_dict_keys(value, prefix)
if hasattr(value, 'keys') else value)
for key, value in input_dict.items())
def flatten_dict(input_dict):
result = {}
input_dict_items = input_dict.items()
for key, value in input_dict_items:
if type(value) is dict and value != {}:
more_results = flatten_dict(value)
result.update(change_dict_keys(more_results, '{}.'.format(key)))
else:
result[key] = value
return result
def unflatten_dict(input_dict):
result = {}
input_dict_items = input_dict.items()
for key, value in input_dict_items:
changing_result = result
keys = key.split('.')
for sub_key in keys[:-1]:
if sub_key not in changing_result:
changing_result[sub_key] = {}
changing_result = changing_result[sub_key]
changing_result[keys[-1]] = value
return result
def reps(input_data):
return tuple([element for element in input_data
if input_data.count(element) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.178s

OK

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

Александър обнови решението на 18.03.2015 11:02 (преди над 9 години)

+def extract_type(symbols, type_):
+ return ''.join([str(symbol) for symbol, times in symbols
+ for _ in range(times) if type(symbol) is type_])
+
+
+def reversed_dict(input_dict):
+ return {input_dict[k]: k for k in input_dict}
+
+
+def change_keys(mydict, prefix):
+ return dict((prefix + k, f(v) if hasattr(v, 'keys') else v)
+ for k, v in mydict.items())
+
+
+def flatten_dict(input_dict):
+ result = {}
+ dict_items = input_dict.items()
+ for key, value in dict_items:
+ if type(value) is dict:
+ more_results = flatten_dict(value)
+ result.update(change_keys(more_results, '{}.'.format(key)))
+ else:
+ result[key] = value
+ return result
+
+
+def unflatten_dict(input_dict):
+ result = {}
+ dict_items = input_dict.items()
+ for key, value in dict_items:
+ changing_dict = result
+ keys = key.split('.')
+ for sub_key in keys[:-1]:
+ if sub_key not in changing_dict:
+ changing_dict[sub_key] = {}
+ changing_dict = changing_dict[sub_key]
+ changing_dict[keys[-1]] = value
+ return result
+
+
+def reps(input_list):
+ return tuple([x for x in input_list if input_list.count(x) > 1])

Александър обнови решението на 18.03.2015 16:55 (преди над 9 години)

def extract_type(symbols, type_):
return ''.join([str(symbol) for symbol, times in symbols
for _ in range(times) if type(symbol) is type_])
def reversed_dict(input_dict):
return {input_dict[k]: k for k in input_dict}
def change_keys(mydict, prefix):
- return dict((prefix + k, f(v) if hasattr(v, 'keys') else v)
+ return dict((prefix + k, change_keys(v, prefix) if hasattr(v, 'keys') else v)
for k, v in mydict.items())
def flatten_dict(input_dict):
result = {}
dict_items = input_dict.items()
for key, value in dict_items:
- if type(value) is dict:
+ if type(value) is dict and value != {}:
more_results = flatten_dict(value)
result.update(change_keys(more_results, '{}.'.format(key)))
else:
result[key] = value
return result
def unflatten_dict(input_dict):
result = {}
dict_items = input_dict.items()
for key, value in dict_items:
changing_dict = result
keys = key.split('.')
for sub_key in keys[:-1]:
if sub_key not in changing_dict:
changing_dict[sub_key] = {}
changing_dict = changing_dict[sub_key]
changing_dict[keys[-1]] = value
return result
def reps(input_list):
return tuple([x for x in input_list if input_list.count(x) > 1])

Александър обнови решението на 18.03.2015 16:59 (преди над 9 години)

def extract_type(symbols, type_):
return ''.join([str(symbol) for symbol, times in symbols
for _ in range(times) if type(symbol) is type_])
def reversed_dict(input_dict):
return {input_dict[k]: k for k in input_dict}
def change_keys(mydict, prefix):
- return dict((prefix + k, change_keys(v, prefix) if hasattr(v, 'keys') else v)
- for k, v in mydict.items())
+ return dict((prefix + k, change_keys(v, prefix)
+ if hasattr(v, 'keys') else v) for k, v in mydict.items())
def flatten_dict(input_dict):
result = {}
dict_items = input_dict.items()
for key, value in dict_items:
if type(value) is dict and value != {}:
more_results = flatten_dict(value)
result.update(change_keys(more_results, '{}.'.format(key)))
else:
result[key] = value
return result
def unflatten_dict(input_dict):
result = {}
dict_items = input_dict.items()
for key, value in dict_items:
changing_dict = result
keys = key.split('.')
for sub_key in keys[:-1]:
if sub_key not in changing_dict:
changing_dict[sub_key] = {}
changing_dict = changing_dict[sub_key]
changing_dict[keys[-1]] = value
return result
def reps(input_list):
return tuple([x for x in input_list if input_list.count(x) > 1])

Александър обнови решението на 21.03.2015 18:26 (преди над 9 години)

def extract_type(symbols, type_):
return ''.join([str(symbol) for symbol, times in symbols
for _ in range(times) if type(symbol) is type_])
def reversed_dict(input_dict):
- return {input_dict[k]: k for k in input_dict}
+ return {input_dict[key]: key for key in input_dict}
-def change_keys(mydict, prefix):
- return dict((prefix + k, change_keys(v, prefix)
- if hasattr(v, 'keys') else v) for k, v in mydict.items())
+def change_dict_keys(input_dict, prefix):
+ return dict((prefix + key, change_dict_keys(value, prefix)
+ if hasattr(value, 'keys') else value)
+ for key, value in input_dict.items())
def flatten_dict(input_dict):
result = {}
- dict_items = input_dict.items()
- for key, value in dict_items:
+ input_dict_items = input_dict.items()
+ for key, value in input_dict_items:
if type(value) is dict and value != {}:
more_results = flatten_dict(value)
- result.update(change_keys(more_results, '{}.'.format(key)))
+ result.update(change_dict_keys(more_results, '{}.'.format(key)))
else:
result[key] = value
return result
def unflatten_dict(input_dict):
result = {}
- dict_items = input_dict.items()
- for key, value in dict_items:
- changing_dict = result
+ input_dict_items = input_dict.items()
+ for key, value in input_dict_items:
+ changing_result = result
keys = key.split('.')
for sub_key in keys[:-1]:
- if sub_key not in changing_dict:
- changing_dict[sub_key] = {}
- changing_dict = changing_dict[sub_key]
- changing_dict[keys[-1]] = value
+ if sub_key not in changing_result:
+ changing_result[sub_key] = {}
+ changing_result = changing_result[sub_key]
+ changing_result[keys[-1]] = value
return result
-def reps(input_list):
- return tuple([x for x in input_list if input_list.count(x) > 1])
+def reps(input_data):
+ return tuple([element for element in input_data
+ if input_data.count(element) > 1])

Благодаря много за коментара. :) На няколко места съм оставил имена от вида input_dict, но тъй като самата функция подсказва, а и условието на задачата предполага да бъде получен dict, се надявам името да не е проблем. Всъщност не съм наясно дали в подобен случай е по-удачно името на такава променлива да бъде променено, или е по-добре да си остане по този начин. Благодаря. :)