Решение на Генератори и итератори от Ирина Христова

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

Към профила на Ирина Христова

Резултати

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

Код

def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
def alphabet(code='', letters=''):
latin = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters is not '':
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
def intertwined_sequences(things, generator_definitions=None):
to_do = {'fibonacci': fibonacci(), 'primes': primes()}
for thing in things:
if thing['sequence'] in to_do:
for times in range(thing['length']):
yield next(to_do[thing['sequence']])
else:
if thing['sequence'] == 'alphabet':
if 'letters' in thing:
to_do['alphabet'] = alphabet(letters=thing['letters'])
elif 'code' in thing:
to_do['alphabet'] = alphabet(code=thing['code'])
elif thing['sequence'] == 'multiples_of':
for missing in things:
if (missing['sequence'] == 'multiples_of'
and 'num' in missing):
to_do[thing['sequence']] = multiples_of(missing['num'])
break
else:
# multiples_of doesn't have argument 'num',
# use multiples_of from generator_definitions
to_do[thing['sequence']] = iter(
generator_definitions[thing['sequence']]
)
else:
to_do[thing['sequence']] = iter(
generator_definitions[thing['sequence']]
)
for times in range(thing['length']):
yield next(to_do[thing['sequence']])

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

......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_kwargs_generator (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 8.249s

FAILED (errors=3)

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

Ирина обнови решението на 29.03.2015 19:53 (преди над 9 години)

+def fibonacci():
+ a = 1
+ b = 1
+
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def primes():
+ i = 2
+
+ while True:
+ j = 2
+ while j < i:
+ if i % j == 0:
+ i += 1
+ break
+ j += 1
+ else:
+ yield i
+ i += 1
+
+
+def alphabet(code='', letters=''):
+ latin = 'abcdefghijklmnopqrstuvwxyz'
+ bg = 'абвгдежзийклмнопстуфхцчшщъьюя'
+
+ if letters != '':
+ for char in letters:
+ yield char
+ else:
+ if code == 'lat':
+ for char in latin:
+ yield char
+ else:
+ for char in bg:
+ yield char
+
+
+def intertwined_sequences(things):
+ fib = fibonacci()
+ prime = primes()
+ what_to_do = {'fibonacci': fib, 'primes': prime}
+
+ for thing in things:
+ if thing['sequence'] in what_to_do:
+ for times in range(thing['length']):
+ yield next(what_to_do[thing['sequence']])
+ elif thing['sequence'] == 'alphabet':
+ if 'letters' in thing:
+ alpha = alphabet(letters=thing['letters'])
+ for times in range(thing['length']):
+ yield next(alpha)
+ elif 'code' in thing:
+ alpha = alphabet(code=thing['code'])
+ for times in range(thing['length']):
+ yield next(alpha)

Ирина обнови решението на 31.03.2015 17:51 (преди над 9 години)

def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
def alphabet(code='', letters=''):
latin = 'abcdefghijklmnopqrstuvwxyz'
- bg = 'абвгдежзийклмнопстуфхцчшщъьюя'
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters != '':
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
def intertwined_sequences(things):
fib = fibonacci()
prime = primes()
- what_to_do = {'fibonacci': fib, 'primes': prime}
for thing in things:
- if thing['sequence'] in what_to_do:
- for times in range(thing['length']):
- yield next(what_to_do[thing['sequence']])
- elif thing['sequence'] == 'alphabet':
+ if thing['sequence'] == 'alphabet':
if 'letters' in thing:
alpha = alphabet(letters=thing['letters'])
- for times in range(thing['length']):
- yield next(alpha)
elif 'code' in thing:
alpha = alphabet(code=thing['code'])
- for times in range(thing['length']):
- yield next(alpha)
+ break
+
+ what_to_do = {'fibonacci': fib, 'primes': prime, 'alphabet': alpha}
+
+ for thing in things:
+ if thing['sequence'] in what_to_do:
+ for times in range(thing['length']):
+ yield next(what_to_do[thing['sequence']])
+

Ирина обнови решението на 01.04.2015 17:58 (преди над 9 години)

def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
-def alphabet(code='', letters=''):
+def alphabet(code=None, letters=None):
latin = 'abcdefghijklmnopqrstuvwxyz'
- bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ bg = 'абвгдежзийклмнопpстуфхцчшщъьюя'
- if letters != '':
+ if letters is not None:
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
-def intertwined_sequences(things):
- fib = fibonacci()
- prime = primes()
+def intertwined_sequences(things, generator_definitions=None):
+ what_to_do = {'fibonacci': fibonacci(), 'primes': primes()}
for thing in things:
if thing['sequence'] == 'alphabet':
if 'letters' in thing:
- alpha = alphabet(letters=thing['letters'])
+ what_to_do['alphabet'] = alphabet(letters=thing['letters'])
elif 'code' in thing:
- alpha = alphabet(code=thing['code'])
+ what_to_do['alphabet'] = alphabet(code=thing['code'])
break
- what_to_do = {'fibonacci': fib, 'primes': prime, 'alphabet': alpha}
+ if generator_definitions is not None:
+ for generator in generator_definitions:
+ what_to_do[generator] = iter(generator_definitions[generator])
for thing in things:
- if thing['sequence'] in what_to_do:
+ if str(thing['sequence']) in what_to_do:
for times in range(thing['length']):
yield next(what_to_do[thing['sequence']])
-

Ирина обнови решението на 01.04.2015 23:12 (преди над 9 години)

def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
-def alphabet(code=None, letters=None):
+def alphabet(code='', letters=''):
latin = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопpстуфхцчшщъьюя'
- if letters is not None:
+ if letters is not '':
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
+def ones():
+ while True:
+ yield 1
+
+
+def naturals():
+ number = 1
+ while True:
+ yield number
+ number += 1
+
+
+def multiples_of(num):
+ i = 1
+ while True:
+ yield num * i
+ i += 1
+
+
def intertwined_sequences(things, generator_definitions=None):
- what_to_do = {'fibonacci': fibonacci(), 'primes': primes()}
+ to_do = {'fibonacci': fibonacci(), 'primes': primes()}
for thing in things:
- if thing['sequence'] == 'alphabet':
+ if thing['sequence'] == 'alphabet' and 'alphabet' not in to_do:
if 'letters' in thing:
- what_to_do['alphabet'] = alphabet(letters=thing['letters'])
+ to_do['alphabet'] = alphabet(letters=thing['letters'])
elif 'code' in thing:
- what_to_do['alphabet'] = alphabet(code=thing['code'])
- break
+ to_do['alphabet'] = alphabet(code=thing['code'])
+ if thing['sequence'] == 'multiples' and 'multiples' not in to_do:
+ if 'num' in thing:
+ to_do[thing['sequence']] = multiples_of(thing['num'])
if generator_definitions is not None:
for generator in generator_definitions:
- what_to_do[generator] = iter(generator_definitions[generator])
+ if generator == 'multiples' and 'multiples' in to_do:
+ pass
+ else:
+ to_do[generator] = generator_definitions[generator]
for thing in things:
- if str(thing['sequence']) in what_to_do:
- for times in range(thing['length']):
+ for times in range(thing['length']):
- yield next(what_to_do[thing['sequence']])
+ yield next(to_do[thing['sequence']])

Ирина обнови решението на 02.04.2015 11:11 (преди над 9 години)

+from itertools import cycle
+
+
def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
def alphabet(code='', letters=''):
latin = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопpстуфхцчшщъьюя'
if letters is not '':
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
def ones():
while True:
yield 1
def naturals():
number = 1
while True:
yield number
number += 1
def multiples_of(num):
i = 1
while True:
yield num * i
i += 1
+def endless_growing_sequences():
+ for i, generator_key in enumerate(cycle(['fibonacci', 'primes'])):
+ yield {'sequence': generator_key, 'length': i + 1}
+
+
def intertwined_sequences(things, generator_definitions=None):
to_do = {'fibonacci': fibonacci(), 'primes': primes()}
for thing in things:
- if thing['sequence'] == 'alphabet' and 'alphabet' not in to_do:
- if 'letters' in thing:
- to_do['alphabet'] = alphabet(letters=thing['letters'])
- elif 'code' in thing:
- to_do['alphabet'] = alphabet(code=thing['code'])
- if thing['sequence'] == 'multiples' and 'multiples' not in to_do:
- if 'num' in thing:
- to_do[thing['sequence']] = multiples_of(thing['num'])
-
- if generator_definitions is not None:
- for generator in generator_definitions:
- if generator == 'multiples' and 'multiples' in to_do:
- pass
+ if thing['sequence'] in to_do:
+ for times in range(thing['length']):
+ yield next(to_do[thing['sequence']])
+ else:
+ if thing['sequence'] == 'alphabet':
+ if 'letters' in thing:
+ to_do['alphabet'] = alphabet(letters=thing['letters'])
+ elif 'code' in thing:
+ to_do['alphabet'] = alphabet(code=thing['code'])
+ elif thing['sequence'] == 'multiples_of':
+ for missing in things:
+ if (missing['sequence'] == 'multiples_of'
+ and 'num' in missing):
+ to_do[thing['sequence']] = multiples_of(missing['num'])
+ break
+ else:
+ # multiples_of doesn't have argument 'num',
+ # use multiples_of from generator_definitions
+ to_do[thing['sequence']] = iter(
+ generator_definitions[thing['sequence']]
+ )
else:
- to_do[generator] = generator_definitions[generator]
+ to_do[thing['sequence']] = iter(
+ generator_definitions[thing['sequence']]
+ )
- for thing in things:
- for times in range(thing['length']):
+ for times in range(thing['length']):
- yield next(to_do[thing['sequence']])
+ yield next(to_do[thing['sequence']])

Ирина обнови решението на 03.04.2015 08:21 (преди над 9 години)

from itertools import cycle
def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
def alphabet(code='', letters=''):
latin = 'abcdefghijklmnopqrstuvwxyz'
- bg = 'абвгдежзийклмнопpстуфхцчшщъьюя'
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters is not '':
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
def ones():
while True:
yield 1
def naturals():
number = 1
while True:
yield number
number += 1
def multiples_of(num):
i = 1
while True:
yield num * i
i += 1
def endless_growing_sequences():
for i, generator_key in enumerate(cycle(['fibonacci', 'primes'])):
yield {'sequence': generator_key, 'length': i + 1}
def intertwined_sequences(things, generator_definitions=None):
to_do = {'fibonacci': fibonacci(), 'primes': primes()}
for thing in things:
if thing['sequence'] in to_do:
for times in range(thing['length']):
yield next(to_do[thing['sequence']])
else:
if thing['sequence'] == 'alphabet':
if 'letters' in thing:
to_do['alphabet'] = alphabet(letters=thing['letters'])
elif 'code' in thing:
to_do['alphabet'] = alphabet(code=thing['code'])
elif thing['sequence'] == 'multiples_of':
for missing in things:
if (missing['sequence'] == 'multiples_of'
and 'num' in missing):
to_do[thing['sequence']] = multiples_of(missing['num'])
break
else:
# multiples_of doesn't have argument 'num',
# use multiples_of from generator_definitions
to_do[thing['sequence']] = iter(
generator_definitions[thing['sequence']]
)
else:
to_do[thing['sequence']] = iter(
generator_definitions[thing['sequence']]
)
for times in range(thing['length']):
yield next(to_do[thing['sequence']])

Ирина обнови решението на 03.04.2015 08:25 (преди над 9 години)

-from itertools import cycle
-
-
def fibonacci():
a = 1
b = 1
while True:
yield a
a, b = b, a + b
def primes():
i = 2
while True:
j = 2
while j < i:
if i % j == 0:
i += 1
break
j += 1
else:
yield i
i += 1
def alphabet(code='', letters=''):
latin = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters is not '':
for char in letters:
yield char
else:
if code == 'lat':
for char in latin:
yield char
else:
for char in bg:
yield char
-
-
-def ones():
- while True:
- yield 1
-
-
-def naturals():
- number = 1
- while True:
- yield number
- number += 1
-
-
-def multiples_of(num):
- i = 1
- while True:
- yield num * i
- i += 1
-
-
-def endless_growing_sequences():
- for i, generator_key in enumerate(cycle(['fibonacci', 'primes'])):
- yield {'sequence': generator_key, 'length': i + 1}
def intertwined_sequences(things, generator_definitions=None):
to_do = {'fibonacci': fibonacci(), 'primes': primes()}
for thing in things:
if thing['sequence'] in to_do:
for times in range(thing['length']):
yield next(to_do[thing['sequence']])
else:
if thing['sequence'] == 'alphabet':
if 'letters' in thing:
to_do['alphabet'] = alphabet(letters=thing['letters'])
elif 'code' in thing:
to_do['alphabet'] = alphabet(code=thing['code'])
elif thing['sequence'] == 'multiples_of':
for missing in things:
if (missing['sequence'] == 'multiples_of'
and 'num' in missing):
to_do[thing['sequence']] = multiples_of(missing['num'])
break
else:
# multiples_of doesn't have argument 'num',
# use multiples_of from generator_definitions
to_do[thing['sequence']] = iter(
generator_definitions[thing['sequence']]
)
else:
to_do[thing['sequence']] = iter(
generator_definitions[thing['sequence']]
)
for times in range(thing['length']):
yield next(to_do[thing['sequence']])