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

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

Към профила на Момчил Сулов

Резултати

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

Код

def extract_type(text, given_type):
result = ''
for value, count in text:
if type(value) is given_type:
while count > 0:
result = result + str(value)
count -= 1
return result
def reversed_dict(original):
result = {}
for key in original:
result[original[key]] = key
return result
def any_dict(given):
for key in given:
if type(given[key]) is dict:
return True
return False
def flatten_dict(given):
while any_dict(given):
result = {}
for key in given:
if type(given[key]) is dict:
for k in given[key]:
result[key + '.' + k] = given[key][k]
else:
result[key] = given[key]
given = result
return result
def has_point(word):
index = 0
length = len(word)
while index < length:
if word[index] == '.':
return True
else:
index += 1
return False
def any_with_point(given):
for key in given:
if has_point(key):
return True
return False
def split(word):
index = -1
while word[index] != '.':
index -= 1
suffix = word[index + 1:]
prefix = word[:index]
return (prefix, suffix)
def unflatten_dict(given):
while any_with_point(given):
result = {}
for key in given:
if not has_point(key):
result[key] = given[key]
else:
couple = split(key)
if couple[0] not in result:
result[couple[0]] = {}
result[couple[0]][couple[1]] = given[key]
given = result
return result
def reps(given):
result = [x for x in given if given.count(x) > 1]
return tuple(result)

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

...................
----------------------------------------------------------------------
Ran 19 tests in 0.141s

OK

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

Момчил обнови решението на 22.03.2015 23:00 (преди над 9 години)

+def extract_type(text, given_type):
+ result = ''
+ for value, count in text:
+ if type(value) is given_type:
+ while count > 0:
+ result = result + str(value)
+ count -= 1
+ return result
+
+
+def reversed_dict(original):
+ result = {}
+ for key in original:
+ result[original[key]] = key
+ return result
+
+
+def any_dict(given):
+ for key in given:
+ if type(given[key]) is dict:
+ return True
+ return False
+
+
+def flatten_dict(given):
+ while any_dict(given):
+ result = {}
+ for key in given:
+ if type(given[key]) is dict:
+ for k in given[key]:
+ result[key + '.' + k] = given[key][k]
+ else:
+ result[key] = given[key]
+ given = result
+ return result
+
+
+def has_point(word):
+ index = 0
+ length = len(word)
+ while index < length:
+ if word[index] == '.':
+ return True
+ else:
+ index += 1
+ return False
+
+
+def any_with_point(given):
+ for key in given:
+ if has_point(key):
+ return True
+ return False
+
+
+def split(word):
+ index = -1
+ while word[index] != '.':
+ index -= 1
+ suffix = word[index + 1:]
+ prefix = word[:index]
+ return (prefix, suffix)
+
+
+def unflatten_dict(given):
+ while any_with_point(given):
+ result = {}
+ for key in given:
+ if not has_point(key):
+ result[key] = given[key]
+ else:
+ couple = split(key)
+ if couple[0] not in result:
+ result[couple[0]] = {}
+ result[couple[0]][couple[1]] = given[key]
+ given = result
+ return result
+
+
+def reps(given):
+ result = [x for x in given if given.count(x) > 1]
+ return tuple(result)