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

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

Към профила на Елица Илиева

Резултати

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

Код

def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def extract_type(a_list, a_type):
return ''.join([str(x[0]) * x[1]
for x in a_list if isinstance(x[0], a_type)])
def flatten_dict(dictionary):
def flatten(dictionary, key='', current={}):
for k, v in dictionary.items():
temp_key = key + '.' + str(k) if key else str(k)
if isinstance(v, dict):
current.update(flatten(v, temp_key, current))
else:
current[temp_key] = v
return current
return flatten(dictionary)
def unflatten_dict(dictionary):
unflattened = {}
for item in dictionary.items():
temporary_dict = unflattened
keys = str(item[0]).split('.')
for key in keys[:-1]:
if temporary_dict.get(key, False):
temporary_dict = temporary_dict[key]
else:
temporary_dict.update({key: {}})
temporary_dict = temporary_dict[key]
temporary_dict[keys[-1]] = item[1]
return unflattened
def reps(a_list):
return tuple(elem for elem in a_list if a_list.count(elem) - 1)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.136s

OK

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

Елица обнови решението на 20.03.2015 00:34 (преди над 9 години)

+def reversed_dict(dictionary):
+ return dict(zip(dictionary.values(), dictionary.keys()))
+
+
+def extract_type(a_list, a_type):
+ return ''.join([x[0] * x[1] for x in a_list if isinstance(x[0], a_type)])
+
+
+def flatten_dict(dictionary):
+ def flatten(dictionary, key='', current={}):
+ for k, v in dictionary.items():
+ if isinstance(v, dict):
+ current.update(flatten(v, key + '.' + k if key else k,
+ current))
+ else:
+ current[key + '.' + k if key else k] = v
+ return current
+ return flatten(dictionary)
+
+
+def unflatten_dict(dictionary):
+ unflattened = {}
+ for item in dictionary.items():
+ temporary_dict = unflattened
+ keys = item[0].split('.')
+ for key in keys[:-1]:
+ if temporary_dict.get(key, False):
+ temporary_dict = temporary_dict[key]
+ else:
+ temporary_dict.update({key: {}})
+ temporary_dict = temporary_dict[key]
+ temporary_dict[keys[-1]] = item[1]
+ return unflattened
+
+
+def reps(a_list):
+ return tuple(elem for elem in a_list if a_list.count(elem) - 1)

Елица обнови решението на 23.03.2015 12:15 (преди над 9 години)

def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def extract_type(a_list, a_type):
- return ''.join([x[0] * x[1] for x in a_list if isinstance(x[0], a_type)])
+ return ''.join([str(x[0]) * x[1]
+ for x in a_list if isinstance(x[0], a_type)])
def flatten_dict(dictionary):
def flatten(dictionary, key='', current={}):
for k, v in dictionary.items():
if isinstance(v, dict):
current.update(flatten(v, key + '.' + k if key else k,
current))
else:
current[key + '.' + k if key else k] = v
return current
return flatten(dictionary)
def unflatten_dict(dictionary):
unflattened = {}
for item in dictionary.items():
temporary_dict = unflattened
keys = item[0].split('.')
for key in keys[:-1]:
if temporary_dict.get(key, False):
temporary_dict = temporary_dict[key]
else:
temporary_dict.update({key: {}})
temporary_dict = temporary_dict[key]
temporary_dict[keys[-1]] = item[1]
return unflattened
def reps(a_list):
return tuple(elem for elem in a_list if a_list.count(elem) - 1)

Елица обнови решението на 23.03.2015 13:38 (преди над 9 години)

def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def extract_type(a_list, a_type):
return ''.join([str(x[0]) * x[1]
for x in a_list if isinstance(x[0], a_type)])
def flatten_dict(dictionary):
def flatten(dictionary, key='', current={}):
for k, v in dictionary.items():
+ temp_key = key + '.' + str(k) if key else str(k)
if isinstance(v, dict):
current.update(flatten(v, key + '.' + k if key else k,
current))
else:
current[key + '.' + k if key else k] = v
return current
return flatten(dictionary)
def unflatten_dict(dictionary):
unflattened = {}
for item in dictionary.items():
temporary_dict = unflattened
- keys = item[0].split('.')
+ keys = str(item[0]).split('.')
for key in keys[:-1]:
if temporary_dict.get(key, False):
temporary_dict = temporary_dict[key]
else:
temporary_dict.update({key: {}})
temporary_dict = temporary_dict[key]
temporary_dict[keys[-1]] = item[1]
return unflattened
def reps(a_list):
return tuple(elem for elem in a_list if a_list.count(elem) - 1)

Елица обнови решението на 23.03.2015 16:46 (преди над 9 години)

def reversed_dict(dictionary):
return dict(zip(dictionary.values(), dictionary.keys()))
def extract_type(a_list, a_type):
return ''.join([str(x[0]) * x[1]
for x in a_list if isinstance(x[0], a_type)])
def flatten_dict(dictionary):
def flatten(dictionary, key='', current={}):
for k, v in dictionary.items():
temp_key = key + '.' + str(k) if key else str(k)
if isinstance(v, dict):
- current.update(flatten(v, key + '.' + k if key else k,
- current))
+ current.update(flatten(v, temp_key, current))
else:
- current[key + '.' + k if key else k] = v
+ current[temp_key] = v
return current
return flatten(dictionary)
def unflatten_dict(dictionary):
unflattened = {}
for item in dictionary.items():
temporary_dict = unflattened
keys = str(item[0]).split('.')
for key in keys[:-1]:
if temporary_dict.get(key, False):
temporary_dict = temporary_dict[key]
else:
temporary_dict.update({key: {}})
temporary_dict = temporary_dict[key]
temporary_dict[keys[-1]] = item[1]
return unflattened
def reps(a_list):
return tuple(elem for elem in a_list if a_list.count(elem) - 1)