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

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

Към профила на Цветан Иванов

Резултати

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

Код

def reps(elements):
result = []
for element in elements:
if elements.count(element) > 1:
result.append(element)
return tuple(result)
def extract_type(elements, seeked_type):
result = ''
for element in elements:
if type(element[0]) == seeked_type:
result += str(element[0]) * element[1]
return result
def reversed_dict(elements):
result = {}
for key in elements:
result[elements[key]] = key
return result
def _flatten_dict(outher_key, elements):
result = {}
for key in elements:
if(not type(elements[key]) == dict):
result[outher_key + key] = elements[key]
else:
current_key = outher_key + key + '.'
flatten = _flatten_dict(current_key, elements[key])
result.update(flatten)
return result
def flatten_dict(elements):
return _flatten_dict('', elements)
def add_unflatten_dict(splited_key, result, value):
if len(splited_key) == 1:
result[splited_key[0]] = value
return
if not splited_key[0] in result:
result[splited_key[0]] = {}
add_unflatten_dict(splited_key[1:], result[splited_key[0]], value)
def unflatten_dict(elements):
result = {}
for key in elements:
splited_key = key.split('.')
add_unflatten_dict(splited_key, result, elements[key])
return result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.170s

OK

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

Цветан обнови решението на 22.03.2015 17:38 (преди над 9 години)

+def reps(elements):
+ result = []
+
+ for element in elements:
+ if elements.count(element) > 1:
+ result.append(element)
+
+ return tuple(result)
+
+
+def extract_type(elements, seekedType):
+ result = ''
+
+ for element in elements:
+ if type(element[0]) == seekedType:
+ result += str(element[0]) * element[1]
+
+ return result
+
+
+def reversed_dict(elements):
+ result = {}
+
+ for key in elements:
+ result[elements[key]] = key
+
+ return result
+
+
+def flatten_dict_with_start_key(outher_key, elements):
+ result = {}
+
+ for key in elements:
+ if(not type(elements[key]) == dict):
+ result[outher_key + key] = elements[key]
+ else:
+ current_key = outher_key + key + '.'
+ flatten = flatten_dict_with_start_key(current_key, elements[key])
+ result.update(flatten)
+
+ return result
+
+
+def flatten_dict(elements):
+ return flatten_dict_with_start_key("", elements)
+
+
+def add_unflatten_dict(splited_key, result, value):
+ if len(splited_key) == 1:
+ result[splited_key[0]] = value
+ return
+
+ if not splited_key[0] in result:
+ result[splited_key[0]] = {}
+
+ add_unflatten_dict(splited_key[1:], result[splited_key[0]], value)
+
+
+def unflatten_dict(elements):
+ result = {}
+
+ for key in elements:
+ splited_key = key.split('.')
+ add_unflatten_dict(splited_key, result, elements[key])
+
+ return result

Цветан обнови решението на 23.03.2015 10:32 (преди над 9 години)

def reps(elements):
result = []
for element in elements:
if elements.count(element) > 1:
result.append(element)
return tuple(result)
-def extract_type(elements, seekedType):
+def extract_type(elements, seeked_type):
result = ''
for element in elements:
- if type(element[0]) == seekedType:
+ if type(element[0]) == seeked_type:
result += str(element[0]) * element[1]
return result
def reversed_dict(elements):
result = {}
for key in elements:
result[elements[key]] = key
return result
-def flatten_dict_with_start_key(outher_key, elements):
+def _flatten_dict(outher_key, elements):
result = {}
for key in elements:
if(not type(elements[key]) == dict):
result[outher_key + key] = elements[key]
else:
current_key = outher_key + key + '.'
- flatten = flatten_dict_with_start_key(current_key, elements[key])
+ flatten = _flatten_dict(current_key, elements[key])
result.update(flatten)
return result
def flatten_dict(elements):
- return flatten_dict_with_start_key("", elements)
+ return _flatten_dict('', elements)
def add_unflatten_dict(splited_key, result, value):
if len(splited_key) == 1:
result[splited_key[0]] = value
return
if not splited_key[0] in result:
result[splited_key[0]] = {}
add_unflatten_dict(splited_key[1:], result[splited_key[0]], value)
def unflatten_dict(elements):
result = {}
for key in elements:
splited_key = key.split('.')
add_unflatten_dict(splited_key, result, elements[key])
return result