Решение на Пет малки функции от Любомир Гавадинов

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

Към профила на Любомир Гавадинов

Резултати

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

Код

def extract_type(txt, data_type):
return ''.join([str(x[0]) * x[1] for x in txt if type(x[0]) == data_type])
def reversed_dict(values):
return {value: key for key, value in values.items()}
def flatten_dict(values, p_key=False, sep='.'):
result = []
for key, value in values.items():
new_key = p_key + sep + key if p_key else key
if type(value) == dict:
result.extend(flatten_dict(value, new_key).items())
else:
result.append((new_key, value))
return dict(result)
def unflatten_dict(values, sep='.'):
unflatten = {}
for path, value in values.items():
keys = path.split(sep)
res = unflatten
while len(keys) > 1:
key = keys.pop(0)
if key not in res:
res[key] = {}
res = res[key]
key = keys.pop()
res[key] = value
return unflatten
def reps(values):
return tuple([el for el in values if values.count(el) > 1])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.157s

OK

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

Любомир обнови решението на 23.03.2015 00:52 (преди над 9 години)

+def extract_type(txt, data_type):
+ return ''.join([el[0] * el[1] for el in txt if type(el[0]) == data_type])
+
+
+def reversed_dict(values):
+ return {value: key for key, value in values.items()}
+
+
+def flatten_dict(values, p_key=False):
+ sep = '.'
+ result = []
+ for key, value in values.items():
+ new_key = p_key + sep + key if p_key else key
+ if type(value) == dict:
+ result.extend(flatten_dict(value, new_key).items())
+ else:
+ result.append((new_key, value))
+
+ return dict(result)
+
+
+def unflatten_dict(values):
+ sep = '.'
+ unflatten = {}
+ for path, value in values.items():
+ keys = path.split('.')
+ res = unflatten
+ while len(keys) > 1:
+ key = keys.pop(0)
+ if key not in res:
+ res[key] = {}
+ res = res[key]
+
+ key = keys.pop()
+ res[key] = value
+
+ return unflatten
+
+
+def reps(values):
+ return tuple([el for el in values if values.count(el) > 1])

Любомир обнови решението на 23.03.2015 01:51 (преди над 9 години)

def extract_type(txt, data_type):
return ''.join([el[0] * el[1] for el in txt if type(el[0]) == data_type])
def reversed_dict(values):
return {value: key for key, value in values.items()}
-def flatten_dict(values, p_key=False):
- sep = '.'
+def flatten_dict(values, p_key=False, sep='.'):
result = []
for key, value in values.items():
new_key = p_key + sep + key if p_key else key
if type(value) == dict:
result.extend(flatten_dict(value, new_key).items())
else:
result.append((new_key, value))
return dict(result)
-def unflatten_dict(values):
- sep = '.'
+def unflatten_dict(values, sep='.'):
unflatten = {}
for path, value in values.items():
- keys = path.split('.')
+ keys = path.split(sep)
res = unflatten
while len(keys) > 1:
key = keys.pop(0)
if key not in res:
res[key] = {}
res = res[key]
key = keys.pop()
res[key] = value
return unflatten
def reps(values):
return tuple([el for el in values if values.count(el) > 1])

Любомир обнови решението на 23.03.2015 12:20 (преди над 9 години)

def extract_type(txt, data_type):
- return ''.join([el[0] * el[1] for el in txt if type(el[0]) == data_type])
+ return ''.join([str(x[0]) * x[1] for x in txt if type(x[0]) == data_type])
def reversed_dict(values):
return {value: key for key, value in values.items()}
def flatten_dict(values, p_key=False, sep='.'):
result = []
for key, value in values.items():
new_key = p_key + sep + key if p_key else key
if type(value) == dict:
result.extend(flatten_dict(value, new_key).items())
else:
result.append((new_key, value))
return dict(result)
def unflatten_dict(values, sep='.'):
unflatten = {}
for path, value in values.items():
keys = path.split(sep)
res = unflatten
while len(keys) > 1:
key = keys.pop(0)
if key not in res:
res[key] = {}
res = res[key]
key = keys.pop()
res[key] = value
return unflatten
def reps(values):
return tuple([el for el in values if values.count(el) > 1])