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

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

Към профила на Никола Терзиев

Резултати

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

Код

from itertools import repeat
def extract_type(items, i_type):
output = i_type()
for item in items:
if isinstance(item[0], i_type):
for i in repeat(item[0], item[1]):
output += str(i)
return output
def reversed_dict(dic):
return dict((value, key) for (key, value) in dic.items())
def flatten_dict(dic):
if len(dic) == 0:
return {}
output = {}
for (key, value) in dic.items():
if not isinstance(value, dict):
output[key] = value
else:
output.update(dict((key + '.' + inner_key, inner_value)
for (inner_key, inner_value) in flatten_dict(value).items()))
return output
def unflatten_dict(dic):
output = {}
for (key, value) in dic.items():
if key.find('.') == -1:
output[key] = value
else:
output = dict_merge(output, dictify(key, value, '.'))
return output
def dictify(source, value, delimiter):
output, parts = {}, source.split(delimiter)
if len(parts) == 1:
return {source: value}
ref = output
for i in parts[:-1]:
ref[i] = dict()
ref = ref[i]
ref[parts[-1]] = value
return output
def dict_merge(source, extension):
output = dict(source)
for (key, value) in extension.items():
if not isinstance(value, dict):
output[key] = value
elif key in output and isinstance(output[key], dict):
output[key] = dict_merge(output[key], value)
else:
output[key] = value
return output
def reps(items):
return tuple(filter(lambda x: items.count(x) > 1, items))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.165s

OK

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

Никола обнови решението на 23.03.2015 00:36 (преди над 9 години)

+from itertools import repeat
+
+
+def extract_type(items, i_type):
+ output = i_type()
+ for item in items:
+ if isinstance(item[0], i_type):
+ for i in repeat(item[0], item[1]):
+ output += i
+ return output
+
+
+def reversed_dict(dic):
+ return dict((value, key) for (key, value) in dic.items())
+
+
+def flatten_dict(dic):
+ if len(dic) == 0:
+ return {}
+
+ output = {}
+
+ for (key, value) in dic.items():
+ if not isinstance(value, dict):
+ output[key] = value
+ else:
+ output.update(dict((key + '.' + inner_key, inner_value)
+ for (inner_key, inner_value) in flatten_dict(value).items()))
+ return output
+
+
+def unflatten_dict(dic):
+ output = {}
+ for (key, value) in dic.items():
+ if key.find('.') == -1:
+ output[key] = value
+ else:
+ output = dict_merge(output, dictify(key, value, '.'))
+ return output
+
+
+def dictify(source, value, delimiter):
+ output, parts = {}, source.split(delimiter)
+ if len(parts) == 1:
+ return {source: value}
+
+ ref = output
+ for i in parts[:-1]:
+ ref[i] = dict()
+ ref = ref[i]
+ ref[parts[-1]] = value
+ return output
+
+
+def dict_merge(source, extension):
+ output = dict(source)
+ for (key, value) in extension.items():
+ if not isinstance(value, dict):
+ output[key] = value
+ elif key in output and isinstance(output[key], dict):
+ output[key] = dict_merge(output[key], value)
+ else:
+ output[key] = value
+ return output
+
+
+def reps(items):
+ return tuple(filter(lambda x: items.count(x) > 1, items))

Никола обнови решението на 23.03.2015 15:53 (преди над 9 години)

from itertools import repeat
def extract_type(items, i_type):
output = i_type()
for item in items:
if isinstance(item[0], i_type):
for i in repeat(item[0], item[1]):
- output += i
+ output += str(i)
return output
def reversed_dict(dic):
return dict((value, key) for (key, value) in dic.items())
def flatten_dict(dic):
if len(dic) == 0:
return {}
output = {}
for (key, value) in dic.items():
if not isinstance(value, dict):
output[key] = value
else:
output.update(dict((key + '.' + inner_key, inner_value)
for (inner_key, inner_value) in flatten_dict(value).items()))
return output
def unflatten_dict(dic):
output = {}
for (key, value) in dic.items():
if key.find('.') == -1:
output[key] = value
else:
output = dict_merge(output, dictify(key, value, '.'))
return output
def dictify(source, value, delimiter):
output, parts = {}, source.split(delimiter)
if len(parts) == 1:
return {source: value}
ref = output
for i in parts[:-1]:
ref[i] = dict()
ref = ref[i]
ref[parts[-1]] = value
return output
def dict_merge(source, extension):
output = dict(source)
for (key, value) in extension.items():
if not isinstance(value, dict):
output[key] = value
elif key in output and isinstance(output[key], dict):
output[key] = dict_merge(output[key], value)
else:
output[key] = value
return output
def reps(items):
return tuple(filter(lambda x: items.count(x) > 1, items))