Решение на Генератори и итератори от Виктория Терзиева

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

Към профила на Виктория Терзиева

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 13 успешни тест(а)
  • 1 неуспешни тест(а)

Код

from math import sqrt
from math import floor
def fibonacci():
current = 1
next_ = 1
while True:
yield current
temporary = current
current = next_
next_ += temporary
def is_prime(candidate, is_divider):
if is_divider == 1:
return True
elif candidate % is_divider == 0:
return False
else:
return is_prime(candidate, is_divider - 1)
def primes():
number = 2
while True:
yield number
number += 1
while is_prime(number, floor(sqrt(number))) is False:
number += 1
def alphabet(letters=0, code=0):
if code == 'lat' and letters is 0:
latin_letter_code = 97
while latin_letter_code < 123:
yield chr(latin_letter_code)
latin_letter_code += 1
elif code == 'bg' and letters is 0:
bulgarian_letter_code = 1072
while bulgarian_letter_code < 1104:
if bulgarian_letter_code == 1099 or bulgarian_letter_code == 1101:
bulgarian_letter_code += 1
yield chr(bulgarian_letter_code)
bulgarian_letter_code += 1
else:
next_letter = 0
while next_letter < len(letters):
yield letters[next_letter]
next_letter += 1
def intertwined_sequences(argument, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci, 'primes': primes})
generators = {}
flag = False
for arg in argument:
while arg['length'] > 0:
arg['length'] -= 1
if arg['sequence'] == 'alphabet':
if 'code' in arg.keys() and flag is False:
flag = True
it = alphabet(code=arg['code'])
elif 'letters' in arg.keys() and flag is False:
it = alphabet(letters=arg['letters'])
flag = True
yield next(it)
else:
args_for_function = {}
if len(arg) > 2:
for key, value in arg.items():
if not key == 'sequence' and not key == 'length':
args_for_function.update({key: value})
if not arg['sequence'] in generators.keys():
generators[arg['sequence']] = iter(
generator_definitions[arg['sequence']](
**args_for_function))
yield next(generators[arg['sequence']])

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

.E............
======================================================================
ERROR: test_endless_letters_generator (test.TestAlphabet)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 2.390s

FAILED (errors=1)

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

Виктория обнови решението на 03.04.2015 01:37 (преди над 9 години)

+def fibonacci():
+ current = 1
+ next = 1
+ while True:
+ yield current
+ temporary = current
+ current = next
+ next += temporary
+
+
+def is_prime(candidate, is_divider):
+ if is_divider == 1:
+ return True
+ elif candidate % is_divider == 0:
+ return False
+ else:
+ return is_prime(candidate, is_divider - 1)
+
+
+def primes():
+ number = 2
+ while True:
+ yield number
+ number = number + 1
+ while is_prime(number, floor(sqrt(number))) is False:
+ number = number + 1
+
+
+def alphabet(letters=0, code=0):
+ if code == 'lat' and letters is 0:
+ latin_letter_code = 97
+ while latin_letter_code < 123:
+ yield chr(latin_letter_code)
+ latin_letter_code += 1
+ elif code == 'bg' and letters is 0:
+ bulgarian_letter_code = 1072
+ while bulgarian_letter_code < 1104:
+ if bulgarian_letter_code == 1099 or bulgarian_letter_code == 1101:
+ bulgarian_letter_code += 1
+ yield chr(bulgarian_letter_code)
+ bulgarian_letter_code += 1
+ else:
+ next_letter = 0
+ while next_letter < len(letters):
+ yield letters[next_letter]
+ next_letter += 1
+
+
+def intertwined_sequences(argument, generator_definitions=0):
+ generator_definitions.update({'fibonacci': fibonacci, 'primes': primes})
+ generators = {}
+ for key, value in generator_definitions.items():
+ generators[key] = value()
+ flag = False
+
+ for arg in argument:
+ while arg['length'] > 0:
+ arg['length'] -= 1
+ if arg['sequence'] == 'alphabet':
+ if 'code' in arg.keys() and flag is False:
+ flag = True
+ it = alphabet(code=arg['code'])
+ elif 'letters' in arg.keys() and flag is False:
+ it = alphabet(letters=arg['letters'])
+ flag = True
+ yield next(it)
+ else:
+ yield next(generators[arg['sequence']])

Виктория обнови решението на 03.04.2015 02:11 (преди над 9 години)

+from math import sqrt
+from math import floor
+
+
def fibonacci():
current = 1
next = 1
while True:
yield current
temporary = current
current = next
next += temporary
def is_prime(candidate, is_divider):
if is_divider == 1:
return True
elif candidate % is_divider == 0:
return False
else:
return is_prime(candidate, is_divider - 1)
def primes():
number = 2
while True:
yield number
number = number + 1
while is_prime(number, floor(sqrt(number))) is False:
number = number + 1
def alphabet(letters=0, code=0):
if code == 'lat' and letters is 0:
latin_letter_code = 97
while latin_letter_code < 123:
yield chr(latin_letter_code)
latin_letter_code += 1
elif code == 'bg' and letters is 0:
bulgarian_letter_code = 1072
while bulgarian_letter_code < 1104:
if bulgarian_letter_code == 1099 or bulgarian_letter_code == 1101:
bulgarian_letter_code += 1
yield chr(bulgarian_letter_code)
bulgarian_letter_code += 1
else:
next_letter = 0
while next_letter < len(letters):
yield letters[next_letter]
next_letter += 1
-def intertwined_sequences(argument, generator_definitions=0):
+def intertwined_sequences(argument, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci, 'primes': primes})
generators = {}
- for key, value in generator_definitions.items():
- generators[key] = value()
flag = False
for arg in argument:
while arg['length'] > 0:
arg['length'] -= 1
if arg['sequence'] == 'alphabet':
if 'code' in arg.keys() and flag is False:
flag = True
it = alphabet(code=arg['code'])
elif 'letters' in arg.keys() and flag is False:
it = alphabet(letters=arg['letters'])
flag = True
yield next(it)
else:
+ args_for_function = {}
+ if len(arg) > 2:
+ for key, value in arg.items():
+ if not key == 'sequence' and not key == 'length':
+ args_for_function.update({key: value})
+ if not arg['sequence'] in generators.keys():
+ generators[arg['sequence']] = \
+ generator_definitions[arg['sequence']](**args_for_function)
yield next(generators[arg['sequence']])

Виктория обнови решението на 03.04.2015 14:47 (преди над 9 години)

from math import sqrt
from math import floor
def fibonacci():
current = 1
next = 1
while True:
yield current
temporary = current
current = next
next += temporary
def is_prime(candidate, is_divider):
if is_divider == 1:
return True
elif candidate % is_divider == 0:
return False
else:
return is_prime(candidate, is_divider - 1)
def primes():
number = 2
while True:
yield number
number = number + 1
while is_prime(number, floor(sqrt(number))) is False:
number = number + 1
def alphabet(letters=0, code=0):
if code == 'lat' and letters is 0:
latin_letter_code = 97
while latin_letter_code < 123:
yield chr(latin_letter_code)
latin_letter_code += 1
elif code == 'bg' and letters is 0:
bulgarian_letter_code = 1072
while bulgarian_letter_code < 1104:
if bulgarian_letter_code == 1099 or bulgarian_letter_code == 1101:
bulgarian_letter_code += 1
yield chr(bulgarian_letter_code)
bulgarian_letter_code += 1
else:
next_letter = 0
while next_letter < len(letters):
yield letters[next_letter]
next_letter += 1
def intertwined_sequences(argument, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci, 'primes': primes})
generators = {}
flag = False
for arg in argument:
while arg['length'] > 0:
arg['length'] -= 1
if arg['sequence'] == 'alphabet':
if 'code' in arg.keys() and flag is False:
flag = True
it = alphabet(code=arg['code'])
elif 'letters' in arg.keys() and flag is False:
it = alphabet(letters=arg['letters'])
flag = True
yield next(it)
else:
args_for_function = {}
if len(arg) > 2:
for key, value in arg.items():
if not key == 'sequence' and not key == 'length':
args_for_function.update({key: value})
if not arg['sequence'] in generators.keys():
- generators[arg['sequence']] = \
- generator_definitions[arg['sequence']](**args_for_function)
+ generators[arg['sequence']] = iter(
+ generator_definitions[arg['sequence']](
+ **args_for_function))
yield next(generators[arg['sequence']])

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

from math import sqrt
from math import floor
def fibonacci():
current = 1
- next = 1
+ next_ = 1
while True:
yield current
temporary = current
- current = next
- next += temporary
+ current = next_
+ next_ += temporary
def is_prime(candidate, is_divider):
if is_divider == 1:
return True
elif candidate % is_divider == 0:
return False
else:
return is_prime(candidate, is_divider - 1)
def primes():
number = 2
while True:
yield number
- number = number + 1
+ number += 1
while is_prime(number, floor(sqrt(number))) is False:
- number = number + 1
+ number += 1
def alphabet(letters=0, code=0):
if code == 'lat' and letters is 0:
latin_letter_code = 97
while latin_letter_code < 123:
yield chr(latin_letter_code)
latin_letter_code += 1
elif code == 'bg' and letters is 0:
bulgarian_letter_code = 1072
while bulgarian_letter_code < 1104:
if bulgarian_letter_code == 1099 or bulgarian_letter_code == 1101:
bulgarian_letter_code += 1
yield chr(bulgarian_letter_code)
bulgarian_letter_code += 1
else:
next_letter = 0
while next_letter < len(letters):
yield letters[next_letter]
next_letter += 1
def intertwined_sequences(argument, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci, 'primes': primes})
generators = {}
flag = False
for arg in argument:
while arg['length'] > 0:
arg['length'] -= 1
if arg['sequence'] == 'alphabet':
if 'code' in arg.keys() and flag is False:
flag = True
it = alphabet(code=arg['code'])
elif 'letters' in arg.keys() and flag is False:
it = alphabet(letters=arg['letters'])
flag = True
yield next(it)
else:
args_for_function = {}
if len(arg) > 2:
for key, value in arg.items():
if not key == 'sequence' and not key == 'length':
args_for_function.update({key: value})
if not arg['sequence'] in generators.keys():
generators[arg['sequence']] = iter(
generator_definitions[arg['sequence']](
**args_for_function))
yield next(generators[arg['sequence']])