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

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

Към профила на Екатерина Горанова

Резултати

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

Код

def extract_type(lst, tp):
return ''.join([str(symb) * n for symb, n in lst if isinstance(symb, tp)])
def reversed_dict(dct):
return {value: key for key, value in dct.items()}
def flatten_dict(dct):
result = {}
for key, value in dct.items():
if isinstance(value, dict):
r = flatten_dict(value)
for k, v in r.items():
result['{}.{}'.format(key, k)] = v
else:
result[key] = value
return result
def unflatten_dict(dct):
result = {}
for key, value in dct.items():
keys = key.split('.')
tmp_dict = result
for subkey in keys[:-1]:
if subkey not in tmp_dict:
tmp_dict[subkey] = {}
tmp_dict = tmp_dict[subkey]
tmp_dict[keys[-1]] = value
return result
def reps(lst):
return tuple([item for item in lst if lst.count(item) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.194s

OK

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

Екатерина обнови решението на 19.03.2015 22:26 (преди над 9 години)

+def extract_type(lst, tp):
+ return ''.join([symbol * n for symbol, n in lst if isinstance(symbol, tp)])
+
+
+def reversed_dict(dct):
+ return {value: key for key, value in dct.items()}
+
+
+def flatten_dict(dct):
+ result = {}
+ for key, value in dct.items():
+ if isinstance(value, dict):
+ r = flatten_dict(value)
+ for k, v in r.items():
+ result['{}.{}'.format(key, k)] = v
+ else:
+ result[key] = value
+ return result
+
+
+def unflatten_dict(dct):
+ result = {}
+ for key, value in dct.items():
+ keys = key.split('.')
+ tmp_dict = result
+ for subkey in keys[:-1]:
+ if subkey not in tmp_dict:
+ tmp_dict[subkey] = {}
+ tmp_dict = tmp_dict[subkey]
+ tmp_dict[keys[-1]] = value
+ return result
+
+
+def reps(lst):
+ return tuple([item for item in lst if lst.count(item) > 1])

Екатерина обнови решението на 23.03.2015 14:08 (преди над 9 години)

def extract_type(lst, tp):
- return ''.join([symbol * n for symbol, n in lst if isinstance(symbol, tp)])
+ return ''.join([str(symb) * n for symb, n in lst if isinstance(symb, tp)])
def reversed_dict(dct):
return {value: key for key, value in dct.items()}
def flatten_dict(dct):
result = {}
for key, value in dct.items():
if isinstance(value, dict):
r = flatten_dict(value)
for k, v in r.items():
result['{}.{}'.format(key, k)] = v
else:
result[key] = value
return result
def unflatten_dict(dct):
result = {}
for key, value in dct.items():
keys = key.split('.')
tmp_dict = result
for subkey in keys[:-1]:
if subkey not in tmp_dict:
tmp_dict[subkey] = {}
tmp_dict = tmp_dict[subkey]
tmp_dict[keys[-1]] = value
return result
def reps(lst):
return tuple([item for item in lst if lst.count(item) > 1])