Решение на Пет малки функции от Иван Иванов

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

Към профила на Иван Иванов

Резултати

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

Код

def extract_type(unextracted, type_):
return ''.join([str(pair[0]) * pair[1] for pair in unextracted
if type(pair[0]) is type_])
def reversed_dict(_):
return dict([[value, key] for key, value in _.items()])
def flatten_dict(unflattened):
def expand(key_, value_):
if isinstance(value_, dict):
return [(key_ + '.' + key, value)
for key, value in flatten_dict(value_).items()]
return [(key_, value_)]
return dict([item for key, value in unflattened.items()
for item in expand(key, value)])
def unflatten_dict(flattened, result=None):
if result is None:
result = {}
for element in flattened:
if '.' in element:
if element.split('.')[0] in result:
result[element.split('.')[0]] = unflatten_dict(
{element[len(element.split('.')[0]) + 1:]:
flattened[element]},
result[element.split('.')[0]])
else:
result[element.split('.')[0]] = unflatten_dict(
{element[len(element.split('.')[0]) + 1:]:
flattened[element]},
{})
else:
result[element] = flattened[element]
return result
def reps(elements):
return tuple([_ for _ in elements if elements.count(_) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Иван обнови решението на 22.03.2015 19:23 (преди над 9 години)

+def extract_type(args, type_):
+ return ''.join([str(arg[0])*arg[1] for arg in args
+ if type(arg[0]) is type_])
+
+
+def reversed_dict(arg):
+ return dict([[value, key] for key, value in arg.items()])
+
+
+def flatten_dict(arg):
+ def expand(key_, value_):
+ if isinstance(value_, dict):
+ return [(key_ + '.' + key, value)
+ for key, value in flatten_dict(value_).items()]
+ return [(key_, value_)]
+
+ return dict([item for key, value in arg.items()
+ for item in expand(key, value)])
+
+
+def unflatten_dict(arg, result=None):
+ if result is None:
+ result = {}
+
+ for element in arg:
+ if '.' in element:
+ if element.split('.')[0] in result:
+ result[element.split('.')[0]] = unflatten_dict(
+ {element[len(element.split('.')[0]) + 1:]: arg[element]},
+ result[element.split('.')[0]])
+ else:
+ result[element.split('.')[0]] = unflatten_dict(
+ {element[len(element.split('.')[0]) + 1:]: arg[element]},
+ {})
+ else:
+ result[element] = arg[element]
+
+ return result
+
+
+def reps(arg):
+ return tuple([_ for _ in arg if arg.count(_) > 1])

Иван обнови решението на 22.03.2015 19:26 (преди над 9 години)

def extract_type(args, type_):
- return ''.join([str(arg[0])*arg[1] for arg in args
+ return ''.join([str(arg[0]) * arg[1] for arg in args
if type(arg[0]) is type_])
def reversed_dict(arg):
return dict([[value, key] for key, value in arg.items()])
def flatten_dict(arg):
def expand(key_, value_):
if isinstance(value_, dict):
return [(key_ + '.' + key, value)
for key, value in flatten_dict(value_).items()]
return [(key_, value_)]
return dict([item for key, value in arg.items()
for item in expand(key, value)])
def unflatten_dict(arg, result=None):
if result is None:
result = {}
for element in arg:
if '.' in element:
if element.split('.')[0] in result:
result[element.split('.')[0]] = unflatten_dict(
{element[len(element.split('.')[0]) + 1:]: arg[element]},
result[element.split('.')[0]])
else:
result[element.split('.')[0]] = unflatten_dict(
{element[len(element.split('.')[0]) + 1:]: arg[element]},
{})
else:
result[element] = arg[element]
return result
def reps(arg):
return tuple([_ for _ in arg if arg.count(_) > 1])

Иван обнови решението на 23.03.2015 15:54 (преди над 9 години)

-def extract_type(args, type_):
- return ''.join([str(arg[0]) * arg[1] for arg in args
- if type(arg[0]) is type_])
+def extract_type(unextracted, type_):
+ return ''.join([str(pair[0]) * pair[1] for pair in unextracted
+ if type(pair[0]) is type_])
-def reversed_dict(arg):
- return dict([[value, key] for key, value in arg.items()])
+def reversed_dict(_):
+ return dict([[value, key] for key, value in _.items()])
-def flatten_dict(arg):
+def flatten_dict(unflattened):
def expand(key_, value_):
if isinstance(value_, dict):
return [(key_ + '.' + key, value)
for key, value in flatten_dict(value_).items()]
return [(key_, value_)]
- return dict([item for key, value in arg.items()
+ return dict([item for key, value in unflattened.items()
for item in expand(key, value)])
-def unflatten_dict(arg, result=None):
+def unflatten_dict(flattened, result=None):
if result is None:
result = {}
- for element in arg:
+ for element in flattened:
if '.' in element:
if element.split('.')[0] in result:
result[element.split('.')[0]] = unflatten_dict(
- {element[len(element.split('.')[0]) + 1:]: arg[element]},
+ {element[len(element.split('.')[0]) + 1:]:
+ flattened[element]},
result[element.split('.')[0]])
else:
result[element.split('.')[0]] = unflatten_dict(
- {element[len(element.split('.')[0]) + 1:]: arg[element]},
+ {element[len(element.split('.')[0]) + 1:]:
+ flattened[element]},
{})
else:
- result[element] = arg[element]
+ result[element] = flattened[element]
return result
-def reps(arg):
- return tuple([_ for _ in arg if arg.count(_) > 1])
+def reps(elements):
+ return tuple([_ for _ in elements if elements.count(_) > 1])