Решение на Генератори и итератори от Милка Ферезлийска

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

Към профила на Милка Ферезлийска

Резултати

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

Код

from itertools import islice
def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
raise StopIteration
def alphabet(*, letters='', code=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
iterables = alphabets[code] if not letters else letters
return (iterable for iterable in iterables)
from types import GeneratorType
def intertwined_sequences(data, *, generator_definitions={}):
collected = {}
result = []
functions = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
data = (next(data),) if type(data) is GeneratorType else data
if generator_definitions:
functions.update(generator_definitions)
for function in data:
args = {key: value for key, value in function.items()
if key != 'sequence' and key != 'length'}
function_name = functions[function['sequence']]
if function_name in collected:
result.extend(list(islice(collected[function_name],
function['length'])))
else:
collected[function_name] = function_name(**args)
result.extend(list(islice(collected[function_name],
function['length'])))
return iter(result)

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

......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 6.743s

FAILED (errors=3)

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

Милка обнови решението на 31.03.2015 22:04 (преди над 9 години)

+def fibonacci():
+ number, next_number = 0, 1
+ yield next_number
+ while True:
+ number, next_number = next_number, number + next_number
+ yield next_number
+
+
+def primes():
+ yield 2
+ numbers = [2]
+ number = numbers[-1] + 1
+ while True:
+ if any(map(lambda x: number % x is 0, numbers)):
+ number += 1
+ else:
+ numbers.append(number)
+ yield number

Милка обнови решението на 31.03.2015 22:52 (преди над 9 години)

def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
- yield number
+ yield number
+
+
+def alphabet(**kwargs):
+ alphabets = {
+ 'lat': string.ascii_lowercase,
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+ if 'letters' in kwargs:
+ for letter in kwargs['letters']:
+ yield letter
+ elif 'code' in kwargs:
+ for letter in alphabets[kwargs['code']]:
+ yield letter

Милка обнови решението на 01.04.2015 17:47 (преди над 9 години)

+from string import ascii_lowercase
+
+
def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
def alphabet(**kwargs):
alphabets = {
- 'lat': string.ascii_lowercase,
+ 'lat': ascii_lowercase,
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
if 'letters' in kwargs:
for letter in kwargs['letters']:
yield letter
elif 'code' in kwargs:
for letter in alphabets[kwargs['code']]:
yield letter

Милка обнови решението на 02.04.2015 22:04 (преди над 9 години)

-from string import ascii_lowercase
-
-
def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
-def alphabet(**kwargs):
+def alphabet(*, letters='', code=''):
alphabets = {
- 'lat': ascii_lowercase,
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
- if 'letters' in kwargs:
- for letter in kwargs['letters']:
+ if letters:
+ for letter in letters:
yield letter
- elif 'code' in kwargs:
- for letter in alphabets[kwargs['code']]:
+ elif code:
- yield letter
+ for letter in alphabets[code]:
+ yield letter

Милка обнови решението на 02.04.2015 22:35 (преди над 9 години)

def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
def alphabet(*, letters='', code=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
- if letters:
- for letter in letters:
+ iterables = alphabets[code] if not letters else letters
- yield letter
+ return (iterable for iterable in iterables)
- elif code:
- for letter in alphabets[code]:
- yield letter

Милка обнови решението на 03.04.2015 16:17 (преди над 9 години)

+from itertools import islice
+
+
def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
+ raise StopIteration
def alphabet(*, letters='', code=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
iterables = alphabets[code] if not letters else letters
return (iterable for iterable in iterables)
+
+
+def intertwined_sequences(data):
+ collected = {}
+ result = []
+ functions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+ for function in data:
+ args = {key: value for key, value in function.items()
+ if key != 'sequence' and key != 'length'}
+ function_name = functions[function['sequence']]
+ if function_name in collected:
+ result.extend(list(islice(collected[function_name],
+ function['length'])))
+ else:
+ collected[function_name] = function_name(**args)
+ result.extend(list(islice(collected[function_name],
+ function['length'])))
+ return result

Милка обнови решението на 03.04.2015 16:49 (преди над 9 години)

from itertools import islice
def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
raise StopIteration
def alphabet(*, letters='', code=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
iterables = alphabets[code] if not letters else letters
return (iterable for iterable in iterables)
-def intertwined_sequences(data):
+def intertwined_sequences(data, *, generator_definitions={}):
collected = {}
result = []
functions = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
+ if generator_definitions:
+ functions.update(generator_definitions)
for function in data:
args = {key: value for key, value in function.items()
if key != 'sequence' and key != 'length'}
function_name = functions[function['sequence']]
if function_name in collected:
result.extend(list(islice(collected[function_name],
function['length'])))
else:
collected[function_name] = function_name(**args)
result.extend(list(islice(collected[function_name],
function['length'])))
- return result
+ return iter(result)

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

from itertools import islice
def fibonacci():
number, next_number = 0, 1
yield next_number
while True:
number, next_number = next_number, number + next_number
yield next_number
def primes():
yield 2
numbers = [2]
number = numbers[-1] + 1
while True:
if any(map(lambda x: number % x is 0, numbers)):
number += 1
else:
numbers.append(number)
yield number
raise StopIteration
def alphabet(*, letters='', code=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
iterables = alphabets[code] if not letters else letters
return (iterable for iterable in iterables)
+from types import GeneratorType
+
+
def intertwined_sequences(data, *, generator_definitions={}):
collected = {}
result = []
functions = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
+ data = (next(data),) if type(data) is GeneratorType else data
if generator_definitions:
functions.update(generator_definitions)
for function in data:
args = {key: value for key, value in function.items()
if key != 'sequence' and key != 'length'}
function_name = functions[function['sequence']]
if function_name in collected:
result.extend(list(islice(collected[function_name],
function['length'])))
else:
collected[function_name] = function_name(**args)
result.extend(list(islice(collected[function_name],
function['length'])))
return iter(result)