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

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

Към профила на Галин Ангелов

Резултати

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

Код

def extract_type(elements, typeof):
result = ""
for element in elements:
if type(element[0]) == typeof:
result += str(element[0])*element[1]
return result
def reversed_dict(dictionary):
new_dict = {}
for key in dictionary:
new_dict[dictionary[key]] = key
return new_dict
def flatten_dict(dictionary, lkey=''):
result = {}
for key, val in dictionary.items():
key = lkey + key
if type(val) == dict:
result.update(flatten_dict(val, key+'.'))
else:
result[key] = val
return result
def unflatten_dict(dictionary):
result = dict()
for key, value in dictionary.items():
parts = key.split(".")
diction = result
for part in parts[:-1]:
if part not in diction:
diction[part] = dict()
diction = diction[part]
diction[parts[-1]] = value
return result
def reps(list):
return tuple(filter(lambda x: x in list[list.index(x)+1:], list))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.132s

OK

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

Галин обнови решението на 22.03.2015 13:23 (преди над 9 години)

+def extract_type(elements, typeof):
+ result = ""
+ for element in elements:
+ if type(element[0]) == typeof:
+ result += str(element[0])*element[1]
+ return result
+
+
+def reversed_dict(dictionary):
+ new_dict = {}
+ for key in dictionary:
+ new_dict[dictionary[key]] = key
+ return new_dict
+
+
+def flatten_dict(dictionary, lkey=''):
+ result = {}
+ for key, val in dictionary.items():
+ key = lkey + key
+ if type(val) == dict:
+ result.update(flatten_dict(val, key+'.'))
+ else:
+ result[key] = val
+ return result
+
+
+def unflatten_dict(dictionary):
+ result = dict()
+ for key, value in dictionary.items():
+ parts = key.split(".")
+ diction = result
+ for part in parts[:-1]:
+ if part not in diction:
+ diction[part] = dict()
+ diction = diction[part]
+ diction[parts[-1]] = value
+ return result
+
+
+def reps(list):
+ return tuple(filter(lambda x: x in list[list.index(x)+1:], list))