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

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

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

Резултати

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

Код

from functools import reduce
def extract_type(items, valid_type):
result = [str(value) * count for (value, count)
in items if type(value) is valid_type]
return "".join(result)
def update_and_return(dictionary, pair):
dictionary.update({pair[1]: pair[0]})
return dictionary
def reversed_dict(dictionary):
return reduce(update_and_return, dictionary.items(), {})
def flatten_dict_internal(result, context, dictionary):
for pair in dictionary.items():
new_context = context[:]
new_context.append(pair[0])
if type(pair[1]) is dict:
flatten_dict_internal(result, new_context, pair[1])
else:
result[".".join(new_context)] = pair[1]
return result
def flatten_dict(dictionary):
return flatten_dict_internal({}, [], dictionary)
def unflatten_dict(dictionary):
result = {}
for pair in dictionary.items():
current = result
keys = pair[0].split('.')
for key in keys[:-1]:
current[key] = current.get(key, {})
current = current[key]
current[keys[-1]] = pair[1]
return result
def reps(items):
return tuple([item for item in items if items.count(item) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.135s

OK

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

Кристиан обнови решението на 18.03.2015 20:17 (преди над 9 години)

+from functools import reduce
+
+
+def extract_type(list, valid_type):
+ result = [str(value) * count for (value, count)
+ in list if type(value) is valid_type]
+ return "".join(result)
+
+
+def update_and_return(dictionary, pair):
+ dictionary.update({pair[1]: pair[0]})
+ return dictionary
+
+
+def reversed_dict(dictionary):
+ return reduce(update_and_return, dictionary.items(), {})
+
+
+def flatten_dict_internal(result, context, dictionary):
+ for pair in dictionary.items():
+ new_context = context[:]
+ new_context.append(pair[0])
+
+ if type(pair[1]) is dict:
+ flatten_dict_internal(result, new_context, pair[1])
+ else:
+ result[".".join(new_context)] = pair[1]
+ return result
+
+
+def flatten_dict(dictionary):
+ return flatten_dict_internal({}, [], dictionary)
+
+
+def unflatten_dict(dictionary):
+ result = {}
+ for pair in dictionary.items():
+ current = result
+ keys = pair[0].split('.')
+ for key in keys[:-1]:
+ current[key] = current.get(key, {})
+ current = current[key]
+ current[keys[-1]] = pair[1]
+ return result
+
+
+def reps(items):
+ return tuple([item for item in items if items.count(item) > 1])

Кристиан обнови решението на 21.03.2015 14:50 (преди над 9 години)

from functools import reduce
-def extract_type(list, valid_type):
+def extract_type(items, valid_type):
result = [str(value) * count for (value, count)
- in list if type(value) is valid_type]
+ in items if type(value) is valid_type]
return "".join(result)
def update_and_return(dictionary, pair):
dictionary.update({pair[1]: pair[0]})
return dictionary
def reversed_dict(dictionary):
return reduce(update_and_return, dictionary.items(), {})
def flatten_dict_internal(result, context, dictionary):
for pair in dictionary.items():
new_context = context[:]
new_context.append(pair[0])
if type(pair[1]) is dict:
flatten_dict_internal(result, new_context, pair[1])
else:
result[".".join(new_context)] = pair[1]
return result
def flatten_dict(dictionary):
return flatten_dict_internal({}, [], dictionary)
def unflatten_dict(dictionary):
result = {}
for pair in dictionary.items():
current = result
keys = pair[0].split('.')
for key in keys[:-1]:
current[key] = current.get(key, {})
current = current[key]
current[keys[-1]] = pair[1]
return result
def reps(items):
- return tuple([item for item in items if items.count(item) > 1])
+ return tuple([item for item in items if items.count(item) > 1])