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

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

Към профила на Янислав Василев

Резултати

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

Код

import string
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_ALPHABET = string.ascii_lowercase
def fibonacci(a=1, b=1):
while True:
yield a
a, b = b, a + b
def is_prime(number):
return all(number % divisor for divisor in range(2, number))
def primes(number=2):
while True:
if is_prime(number):
yield number
number += 1
def alphabet(**kwargs):
codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
if 'letters' in kwargs:
for char in kwargs['letters']:
yield char
else:
for char in codes[kwargs['code']]:
yield char
def intertwined_sequences(data, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet, })
# @instances holds all my generator instances with validated parameters
instances = {}
for dicty in iter(data):
arguments = {}
for key, value in dicty.items():
if not any([key == 'sequence', key == 'length',
key in arguments]):
arguments[key] = value
# I have to initialize only one instance per sequence and if
# I meet second time sequence that I already initilized
#(arguments for them are given only the first time)
# I have to catch TypeError
try:
if dicty['sequence'] not in instances.keys():
instances.update(
{dicty['sequence']:
generator_definitions[dicty['sequence']](**arguments)})
except TypeError:
pass
for _ in range(dicty['length']):
yield next(iter(instances[dicty['sequence']]))

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

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

----------------------------------------------------------------------
Ran 14 tests in 3.616s

FAILED (errors=1)

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

Янислав обнови решението на 01.04.2015 05:13 (преди над 9 години)

+import string
+
+BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+LATIN_ALPHABET = string.ascii_lowercase
+
+
+def fibonacci(a=1, b=1):
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(number):
+ return all(number % divisor for divisor in range(2, number))
+
+
+def primes(number=2):
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(**kwargs):
+ codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
+ if 'letters' in kwargs:
+ for char in kwargs['letters']:
+ yield char
+ else:
+ for char in codes[kwargs['code']]:
+ yield char
+
+
+def intertwined_sequences(data, generator_definitions={}):
+ generator_definitions.update({'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet, })
+
+ # @instances holds all my generator instances with validated parameters
+ instances = {}
+ for dicty in list(data):
+ arguments = {key: value for key, value in dicty.items()
+ if not any([key == 'sequence', key == 'length'])}
+
+ # I have to initialize only one instance per sequence and if
+ # I meet second time sequence that I already initilized(arguments for them are given only first time)
+ # I have to catch TypeError
+ try:
+ instances.update(
+ {dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
+ except TypeError:
+ continue
+
+ for dictionary in data:
+ for _ in range(dictionary['length']):
+ yield next(instances[dictionary['sequence']])

Янислав обнови решението на 03.04.2015 16:01 (преди над 9 години)

import string
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_ALPHABET = string.ascii_lowercase
def fibonacci(a=1, b=1):
while True:
yield a
a, b = b, a + b
def is_prime(number):
return all(number % divisor for divisor in range(2, number))
def primes(number=2):
while True:
if is_prime(number):
yield number
number += 1
def alphabet(**kwargs):
codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
if 'letters' in kwargs:
for char in kwargs['letters']:
yield char
else:
for char in codes[kwargs['code']]:
yield char
def intertwined_sequences(data, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet, })
# @instances holds all my generator instances with validated parameters
instances = {}
for dicty in list(data):
- arguments = {key: value for key, value in dicty.items()
- if not any([key == 'sequence', key == 'length'])}
-
- # I have to initialize only one instance per sequence and if
- # I meet second time sequence that I already initilized(arguments for them are given only first time)
+ arguments = {}
+ for key, value in dicty.items():
+ if not any([key == 'sequence', key == 'length', key in arguments]):
+ arguments[key] = value
+
+ # I have to initialize only one instance per sequence and if
+ # I meet second time sequence that I already initilized(arguments for them are given only the first time)
# I have to catch TypeError
try:
- instances.update(
- {dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
+ if dicty['sequence'] not in instances.keys():
+ instances.update(
+ {dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
except TypeError:
- continue
+ pass
for dictionary in data:
for _ in range(dictionary['length']):
yield next(instances[dictionary['sequence']])

Янислав обнови решението на 03.04.2015 16:08 (преди над 9 години)

import string
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_ALPHABET = string.ascii_lowercase
def fibonacci(a=1, b=1):
while True:
yield a
a, b = b, a + b
def is_prime(number):
return all(number % divisor for divisor in range(2, number))
def primes(number=2):
while True:
if is_prime(number):
yield number
number += 1
def alphabet(**kwargs):
codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
if 'letters' in kwargs:
for char in kwargs['letters']:
yield char
else:
for char in codes[kwargs['code']]:
yield char
def intertwined_sequences(data, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet, })
# @instances holds all my generator instances with validated parameters
instances = {}
for dicty in list(data):
arguments = {}
for key, value in dicty.items():
if not any([key == 'sequence', key == 'length', key in arguments]):
arguments[key] = value
# I have to initialize only one instance per sequence and if
# I meet second time sequence that I already initilized(arguments for them are given only the first time)
# I have to catch TypeError
try:
if dicty['sequence'] not in instances.keys():
instances.update(
{dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
except TypeError:
pass
- for dictionary in data:
- for _ in range(dictionary['length']):
+ for _ in range(dicty['length']):
- yield next(instances[dictionary['sequence']])
+ yield next(instances[dicty['sequence']])

Янислав обнови решението на 03.04.2015 16:27 (преди над 9 години)

import string
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_ALPHABET = string.ascii_lowercase
def fibonacci(a=1, b=1):
while True:
yield a
a, b = b, a + b
def is_prime(number):
return all(number % divisor for divisor in range(2, number))
def primes(number=2):
while True:
if is_prime(number):
yield number
number += 1
def alphabet(**kwargs):
codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
if 'letters' in kwargs:
for char in kwargs['letters']:
yield char
else:
for char in codes[kwargs['code']]:
yield char
def intertwined_sequences(data, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet, })
# @instances holds all my generator instances with validated parameters
instances = {}
- for dicty in list(data):
+ for dicty in iter(data):
arguments = {}
for key, value in dicty.items():
if not any([key == 'sequence', key == 'length', key in arguments]):
arguments[key] = value
# I have to initialize only one instance per sequence and if
# I meet second time sequence that I already initilized(arguments for them are given only the first time)
# I have to catch TypeError
try:
if dicty['sequence'] not in instances.keys():
instances.update(
{dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
except TypeError:
pass
for _ in range(dicty['length']):
- yield next(instances[dicty['sequence']])
+ yield next(instances[dicty['sequence']])

Янислав обнови решението на 03.04.2015 16:37 (преди над 9 години)

import string
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_ALPHABET = string.ascii_lowercase
def fibonacci(a=1, b=1):
while True:
yield a
a, b = b, a + b
def is_prime(number):
return all(number % divisor for divisor in range(2, number))
def primes(number=2):
while True:
if is_prime(number):
yield number
number += 1
def alphabet(**kwargs):
codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
if 'letters' in kwargs:
for char in kwargs['letters']:
yield char
else:
for char in codes[kwargs['code']]:
yield char
def intertwined_sequences(data, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet, })
# @instances holds all my generator instances with validated parameters
instances = {}
for dicty in iter(data):
arguments = {}
for key, value in dicty.items():
- if not any([key == 'sequence', key == 'length', key in arguments]):
+ if not any([key == 'sequence', key == 'length',
+ key in arguments]):
arguments[key] = value
# I have to initialize only one instance per sequence and if
- # I meet second time sequence that I already initilized(arguments for them are given only the first time)
+ # I meet second time sequence that I already initilized
+ #(arguments for them are given only the first time)
# I have to catch TypeError
try:
if dicty['sequence'] not in instances.keys():
instances.update(
- {dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
+ {dicty['sequence']:
+ generator_definitions[dicty['sequence']](**arguments)})
except TypeError:
pass
for _ in range(dicty['length']):
- yield next(instances[dicty['sequence']])
+ yield next(iter(instances[dicty['sequence']]))