Решение на Пет малки функции от Дияна Кръстева

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

Към профила на Дияна Кръстева

Резултати

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

Код

def reversed_dict(d):
new_dict = {}
for key in d:
new_dict[d[key]]= key
return (new_dict)
def extract_type(a, b):
result = []
for item in a:
if type(item[0]) == b:
result.append(str(item[0])*item[1])
return("".join(result))
def reps(a):
c = list(set(a))
result = list(a)
for element in c:
for item in result:
if item == element:
result.remove(item)
break
r =()
for item in a:
if item in result:
r += (item,)
return (r)
def unflatten_dict(d):
new_d = {}
for key in d:
list = key.split(".")
if len(list) == 1:
new_d[key] = d[key]
else:
previous_d = new_d
for element in list:
if list.index(element) == len(list) - 1:
previous_d[element] = d[key]
else:
if element not in previous_d:
previous_d[element] = {}
previous_d = previous_d[element]
return new_d
def flatten_dict(d):
new_d = {}
for key, value in d.items():
if type(value) != dict:
new_d[key] = value
else:
for skey in value:
if type(value[skey]) != dict:
new_d[key + "." + skey] = value[skey]
else:
for tkey in value[skey]:
if type(value[skey][tkey]) != dict:
new_d[key + "." + skey+"."+tkey] = value[skey][tkey]
else:
new_d[key + "." + skey+"."+tkey] = flatten_dict(value[skey][tkey])
return new_d

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.131s

OK

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

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

+def reversed_dict(d):
+ new_dict = {}
+ for key in d:
+ new_dict[d[key]]= key
+ return (new_dict)
+
+def extract_type(a, b):
+ result = []
+ for item in a:
+ if type(item[0]) == b:
+ result.append(str(item[0])*item[1])
+ return("".join(result))
+
+def reps(a):
+ c = list(set(a))
+ result = list(a)
+ for element in c:
+ for item in result:
+ if item == element:
+ result.remove(item)
+ break
+ r =()
+ for item in a:
+ if item in result:
+ r += (item,)
+ return (r)
+
+def unflatten_dict(d):
+ new_d = {}
+ for key in d:
+ list = key.split(".")
+ if len(list) == 1:
+ new_d[key] = d[key]
+ else:
+ previous_d = new_d
+ for element in list:
+ if list.index(element) == len(list) - 1:
+ previous_d[element] = d[key]
+ else:
+ if element not in previous_d:
+ previous_d[element] = {}
+ previous_d = previous_d[element]
+ return new_d
+
+def flatten_dict(d):
+ new_d = {}
+ for key, value in d.items():
+ if type(value) != dict:
+ new_d[key] = value
+ else:
+ for skey in value:
+ if type(value[skey]) != dict:
+ new_d[key + "." + skey] = value[skey]
+ else:
+ for tkey in value[skey]:
+ if type(value[skey][tkey]) != dict:
+ new_d[key + "." + skey+"."+tkey] = value[skey][tkey]
+ else:
+ new_d[key + "." + skey+"."+tkey] = flatten_dict(value[skey][tkey])
+ return new_d

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

def reversed_dict(d):
new_dict = {}
for key in d:
new_dict[d[key]]= key
return (new_dict)
def extract_type(a, b):
result = []
for item in a:
- if type(item[0]) == b:
- result.append(str(item[0])*item[1])
+ if type(item[0]) == b:
+ result.append(str(item[0])*item[1])
return("".join(result))
def reps(a):
c = list(set(a))
result = list(a)
for element in c:
for item in result:
if item == element:
result.remove(item)
break
r =()
for item in a:
if item in result:
r += (item,)
return (r)
def unflatten_dict(d):
new_d = {}
for key in d:
list = key.split(".")
if len(list) == 1:
new_d[key] = d[key]
else:
previous_d = new_d
for element in list:
if list.index(element) == len(list) - 1:
previous_d[element] = d[key]
else:
if element not in previous_d:
previous_d[element] = {}
previous_d = previous_d[element]
return new_d
def flatten_dict(d):
new_d = {}
for key, value in d.items():
if type(value) != dict:
new_d[key] = value
else:
for skey in value:
if type(value[skey]) != dict:
new_d[key + "." + skey] = value[skey]
else:
for tkey in value[skey]:
if type(value[skey][tkey]) != dict:
new_d[key + "." + skey+"."+tkey] = value[skey][tkey]
else:
new_d[key + "." + skey+"."+tkey] = flatten_dict(value[skey][tkey])
return new_d