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

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

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

Резултати

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

Код

def append_item(tuple, type):
result = ''
if isinstance(tuple[0], type):
for i in range(0, tuple[1]):
result += str(tuple[0])
return result
def extract_type(list, type):
result = ''
for tuple in list:
result += append_item(tuple, type)
return result
def reversed_dict(d):
new_dict = {}
for key in d:
new_dict[d[key]] = key
return new_dict
def flatten_key(key, value):
if isinstance(value, dict):
return [(key + '.' + k, v) for k, v in flatten_dict(value).items()]
else:
return [(key, value)]
def flatten_dict(d):
items = [item for k, v in d.items() for item in flatten_key(k, v)]
return dict(items)
def append_key(keys, value, help_dict):
for key in keys[:-1]:
if key not in help_dict:
help_dict[key] = {}
help_dict = help_dict[key]
help_dict[keys[-1]] = value
def unflatten_dict(d):
new_dict = {}
for key, value in d.items():
help_dict = new_dict
keys = key.split('.')
append_key(keys, value, help_dict)
return new_dict
def reps(arr):
result = []
for item in arr:
if arr.count(item) > 1:
result.append(item)
return tuple(result)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.133s

OK

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

Виктор обнови решението на 23.03.2015 16:43 (преди над 9 години)

+def append_item(tuple, type):
+ result = ''
+ if isinstance(tuple[0], type):
+ for i in range(0, tuple[1]):
+ result += str(tuple[0])
+ return result
+
+
+def extract_type(list, type):
+ result = ''
+ for tuple in list:
+ result += append_item(tuple, type)
+ return result
+
+
+def reversed_dict(d):
+ new_dict = {}
+ for key in d:
+ new_dict[d[key]] = key
+ return new_dict
+
+
+def flatten_key(key, value):
+ if isinstance(value, dict):
+ return [(key + '.' + k, v) for k, v in flatten_dict(value).items()]
+ else:
+ return [(key, value)]
+
+
+def flatten_dict(d):
+ items = [item for k, v in d.items() for item in flatten_key(k, v)]
+ return dict(items)
+
+
+def append_key(keys, value, help_dict):
+ for key in keys[:-1]:
+ if key not in help_dict:
+ help_dict[key] = {}
+ help_dict = help_dict[key]
+ help_dict[keys[-1]] = value
+
+
+def unflatten_dict(d):
+ new_dict = {}
+ for key, value in d.items():
+ help_dict = new_dict
+ keys = key.split('.')
+ append_key(keys, value, help_dict)
+ return new_dict
+
+
+def reps(arr):
+ result = []
+ for item in arr:
+ if arr.count(item) > 1:
+ result.append(item)
+ return tuple(result)