Решение на Генератори и итератори от Данаил Димитров

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

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

Резултати

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

Код

from string import ascii_lowercase
def fibonacci():
first = 1
second = 1
while True:
yield first
second += first
first = second - first
def primes():
prime_numbers = []
current = 2
while True:
prime_numbers.append(current)
yield current
primeness = lambda prime: current % prime == 0
while list(filter(primeness, prime_numbers)):
current += 1
CODE = {
'lat': list(ascii_lowercase),
'bg': list('абвгдежзийклмнопрстуфхцчшщъьюя')
}
def alphabet(code='', letters=''):
alpha = ''
if code != '':
alpha = CODE[code]
if letters != '':
alpha = letters
for letter in alpha:
yield letter
def intertwined_sequences(sequences, generator_definitions={}):
loaded_gens = {}
for seq in sequences:
sequence = seq['sequence']
length = seq['length']
args = seq.copy()
del args['sequence']
del args['length']
if sequence in loaded_gens:
gen = loaded_gens[sequence]
else:
if sequence in generator_definitions:
gen = generator_definitions[sequence]
else:
gen = globals()[sequence]
if args != {}:
gen = gen(**args)
else:
gen = gen()
loaded_gens[sequence] = gen
while length > 0:
yield next(gen)
length -= 1

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

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 6.152s

FAILED (errors=3)

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

Данаил обнови решението на 01.04.2015 23:48 (преди над 9 години)

+from string import ascii_lowercase
+
+
+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ second += first
+ first = second-first
+
+
+def primes():
+ prime_numbers = []
+ current = 2
+ while True:
+ prime_numbers.append(current)
+ yield current
+
+ primeness = lambda prime: current % prime == 0
+ while list(filter(primeness, prime_numbers)):
+ current += 1
+
+
+CODE = {
+ 'lat': list(ascii_lowercase),
+ 'bg': list('абвгдежзийклмнопрстуфхцчшщъьюя')
+}
+
+
+def alphabet(code='', letters=''):
+ alpha = ''
+ if code != '':
+ alpha = CODE[code]
+ if letters != '':
+ alpha = letters
+ for letter in alpha:
+ yield letter
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ for seq in sequences:
+ if seq['sequence'] in generator_definitions:
+ sequence = generator_definitions[seq['sequence']]
+ else:
+ sequence = seq['sequence']
+ del seq['sequence']
+ length = seq['length']
+ del seq['length']
+ if seq != {}:
+ gen = globals()[sequence](**seq)
+ else:
+ gen = globals()[sequence]()
+ while length > 0:
+ yield next(gen)
+ length -= 1

Данаил обнови решението на 03.04.2015 11:58 (преди над 9 години)

from string import ascii_lowercase
def fibonacci():
first = 1
second = 1
while True:
yield first
second += first
- first = second-first
+ first = second - first
def primes():
prime_numbers = []
current = 2
while True:
prime_numbers.append(current)
yield current
primeness = lambda prime: current % prime == 0
while list(filter(primeness, prime_numbers)):
current += 1
CODE = {
'lat': list(ascii_lowercase),
'bg': list('абвгдежзийклмнопрстуфхцчшщъьюя')
}
def alphabet(code='', letters=''):
alpha = ''
if code != '':
alpha = CODE[code]
if letters != '':
alpha = letters
for letter in alpha:
yield letter
def intertwined_sequences(sequences, generator_definitions={}):
+ loaded_gens = {}
for seq in sequences:
- if seq['sequence'] in generator_definitions:
- sequence = generator_definitions[seq['sequence']]
- else:
- sequence = seq['sequence']
- del seq['sequence']
+ sequence = seq['sequence']
length = seq['length']
- del seq['length']
- if seq != {}:
- gen = globals()[sequence](**seq)
+ args = seq.copy()
+ del args['sequence']
+ del args['length']
+ if sequence in loaded_gens:
+ gen = loaded_gens[sequence]
else:
- gen = globals()[sequence]()
+ if sequence in generator_definitions:
+ gen = generator_definitions[sequence]
+ else:
+ gen = globals()[sequence]
+ if args != {}:
+ gen = gen(**args)
+ else:
+ gen = gen()
+ loaded_gens[sequence] = gen
while length > 0:
yield next(gen)
- length -= 1
+ length -= 1