Решение на Генератори и итератори от Георги Любенов

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

Към профила на Георги Любенов

Резултати

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

Код

from itertools import islice
def memorize(func):
memory = {}
def memorized(*args):
if args in memory:
return memory[args]
result = func(*args)
memory[args] = result
return result
return memorized
@memorize
def fib(number):
if number in [0, 1]:
return 1
return fib(number - 1) + fib(number - 2)
def fibonacci():
value = 0
while True:
yield fib(value)
value += 1
def is_prime(number):
for i in range(2, number - 1):
if number % i == 0:
return False
return True
def primes():
value = 2
while True:
if is_prime(value) is True:
yield value
value += 1
def alphabet(code='', letters=''):
def generate_alphabet(letters):
counter = 0
while counter < len(letters):
yield letters[counter]
counter += 1
raise StopIteration
types_of_alphabet = {
'lat': list(map(chr, range(97, 123))),
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
if letters != '':
return generate_alphabet(letters)
for key in types_of_alphabet:
if code == key:
return generate_alphabet(types_of_alphabet[key])
def intertwined_sequences(iterable_object, generator_definitions={}):
def alphabet_iterator():
for dictionary in iterable_object:
if 'code' in dictionary:
return alphabet(code=dictionary['code'])
elif 'letters' in dictionary:
return alphabet(letters=dictionary['letters'])
def generate_slice(sequence, length):
for obj in islice(sequence, 0, length):
yield (obj)
sequences = {
'fibonacci': fibonacci(),
'primes': primes(),
'alphabet': alphabet_iterator()
}
for dictionary in iterable_object:
for key in dictionary:
if key != 'sequence' and key != 'length':
arg = dictionary[key]
for key in generator_definitions:
temp = generator_definitions[key]
try:
obj = temp(arg)
except:
obj = temp()
generator_definitions[key] = obj
sequences.update(generator_definitions)
for dictionary in iterable_object:
length = dictionary['length']
for key in sequences:
if dictionary['sequence'] == key:
temp = iter(generate_slice(iter(sequences[key]), length))
for obj in islice(temp, 0, length):
yield obj
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

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

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

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

FAILED (errors=4)

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

Георги обнови решението на 03.04.2015 16:45 (преди над 9 години)

+from itertools import islice
+
+
+def memorize(func):
+ memory = {}
+
+ def memorized(*args):
+ if args in memory:
+ return memory[args]
+ result = func(*args)
+ memory[args] = result
+ return result
+ return memorized
+
+
+@memorize
+def fib(number):
+ if number in [0, 1]:
+ return 1
+ return fib(number - 1) + fib(number - 2)
+
+
+def fibonacci():
+ value = 0
+ while True:
+ yield fib(value)
+ value += 1
+
+
+def is_prime(number):
+ for i in range(2, number - 1):
+ if number % i == 0:
+ return False
+ return True
+
+
+def primes():
+ value = 2
+ while True:
+ if is_prime(value) is True:
+ yield value
+ value += 1
+
+
+def alphabet(code='', letters=''):
+ def generate_alphabet(letters):
+ counter = 0
+ while counter < len(letters):
+ yield letters[counter]
+ counter += 1
+ raise StopIteration
+ types_of_alphabet = {
+ 'lat': list(map(chr, range(97, 123))),
+ 'bg': 'абвгдежзийклмнопстуфхцчшщъьюя'
+ }
+ if letters != '':
+ return generate_alphabet(letters)
+ for key in types_of_alphabet:
+ if code == key:
+ return generate_alphabet(types_of_alphabet[key])
+
+
+def intertwined_sequences(iterable_object, generator_definitions={}):
+ def alphabet_iterator():
+ for dictionary in iterable_object:
+ if 'code' in dictionary:
+ return alphabet(code=dictionary['code'])
+ elif 'letters' in dictionary:
+ return alphabet(letters=dictionary['letters'])
+
+ def generate_slice(sequence, length):
+ for obj in islice(sequence, 0, length):
+ yield (obj)
+ sequences = {
+ 'fibonacci': fibonacci(),
+ 'primes': primes(),
+ 'alphabet': alphabet_iterator()
+ }
+ for dictionary in iterable_object:
+ for key in dictionary:
+ if key != 'sequence' and key != 'length':
+ arg = dictionary[key]
+ for key in generator_definitions:
+ temp = generator_definitions[key]
+ try:
+ obj = temp(arg)
+ except:
+ obj = temp()
+ generator_definitions[key] = obj
+
+ sequences.update(generator_definitions)
+ for dictionary in iterable_object:
+ length = dictionary['length']
+ for key in sequences:
+ if dictionary['sequence'] == key:
+ temp = iter(generate_slice(iter(sequences[key]), length))
+ for obj in islice(temp, 0, length):
+ yield obj
+
+
+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

Георги обнови решението на 03.04.2015 16:47 (преди над 9 години)

from itertools import islice
def memorize(func):
memory = {}
def memorized(*args):
if args in memory:
return memory[args]
result = func(*args)
memory[args] = result
return result
return memorized
@memorize
def fib(number):
if number in [0, 1]:
return 1
return fib(number - 1) + fib(number - 2)
def fibonacci():
value = 0
while True:
yield fib(value)
value += 1
def is_prime(number):
for i in range(2, number - 1):
if number % i == 0:
return False
return True
def primes():
value = 2
while True:
if is_prime(value) is True:
yield value
value += 1
def alphabet(code='', letters=''):
def generate_alphabet(letters):
counter = 0
while counter < len(letters):
yield letters[counter]
counter += 1
raise StopIteration
types_of_alphabet = {
'lat': list(map(chr, range(97, 123))),
- 'bg': 'абвгдежзийклмнопстуфхцчшщъьюя'
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
if letters != '':
return generate_alphabet(letters)
for key in types_of_alphabet:
if code == key:
return generate_alphabet(types_of_alphabet[key])
def intertwined_sequences(iterable_object, generator_definitions={}):
def alphabet_iterator():
for dictionary in iterable_object:
if 'code' in dictionary:
return alphabet(code=dictionary['code'])
elif 'letters' in dictionary:
return alphabet(letters=dictionary['letters'])
def generate_slice(sequence, length):
for obj in islice(sequence, 0, length):
yield (obj)
sequences = {
'fibonacci': fibonacci(),
'primes': primes(),
'alphabet': alphabet_iterator()
}
for dictionary in iterable_object:
for key in dictionary:
if key != 'sequence' and key != 'length':
arg = dictionary[key]
for key in generator_definitions:
temp = generator_definitions[key]
try:
obj = temp(arg)
except:
obj = temp()
generator_definitions[key] = obj
sequences.update(generator_definitions)
for dictionary in iterable_object:
length = dictionary['length']
for key in sequences:
if dictionary['sequence'] == key:
temp = iter(generate_slice(iter(sequences[key]), length))
for obj in islice(temp, 0, length):
yield obj
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