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

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

Към профила на Димитрина Гецкова

Резултати

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

Код

def extract_type(all_obj, type_to_return):
result = "" # po skoror strin
for x in all_obj:
if type(x[0]) is type_to_return:
result += (str(x[0]) * x[1])
return result
def reversed_dict(obj):
result = {}
for x in obj:
result[obj[x]] = x
return result
def reps(obj):
return tuple([x for x in obj if obj.count(x) is not 1])
def unflatten_dict(obj):
obj_new = {}
for key, value in obj.items():
parts = key.split(".")
to_work = obj_new
for part in parts[:-1]:
if part not in to_work:
to_work[part] = {}
to_work = to_work[part]
to_work[parts[-1]] = value
return obj_new
def flatten_dict(obj):
# one level
new_obj = {}
for elem in obj:
if is_dict(obj[elem]):
path = str(elem)
for e in obj[elem]:
path += "."
path += str(e)
print (type(e))
new_obj[path] = obj[elem][e]
###
path_global = path
one_flatten()
else:
new_obj[elem] = obj[elem]
return new_obj
path_global = ""
def flatten_dont_work(obj):
global path_global
new_obj = {}
for elem in obj:
if is_dict(obj[elem]):
path_global += elem
path_global += "."
flatten_last(obj[elem])
else:
new_obj[path_global] = obj[elem]
path_global = path_global[:-1]
path_global = path_global[::-1]
path_global = path_global[path_global.index(".") + 1:]
path_global = pathglobal[::-1]
return new_obj

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

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

OK

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

Димитрина обнови решението на 23.03.2015 16:34 (преди над 9 години)

+def extract_type(all_obj, type_to_return):
+ result = "" # po skoror strin
+ for x in all_obj:
+ if type(x[0]) is type_to_return:
+ result += (str(x[0]) * x[1])
+ return result
+
+
+def reversed_dict(obj):
+ result = {}
+ for x in obj:
+ result[obj[x]] = x
+
+ return result
+
+
+def reps(obj):
+ return tuple([x for x in obj if obj.count(x) is not 1])
+
+
+def unflatten_dict(obj):
+ obj_new = {}
+ for key, value in obj.items():
+ parts = key.split(".")
+ to_work = obj_new
+ for part in parts[:-1]:
+ if part not in to_work:
+ to_work[part] = {}
+ to_work = to_work[part]
+ to_work[parts[-1]] = value
+ return obj_new
+
+
+def flatten_dict(obj):
+ # one level
+ new_obj = {}
+ for elem in obj:
+ if is_dict(obj[elem]):
+ path = str(elem)
+ for e in obj[elem]:
+ path += "."
+ path += str(e)
+ print (type(e))
+ new_obj[path] = obj[elem][e]
+ ###
+ path_global = path
+ one_flatten()
+ else:
+ new_obj[elem] = obj[elem]
+ return new_obj
+
+
+path_global = ""
+
+
+def flatten_dont_work(obj):
+ global path_global
+ new_obj = {}
+ for elem in obj:
+ if is_dict(obj[elem]):
+ path_global += elem
+ path_global += "."
+ flatten_last(obj[elem])
+ else:
+ new_obj[path_global] = obj[elem]
+ path_global = path_global[:-1]
+ path_global = path_global[::-1]
+ path_global = path_global[path_global.index(".") + 1:]
+ path_global = pathglobal[::-1]
+
+ return new_obj