Решение на Генератори и итератори от Станислав Венков

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

Към профила на Станислав Венков

Резултати

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

Код

from collections import Iterable
LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
last_number = 0
next_number = 1
while True:
yield next_number
last_number = next_number+last_number
yield last_number
next_number = next_number+last_number
def primes():
def is_prime(number):
if (number == 2):
return True
if (number % 2 == 0):
return False
for factor in range(3, 1+int(round(number**0.5)), 2):
if (number % factor == 0):
return False
return True
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(code='lat', letters=False):
if not isinstance(letters, Iterable):
if code is 'lat':
letters = LATIN_ALPHABET
elif code is 'bg':
letters = CYRILLIC_ALPHABET
else:
return None
counter = 0
while counter < len(letters):
yield letters[counter]
counter += 1
def intertwined_sequences(iterable_object, generator_definitions={}):
for elem in iterable_object:
length = elem['length']
if elem['sequence'] is 'alphabet':
if elem.get('letters'):
key = elem.get('letters')
gen = eval('alphabet(letters="{}")'.format(key))
elif elem.get('code'):
key = elem.get('code')
gen = eval('alphabet(code="{}")'.format(key))
if not generator_definitions.get(key):
generator_definitions.update({key: gen})
else:
gen = generator_definitions[key]
else:
key = elem['sequence']
if not generator_definitions.get(key):
del(elem['sequence'])
del(elem['length'])
gen = eval('{}({})'.format(key, ', '.join(list(elem))))
generator_definitions.update({key: gen})
else:
gen = generator_definitions[key]
for _ in range(length):
yield next(gen)

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

.E....EE..EE..
======================================================================
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

======================================================================
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

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

----------------------------------------------------------------------
Ran 14 tests in 10.199s

FAILED (errors=5)

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

Станислав обнови решението на 30.03.2015 23:34 (преди над 9 години)

+from collections import Iterable
+
+LATIN_ALPHABET = 'abcdefghijklmnopqrstuvw'
+CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
+def fibonacci():
+ last_number = 0
+ next_number = 1
+ while True:
+ yield next_number
+ last_number = next_number+last_number
+ yield last_number
+ next_number = next_number+last_number
+
+
+def primes():
+ def is_prime(number):
+ if (number == 2):
+ return True
+ if (number % 2 == 0):
+ return False
+ for factor in range(3, 1+int(round(number**0.5)), 2):
+ if (number % factor == 0):
+ return False
+ return True
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(code='lat', letters=False):
+ if not isinstance(letters, Iterable):
+ if code is 'lat':
+ letters = LATIN_ALPHABET
+ elif code is 'bg':
+ letters = CYRILLIC_ALPHABET
+ else:
+ return None
+
+ counter = 0
+ while True:
+ yield letters[counter]
+ counter += 1
+
+
+def intertwined_sequences(iterable_object, generator_definitions={}):
+
+ for elem in iterable_object:
+ length = elem['length']
+ if elem['sequence'] is 'alphabet':
+ if elem.get('letters'):
+ key = elem.get('letters')
+ gen = eval('alphabet(letters="{}")'.format(key))
+ elif elem.get('code'):
+ key = elem.get('code')
+ gen = eval('alphabet(code="{}")'.format(key))
+
+ if not generator_definitions.get(key):
+ generator_definitions.update({key: gen})
+ else:
+ gen = generator_definitions[key]
+ else:
+
+ key = elem['sequence']
+ if not generator_definitions.get(key):
+ del(elem['sequence'])
+ del(elem['length'])
+ gen = eval('{}({})'.format(key, ', '.join(list(elem))))
+ generator_definitions.update({key: gen})
+ else:
+ gen = generator_definitions[key]
+
+ for _ in range(length):
+ yield next(gen)

Станислав обнови решението на 31.03.2015 18:11 (преди над 9 години)

from collections import Iterable
-LATIN_ALPHABET = 'abcdefghijklmnopqrstuvw'
+LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
last_number = 0
next_number = 1
while True:
yield next_number
last_number = next_number+last_number
yield last_number
next_number = next_number+last_number
def primes():
def is_prime(number):
if (number == 2):
return True
if (number % 2 == 0):
return False
for factor in range(3, 1+int(round(number**0.5)), 2):
if (number % factor == 0):
return False
return True
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(code='lat', letters=False):
if not isinstance(letters, Iterable):
if code is 'lat':
letters = LATIN_ALPHABET
elif code is 'bg':
letters = CYRILLIC_ALPHABET
else:
return None
counter = 0
while True:
yield letters[counter]
counter += 1
def intertwined_sequences(iterable_object, generator_definitions={}):
-
for elem in iterable_object:
length = elem['length']
if elem['sequence'] is 'alphabet':
if elem.get('letters'):
key = elem.get('letters')
gen = eval('alphabet(letters="{}")'.format(key))
elif elem.get('code'):
key = elem.get('code')
gen = eval('alphabet(code="{}")'.format(key))
-
if not generator_definitions.get(key):
generator_definitions.update({key: gen})
else:
gen = generator_definitions[key]
else:
-
key = elem['sequence']
if not generator_definitions.get(key):
del(elem['sequence'])
del(elem['length'])
gen = eval('{}({})'.format(key, ', '.join(list(elem))))
generator_definitions.update({key: gen})
else:
gen = generator_definitions[key]
for _ in range(length):
yield next(gen)

Станислав обнови решението на 03.04.2015 10:17 (преди над 9 години)

from collections import Iterable
LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
last_number = 0
next_number = 1
while True:
yield next_number
last_number = next_number+last_number
yield last_number
next_number = next_number+last_number
def primes():
def is_prime(number):
if (number == 2):
return True
if (number % 2 == 0):
return False
for factor in range(3, 1+int(round(number**0.5)), 2):
if (number % factor == 0):
return False
return True
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(code='lat', letters=False):
if not isinstance(letters, Iterable):
if code is 'lat':
letters = LATIN_ALPHABET
elif code is 'bg':
letters = CYRILLIC_ALPHABET
else:
return None
-
counter = 0
- while True:
+ while counter < len(letters):
yield letters[counter]
counter += 1
def intertwined_sequences(iterable_object, generator_definitions={}):
for elem in iterable_object:
length = elem['length']
if elem['sequence'] is 'alphabet':
if elem.get('letters'):
key = elem.get('letters')
gen = eval('alphabet(letters="{}")'.format(key))
elif elem.get('code'):
key = elem.get('code')
gen = eval('alphabet(code="{}")'.format(key))
+
if not generator_definitions.get(key):
generator_definitions.update({key: gen})
else:
gen = generator_definitions[key]
else:
key = elem['sequence']
if not generator_definitions.get(key):
del(elem['sequence'])
del(elem['length'])
gen = eval('{}({})'.format(key, ', '.join(list(elem))))
generator_definitions.update({key: gen})
else:
gen = generator_definitions[key]
-
for _ in range(length):
- yield next(gen)
+ yield next(gen)