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

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

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

Резултати

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

Код

def extract_type(text, searched_type):
extract = lambda symbol, repeats: str(symbol) * repeats
match_type = lambda symbol: type(symbol) is searched_type
finds = [extract(pair[0], pair[1]) for pair in text if match_type(pair[0])]
return ''.join(finds)
def reversed_dict(dikt):
return {value:key for key, value in dikt.items()}
def flatten_dict(dikt, key=None):
flattened = {}
for top_key, top_value in dikt.items():
if type(top_value) is dict:
inner_dict = flatten_dict(top_value, top_key)
for inner_key, inner_value in inner_dict.items():
flattened[top_key + "." + inner_key] = inner_value
else:
flattened[top_key] = top_value
return flattened
def unflatten_dict(dikt):
unflattened = {}
for top_key, top_value in dikt.items():
keys = top_key.split('.')
outer_list = unflattened
for inner_key in keys:
if inner_key not in outer_list.keys():
outer_list[inner_key] = {}
if inner_key == keys[-1]:
outer_list[inner_key] = top_value
outer_list = outer_list[inner_key]
return unflattened
def reps(collection):
return tuple([item for item in collection if collection.count(item) != 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.156s

OK

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

Добромир обнови решението на 19.03.2015 22:20 (преди над 9 години)

+def extract_type(syms, srched_t):
+ return ''.join([str(p[0]) * p[1] for p in syms if type(p[0]) is srched_t])
+
+
+def reversed_dict(dikt):
+ return {v:k for k, v in dikt.items()}
+
+
+def flatten_dict(dikt, key=None):
+ flattened = {}
+ for top_k, top_v in dikt.items():
+ if type(top_v) is dict:
+ inner_dict = flatten_dict(top_v, top_k)
+ for inner_k, inner_v in inner_dict.items():
+ flattened[top_k + "." + inner_k] = inner_v
+ else:
+ flattened[top_k] = top_v
+ return flattened
+
+
+def unflatten_dict(dikt):
+ unflattened = {}
+ for top_k, top_v in dikt.items():
+ keys = top_k.split('.')
+ lst = unflattened
+ for inner_k in keys:
+ if inner_k not in lst.keys():
+ lst[inner_k] = {}
+ if inner_k == keys[-1]:
+ lst[inner_k] = top_v
+ lst = lst[inner_k]
+ return unflattened
+
+
+def reps(collection):
+ return tuple([x for x in collection if collection.count(x) != 1])

Добромир обнови решението на 22.03.2015 23:21 (преди над 9 години)

-def extract_type(syms, srched_t):
- return ''.join([str(p[0]) * p[1] for p in syms if type(p[0]) is srched_t])
+def extract_type(text, searched_type):
+ extract = lambda symbol, repeats: str(symbol) * repeats
+ match_type = lambda symbol: type(symbol) is searched_type
+ finds = [extract(pair[0], pair[1]) for pair in text if match_type(pair[0])]
+ return ''.join(finds)
+
def reversed_dict(dikt):
- return {v:k for k, v in dikt.items()}
+ return {value:key for key, value in dikt.items()}
def flatten_dict(dikt, key=None):
flattened = {}
- for top_k, top_v in dikt.items():
- if type(top_v) is dict:
- inner_dict = flatten_dict(top_v, top_k)
- for inner_k, inner_v in inner_dict.items():
- flattened[top_k + "." + inner_k] = inner_v
+ for top_key, top_value in dikt.items():
+ if type(top_value) is dict:
+ inner_dict = flatten_dict(top_value, top_key)
+ for inner_key, inner_value in inner_dict.items():
+ flattened[top_key + "." + inner_key] = inner_value
else:
- flattened[top_k] = top_v
+ flattened[top_key] = top_value
return flattened
def unflatten_dict(dikt):
unflattened = {}
- for top_k, top_v in dikt.items():
- keys = top_k.split('.')
- lst = unflattened
- for inner_k in keys:
- if inner_k not in lst.keys():
- lst[inner_k] = {}
- if inner_k == keys[-1]:
- lst[inner_k] = top_v
- lst = lst[inner_k]
+ for top_key, top_value in dikt.items():
+ keys = top_key.split('.')
+ outer_list = unflattened
+ for inner_key in keys:
+ if inner_key not in outer_list.keys():
+ outer_list[inner_key] = {}
+ if inner_key == keys[-1]:
+ outer_list[inner_key] = top_value
+ outer_list = outer_list[inner_key]
return unflattened
def reps(collection):
- return tuple([x for x in collection if collection.count(x) != 1])
+ return tuple([item for item in collection if collection.count(item) != 1])