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

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

Към профила на Йордан Русев

Резултати

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

Код

def extract_type(record, argument):
elements = ""
for element in record:
if isinstance(element[0], argument):
for i in range(element[1]):
elements += element[0]
return elements
def reversed_dict(hash_table):
result = {}
for name, value in hash_table.items():
result[value] = name
return result
def flatten_dict(hash_table, name0=""):
result = {}
for name, value in hash_table.items():
name1 = name0 + name
if not isinstance(value, dict):
result[name1] = value
else:
result.update(flatten_dict(value, name1 + '.'))
return result
def unflatten_dict(hash_table):
result = {}
for name, value in hash_table.items():
counter = 1
n = len(name)
name2 = ''
for i in range(n):
if name[i] == '.':
name3 = ''
j = i + 1
while j < n:
name3 += name[j]
j = j + 1
try:
result[name2]
except KeyError:
result[name2] = {}
result[name2].update(unflatten_dict({name3: value}))
counter = 0
elif counter:
name2 += name[i]
if counter:
result.update({name2: value})
return result
def reps(record):
result = []
for element in record:
if record.count(element) > 1:
result.append(element)
new_result = tuple(result)
return new_result

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.137s

OK

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

Йордан обнови решението на 23.03.2015 16:35 (преди над 9 години)

+def extract_type(record, argument):
+ elements = ""
+ for element in record:
+ if isinstance(element[0], argument):
+ for i in range(element[1]):
+ elements += element[0]
+ return elements
+
+
+def reversed_dict(hash_table):
+ result = {}
+ for name, value in hash_table.items():
+ result[value] = name
+ return result
+
+
+def flatten_dict(hash_table, name0=""):
+ result = {}
+ for name, value in hash_table.items():
+ name1 = name0 + name
+ if not isinstance(value, dict):
+ result[name1] = value
+ else:
+ result.update(flatten_dict(value, name1 + '.'))
+ return result
+
+
+def unflatten_dict(hash_table):
+ result = {}
+ for name, value in hash_table.items():
+ counter = 1
+ n = len(name)
+ name2 = ''
+ for i in range(n):
+ if name[i] == '.':
+ name3 = ''
+ j = i + 1
+ while j < n:
+ name3 += name[j]
+ j = j + 1
+ try:
+ result[name2]
+ except KeyError:
+ result[name2] = {}
+ result[name2].update(unflatten_dict({name3: value}))
+ counter = 0
+ elif counter:
+ name2 += name[i]
+ if counter:
+ result.update({name2: value})
+ return result
+
+
+def reps(record):
+ result = []
+ for element in record:
+ if record.count(element) > 1:
+ result.append(element)
+ new_result = tuple(result)
+ return new_result