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

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

Към профила на Мартин Пацов

Резултати

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

Код

def extract_type(*args):
extracted = ""
for element in args[0]:
if type(element[0]) is args[1]:
extracted += str(element[0])*element[1]
return extracted
def reversed_dict(kwargs):
reverse = {}
for item in kwargs:
reverse[kwargs[item]] = item
return reverse
curr_flat = {}
keys = []
level = 0
def flatten_dict(kwargs):
global level
global curr_flat
global keys
for key, value in kwargs.items():
if len(keys) == level:
keys.append(key)
else:
keys[level] = key
if type(value) is dict:
level += 1
flatten_dict(value)
level -= 1
else:
key_str = ""
for i in range(level + 1):
key_str += keys[i] + '.'
curr_flat[key_str[:-1]] = value
if level == 0:
ret_flat = curr_flat
curr_flat = {}
keys = []
level = 0
return ret_flat
else:
return curr_flat
def unflatten_dict(kwargs):
ret_unflat = {}
for item, value in kwargs.items():
all_curr_keys = item.split('.')
curr_unflat = ret_unflat
for key_pos in range(len(all_curr_keys) - 1):
if all_curr_keys[key_pos] not in curr_unflat:
curr_unflat[all_curr_keys[key_pos]] = {}
curr_unflat = integrate_dict(curr_unflat, all_curr_keys[key_pos])
curr_unflat[all_curr_keys[len(all_curr_keys) - 1]] = value
return ret_unflat
def integrate_dict(dict_for_int, key):
return dict_for_int[key]
def reps(args):
return tuple([x for x in args if args.count(x) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.133s

OK

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

Мартин обнови решението на 22.03.2015 00:48 (преди над 9 години)

+def extract_type(*args):
+ extracted = ""
+ for element in args[0]:
+ if type(element[0]) is args[1]:
+ extracted += str(element[0])*element[1]
+ return extracted
+
+
+def reversed_dict(kwargs):
+ reverse = {}
+ for item in kwargs:
+ reverse[kwargs[item]] = item
+ return reverse
+
+
+curr_flat = {}
+keys = []
+level = 0
+
+
+def flatten_dict(kwargs):
+ global level
+ global curr_flat
+ global keys
+ for key, value in kwargs.items():
+ if len(keys) == level:
+ keys.append(key)
+ else:
+ keys[level] = key
+
+ if type(value) is dict:
+ level += 1
+ flatten_dict(value)
+ level -= 1
+ else:
+ key_str = ""
+ for i in range(level + 1):
+ key_str += keys[i] + '.'
+
+ curr_flat[key_str[:-1]] = value
+
+ if level == 0:
+ ret_flat = curr_flat
+ curr_flat = {}
+ keys = []
+ level = 0
+ return ret_flat
+ else:
+ return curr_flat
+
+
+def unflatten_dict(kwargs):
+ ret_unflat = {}
+ for item, value in kwargs.items():
+ all_curr_keys = item.split('.')
+ curr_unflat = ret_unflat
+
+ for key_pos in range(len(all_curr_keys) - 1):
+ if all_curr_keys[key_pos] not in curr_unflat:
+ curr_unflat[all_curr_keys[key_pos]] = {}
+ curr_unflat = integrate_dict(curr_unflat, all_curr_keys[key_pos])
+ curr_unflat[all_curr_keys[len(all_curr_keys) - 1]] = value
+ return ret_unflat
+
+
+def integrate_dict(dict_for_int, key):
+ return dict_for_int[key]
+
+
+def reps(args):
+ return tuple([x for x in args if args.count(x) > 1])