Решение на Пет малки функции от Мартина Тонковска

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

Към профила на Мартина Тонковска

Резултати

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

Код

def extract_type(element_list, characters):
word = ''
for element in element_list:
if type(element[0]) is characters:
for i in range(element[1]):
word = word + str(element[0])
return word
def reversed_dict(dictionary):
return dict(map(lambda x: (dictionary[x], x), dictionary.keys()))
def modify_keys(dictionary, k):
if len(dictionary) == 0:
return {}
else:
(key, value) = dictionary.popitem()
return dict({str(k + '.' + key): value}, **modify_keys(dictionary, k))
def flatten_dict(dictionary):
if len(dictionary) == 0:
return {}
else:
(key, value) = dictionary.popitem()
if type(value) is not dict:
return dict(flatten_dict(dictionary), **{key: value})
else:
return dict(flatten_dict(dictionary),
**flatten_dict(modify_keys(value, key)))
def merge_dicts(a, b):
c = a.copy()
for key in b:
if key in c:
c.update({key: merge_dicts(a[key], b[key])})
else:
c[key] = b[key]
return c
def unflatten_dict(dictionary):
if len(dictionary) == 0:
return {}
else:
(key, value) = dictionary.popitem()
if key.count('.') == 0:
return dict(unflatten_dict(dictionary), **{key: value})
else:
temp = dict({key[:key.find('.')]:
unflatten_dict({key[key.find('.') + 1:]: value})})
return merge_dicts(unflatten_dict(dictionary), temp)
def reps(element_list):
return tuple(filter(lambda x: element_list.count(x) > 1, element_list))

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.140s

OK

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

Мартина обнови решението на 23.03.2015 01:27 (преди около 9 години)

+def extract_type(element_list, characters):
+ word = ''
+ for element in element_list:
+ if type(element[0]) is characters:
+ for i in range(element[1]):
+ word = word + str(element[0])
+ return word
+
+
+def reversed_dict(dictionary):
+ return dict(map(lambda x: (dictionary[x], x), dictionary.keys()))
+
+
+def modify_keys(dictionary, k):
+ if len(dictionary) == 0:
+ return {}
+ else:
+ (key, value) = dictionary.popitem()
+ return dict({str(k + '.' + key): value}, **modify_keys(dictionary, k))
+
+
+def flatten_dict(dictionary):
+ if len(dictionary) == 0:
+ return {}
+ else:
+ (key, value) = dictionary.popitem()
+ if type(value) is not dict:
+ return dict(flatten_dict(dictionary), **{key: value})
+ else:
+ return dict(flatten_dict(dictionary),
+ **flatten_dict(modify_keys(value, key)))
+
+
+def merge_dicts(a, b):
+ c = a.copy()
+ for key in b:
+ if key in c:
+ c.update({key: merge_dicts(a[key], b[key])})
+ else:
+ c[key] = b[key]
+ return c
+
+
+def unflatten_dict(dictionary):
+ if len(dictionary) == 0:
+ return {}
+ else:
+ (key, value) = dictionary.popitem()
+ if key.count('.') == 0:
+ return dict(unflatten_dict(dictionary), **{key: value})
+ else:
+ temp = dict({key[:key.find('.')]:
+ unflatten_dict({key[key.find('.') + 1:]: value})})
+ return merge_dicts(unflatten_dict(dictionary), temp)
+
+
+def reps(element_list):
+ return tuple(filter(lambda x: element_list.count(x) > 1, element_list))