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

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

Към профила на Красимир Николов

Резултати

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

Код

def extract_type(archive_text, search_type):
filter_ = lambda x: type(x[0]) is search_type
repeats = lambda x: str(x[0]) * x[1]
return ''.join(map(repeats, filter(filter_, archive_text)))
def reversed_dict(items):
return {value: key for key, value in items.items()}
def flatten_dict(items, left_key=''):
result = {}
for right_key, value in items.items():
key = left_key + right_key
if isinstance(value, dict):
result.update(flatten_dict(value, key + '.'))
else:
result[key] = value
return result
def unflatten_dict(items):
result = {}
for key, value in items.items():
keys = key.split('.')
add_in_dict(result, 0, keys, value)
return result
def add_in_dict(dict_, index, keys, value):
key = keys[index]
if index == len(keys) - 1:
dict_[key] = value
return dict_
if key not in dict_:
dict_[key] = {}
return add_in_dict(dict_[key], index + 1, keys, value)
def reps(items):
return tuple(item for item in items if items.count(item) > 1)

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

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

OK

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

Красимир обнови решението на 19.03.2015 21:53 (преди над 9 години)

+def extract_type(archive_text, search_type):
+ filter_ = lambda x: type(x[0]) is search_type
+ repeats = lambda x: str(x[0]) * x[1]
+ return ''.join(map(repeats, filter(filter_, archive_text)))
+
+
+def reversed_dict(items):
+ return {value: key for key, value in items.items()}
+
+
+def flatten_dict(items, left_key=''):
+ result = {}
+ for right_key, value in items.items():
+ key = left_key + right_key
+ if isinstance(value, dict):
+ result.update(flatten_dict(value, key + '.'))
+ else:
+ result[key] = value
+ return result
+
+
+def unflatten_dict(items):
+ result = {}
+ for key, value in items.items():
+ keys = key.split('.')
+ add_in_dept_dict(result, 0, keys, value)
+ return result
+
+
+def add_in_dept_dict(dict_, index, keys, value):
+ key = keys[index]
+ if index == len(keys) - 1:
+ dict_[key] = value
+ return dict_
+ if key not in dict_:
+ dict_[key] = {}
+ return add_in_dept_dict(dict_[key], index + 1, keys, value)
+
+
+def reps(items):
+ return tuple(item for item in items if items.count(item) > 1)

Красимир обнови решението на 19.03.2015 21:54 (преди над 9 години)

def extract_type(archive_text, search_type):
filter_ = lambda x: type(x[0]) is search_type
repeats = lambda x: str(x[0]) * x[1]
return ''.join(map(repeats, filter(filter_, archive_text)))
def reversed_dict(items):
return {value: key for key, value in items.items()}
def flatten_dict(items, left_key=''):
result = {}
for right_key, value in items.items():
key = left_key + right_key
if isinstance(value, dict):
result.update(flatten_dict(value, key + '.'))
else:
result[key] = value
return result
def unflatten_dict(items):
result = {}
for key, value in items.items():
keys = key.split('.')
- add_in_dept_dict(result, 0, keys, value)
+ add_in_dict(result, 0, keys, value)
return result
-def add_in_dept_dict(dict_, index, keys, value):
+def add_in_dict(dict_, index, keys, value):
key = keys[index]
if index == len(keys) - 1:
dict_[key] = value
return dict_
if key not in dict_:
dict_[key] = {}
- return add_in_dept_dict(dict_[key], index + 1, keys, value)
+ return add_in_dict(dict_[key], index + 1, keys, value)
def reps(items):
return tuple(item for item in items if items.count(item) > 1)