Решение на Пет малки функции от Станислав Венков

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

Към профила на Станислав Венков

Резултати

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

Код

def extract_type(elements, return_type):
result = ''
for element in elements:
if type(element[0]) is return_type:
result += str(element[0])*element[1]
return result
def reversed_dict(elements):
result = {}
for key, value in elements.items():
result[value] = key
return result
def flatten_dict(elements):
result = {}
for key, value in elements.items():
keys = ''
result.update(concat_keys(key, value, keys, result))
return result
def concat_keys(key, value, keys, result):
keys += key
if type(value) is dict:
keys += '.'
temp_element = ''
for i_key, i_value in value.items():
temp_element = concat_keys(i_key, i_value, keys, result)
result.update(temp_element)
return temp_element
else:
return {keys: value}
def unflatten_dict(elements):
result = {}
for_unflat = {}
for key, value in elements.items():
new_keys = key.split('.')
if len(new_keys) > 1:
for internal_key, internal_value in elements.items():
internal_new_keys = internal_key.split('.')
if new_keys[0] == internal_new_keys[0]:
if not for_unflat.get(new_keys[0]):
for_unflat.update({new_keys[0]: {}})
joined_keys = '.'.join(new_keys[1:])
for_unflat[new_keys[0]].update({joined_keys: value})
else:
result[key] = value
for key, value in for_unflat.items():
result.update({key: unflatten_dict(value)})
return result
def reps(elements):
remove_items = []
if type(elements) is tuple:
elements = list(elements)
for element in elements:
if elements.count(element) == 1:
remove_items.append(element)
return tuple([item for item in elements if item not in remove_items])

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.134s

OK

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

Станислав обнови решението на 20.03.2015 23:19 (преди над 9 години)

+def extract_type(elements, return_type):
+ result = ''
+ for element in elements:
+ if type(element[0]) is return_type:
+ result += str(element[0])*element[1]
+ return result
+
+
+def reversed_dict(elements):
+ result = {}
+ for key, value in elements.items():
+ result[value] = key
+ return result
+
+
+def flatten_dict(elements):
+ result = {}
+ for key, value in elements.items():
+ keys = ''
+ result.update(concat_keys(key, value, keys, result))
+ return result
+
+
+def concat_keys(key, value, keys, result):
+ keys += key
+ if type(value) is dict:
+ keys += '.'
+ temp_element = ''
+ for i_key, i_value in value.items():
+ temp_element = concat_keys(i_key, i_value, keys, result)
+ result.update(temp_element)
+ return temp_element
+ else:
+ return {keys: value}
+
+
+def reps(elements):
+
+ if type(elements) is tuple:
+ elements = list(elements)
+
+ remove_items = []
+ for element in elements:
+ if elements.count(element) == 1:
+ remove_items.append(element)
+
+ return tuple([item for item in elements if item not in remove_items])

Станислав обнови решението на 21.03.2015 20:47 (преди над 9 години)

def extract_type(elements, return_type):
result = ''
+
for element in elements:
if type(element[0]) is return_type:
result += str(element[0])*element[1]
+
return result
def reversed_dict(elements):
result = {}
+
for key, value in elements.items():
result[value] = key
+
return result
def flatten_dict(elements):
result = {}
+
for key, value in elements.items():
keys = ''
result.update(concat_keys(key, value, keys, result))
+
return result
def concat_keys(key, value, keys, result):
keys += key
+
if type(value) is dict:
keys += '.'
temp_element = ''
for i_key, i_value in value.items():
temp_element = concat_keys(i_key, i_value, keys, result)
result.update(temp_element)
return temp_element
else:
return {keys: value}
+def unflatten_dict(elements):
+ result = {}
+ for_unflat = {}
+
+ for key, value in elements.items():
+ new_keys = key.split('.')
+ if len(new_keys) > 1:
+ for internal_key, internal_value in elements.items():
+ internal_new_keys = internal_key.split('.')
+ if new_keys[0] == internal_new_keys[0]:
+ if not for_unflat.get(new_keys[0]):
+ for_unflat.update({new_keys[0]: {}})
+ joined_keys = '.'.join(new_keys[1:])
+ for_unflat[new_keys[0]].update({joined_keys: value})
+ else:
+ result[key] = value
+
+ for key, value in for_unflat.items():
+ result.update({key: unflatten_dict(value)})
+
+ return result
+
+
def reps(elements):
+ remove_items = []
if type(elements) is tuple:
elements = list(elements)
- remove_items = []
for element in elements:
if elements.count(element) == 1:
remove_items.append(element)
return tuple([item for item in elements if item not in remove_items])