Решение на Генератори и итератори от Николай Велков

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

Към профила на Николай Велков

Резултати

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

Код

import math
def fibonacci():
current_number = 1
next_number = 1
yield current_number
yield next_number
while True:
next_number = current_number + next_number
yield next_number
current_number = next_number - current_number
def is_prime(number):
flag = True
current_divider = 2
while flag and current_divider <= math.sqrt(number):
if number % current_divider == 0:
flag = False
current_divider = current_divider + 1
return flag
def primes():
current_number = 2
yield current_number
current_number = current_number + 1
yield current_number
splitter = 2
while True:
current_number = current_number + splitter
if is_prime(current_number):
yield current_number
def alphabet(letters=None, code='lat'):
if letters is not None:
for letter in letters:
yield letter
elif code == 'lat':
lat_alphabet = "abcdefghijklmnopqrstuvwxyz"
for letter in lat_alphabet:
yield letter
elif code == 'bg':
bg_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
for letter in bg_alphabet:
yield letter
def is_started(generators, sequence):
for generator_name in generators:
if generator_name == sequence:
return True
return False
def intertwined_sequences(generator, generator_definitions=None):
generators = {}
for cur_item in generator:
keys = list(cur_item.keys())
cur_sequence = cur_item.pop('sequence')
cur_length = cur_item.pop('length')
if cur_sequence == 'fibonacci':
if not is_started(generators, cur_sequence):
generators[cur_sequence] = fibonacci()
fibonacci_numbers = generators[cur_sequence]
while cur_length > 0:
yield next(fibonacci_numbers)
cur_length = cur_length - 1
elif cur_sequence == 'primes':
if not is_started(generators, cur_sequence):
generators[cur_sequence] = primes()
prime_numbers = generators[cur_sequence]
while cur_length > 0:
yield next(prime_numbers)
cur_length = cur_length - 1
elif cur_sequence == 'alphabet':
cur_alphabet = 'lat'
if cur_item.get('code') is not None:
cur_code = cur_item.pop('code')
cur_alphabet = cur_sequence + str(cur_code)
if not is_started(generators, cur_alphabet):
generators[cur_alphabet] = alphabet(code=cur_code)
else:
letters = cur_item.pop('letters')
cur_alphabet = cur_sequence + str(letters)
if not is_started(generators,
cur_alphabet):
generators[cur_alphabet] = alphabet(letters=letters)
alphabet_symbols = generators[cur_alphabet]
while cur_length > 0:
yield next(alphabet_symbols)
cur_length = cur_length - 1
else:
if not is_started(generators, cur_sequence):
cur_gen = generator_definitions[cur_sequence]
parameters = list(cur_item.keys())
if parameters != []:
generators[cur_sequence] = iter(cur_gen(**cur_item))
else:
generators[cur_sequence] = iter(cur_gen())
custom_generator = generators[cur_sequence]
while cur_length > 0:
yield next(custom_generator)
cur_length = cur_length - 1

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

...........E..
======================================================================
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 2.261s

FAILED (errors=1)

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

Николай обнови решението на 28.03.2015 20:10 (преди над 9 години)

+import math
+
+
+def fibonacci():
+ current_number = 1
+ next_number = 1
+
+ yield current_number
+ yield next_number
+
+ while True:
+ next_number = current_number + next_number
+ yield next_number
+ current_number = next_number - current_number
+
+
+def is_prime(number):
+ flag = True
+ current_divider = 2
+ while flag and current_divider <= math.sqrt(number):
+ if number % current_divider == 0:
+ flag = False
+ current_divider = current_divider + 1
+ return flag
+
+
+def primes():
+ current_number = 2
+ yield current_number
+
+ current_number = current_number + 1
+ yield current_number
+
+ splitter = 2
+ while True:
+ current_number = current_number + splitter
+ if is_prime(current_number):
+ yield current_number
+
+
+def alphabet(letters=None, code='lat'):
+ if letters is not None:
+ for letter in letters:
+ yield letter
+
+ elif code == 'lat':
+ symbol = 'a'
+ counter = 0
+ while counter < 26:
+ yield symbol
+ symbol = chr(ord(symbol) + 1)
+ counter = counter + 1
+
+ elif code == 'bg':
+ symbol = 'а'
+ counter = 0
+ while counter < 30:
+ yield symbol
+
+ symbol = chr(ord(symbol) + 1)
+ if symbol == 'э' or symbol == 'ы':
+ symbol = chr(ord(symbol) + 1)
+ counter = counter + 1
+
+
+def is_started(generators, sequence):
+ for generator_name in generators:
+ if generator_name == sequence:
+ return True
+
+ return False
+
+
+def get_parameters(items):
+ for key in items:
+ if key is not 'sequence' and key is not 'length' and key is not 'code':
+ return key
+ return None
+
+
+def intertwined_sequences(generator, generator_definitions=None):
+ generators = {}
+
+ for current_item in generator:
+ current_sequence = current_item['sequence']
+ current_length = current_item['length']
+
+ if current_sequence == 'fibonacci':
+ if not is_started(generators, current_sequence):
+ generators[current_sequence] = fibonacci()
+
+ fibonacci_numbers = generators[current_sequence]
+ while current_length > 0:
+ yield next(fibonacci_numbers)
+ current_length = current_length - 1
+
+ elif current_sequence == 'primes':
+ if not is_started(generators, current_sequence):
+ generators[current_sequence] = primes()
+
+ prime_numbers = generators[current_sequence]
+ while current_length > 0:
+ yield next(prime_numbers)
+ current_length = current_length - 1
+
+ elif current_sequence == 'alphabet':
+ current_code = current_item['code']
+ current_alphabet = current_sequence + str(current_code)
+
+ if not is_started(generators, current_alphabet):
+ generators[current_alphabet] = alphabet(code=current_code)
+
+ alphabet_symbols = generators[current_alphabet]
+ while current_length > 0:
+ yield next(alphabet_symbols)
+ current_length = current_length - 1
+ else:
+ if not is_started(generators, current_sequence):
+ current_generator = generator_definitions[current_sequence]
+ parameters = get_parameters(current_item)
+
+ if parameters is not None:
+ argument = current_item[parameters]
+ generators[current_sequence] = current_generator(argument)
+ else:
+ generators[current_sequence] = current_generator()
+
+ custom_generator = generators[current_sequence]
+ while current_length > 0:
+ yield next(custom_generator)
+ current_length = current_length - 1
  • Разичташ на това, че елементите за alphabet в списъка подаден на intertwined_sequences винаги ще имат ключ 'code', което не отговаря на условието.
  • Предаването на параметри към конструкторите от generator_definitions не се получава.

Николай обнови решението на 01.04.2015 18:32 (преди над 9 години)

import math
def fibonacci():
current_number = 1
next_number = 1
yield current_number
yield next_number
while True:
next_number = current_number + next_number
yield next_number
current_number = next_number - current_number
def is_prime(number):
flag = True
current_divider = 2
while flag and current_divider <= math.sqrt(number):
if number % current_divider == 0:
flag = False
current_divider = current_divider + 1
return flag
def primes():
current_number = 2
yield current_number
current_number = current_number + 1
yield current_number
splitter = 2
while True:
current_number = current_number + splitter
if is_prime(current_number):
yield current_number
def alphabet(letters=None, code='lat'):
if letters is not None:
for letter in letters:
yield letter
elif code == 'lat':
symbol = 'a'
counter = 0
while counter < 26:
yield symbol
symbol = chr(ord(symbol) + 1)
counter = counter + 1
elif code == 'bg':
symbol = 'а'
counter = 0
while counter < 30:
yield symbol
symbol = chr(ord(symbol) + 1)
if symbol == 'э' or symbol == 'ы':
symbol = chr(ord(symbol) + 1)
counter = counter + 1
def is_started(generators, sequence):
for generator_name in generators:
if generator_name == sequence:
return True
return False
def get_parameters(items):
- for key in items:
- if key is not 'sequence' and key is not 'length' and key is not 'code':
- return key
- return None
+ keys = items.keys()
+ arguments = ['sequence', 'length', 'code', 'letters']
+ for arg in keys:
+ keys.remove(arg)
+ if keys != []:
+ return keys
+
+
def intertwined_sequences(generator, generator_definitions=None):
generators = {}
for current_item in generator:
current_sequence = current_item['sequence']
current_length = current_item['length']
if current_sequence == 'fibonacci':
if not is_started(generators, current_sequence):
generators[current_sequence] = fibonacci()
fibonacci_numbers = generators[current_sequence]
while current_length > 0:
yield next(fibonacci_numbers)
current_length = current_length - 1
elif current_sequence == 'primes':
if not is_started(generators, current_sequence):
generators[current_sequence] = primes()
prime_numbers = generators[current_sequence]
while current_length > 0:
yield next(prime_numbers)
current_length = current_length - 1
elif current_sequence == 'alphabet':
- current_code = current_item['code']
- current_alphabet = current_sequence + str(current_code)
+ current_alphabet = 'lat'
- if not is_started(generators, current_alphabet):
- generators[current_alphabet] = alphabet(code=current_code)
+ if current_item.get('code') is not None:
+ current_code = current_item['code']
+ current_alphabet = current_sequence + str(current_code)
+
+ if not is_started(generators, current_alphabet):
+ generators[current_alphabet] = alphabet(code=current_code)
+ else:
+ letters = current_item['letters']
+ current_alphabet = current_sequence + str(letters)
+
+ if not is_started(generators,
+ current_alphabet):
+ generators[current_alphabet] = alphabet(letters=letters)
alphabet_symbols = generators[current_alphabet]
while current_length > 0:
yield next(alphabet_symbols)
current_length = current_length - 1
else:
if not is_started(generators, current_sequence):
current_generator = generator_definitions[current_sequence]
parameters = get_parameters(current_item)
if parameters is not None:
argument = current_item[parameters]
generators[current_sequence] = current_generator(argument)
else:
generators[current_sequence] = current_generator()
custom_generator = generators[current_sequence]
while current_length > 0:
yield next(custom_generator)
current_length = current_length - 1

Николай обнови решението на 03.04.2015 12:55 (преди над 9 години)

import math
def fibonacci():
current_number = 1
next_number = 1
yield current_number
yield next_number
while True:
next_number = current_number + next_number
yield next_number
current_number = next_number - current_number
def is_prime(number):
flag = True
current_divider = 2
while flag and current_divider <= math.sqrt(number):
if number % current_divider == 0:
flag = False
current_divider = current_divider + 1
return flag
def primes():
current_number = 2
yield current_number
current_number = current_number + 1
yield current_number
splitter = 2
while True:
current_number = current_number + splitter
if is_prime(current_number):
yield current_number
def alphabet(letters=None, code='lat'):
if letters is not None:
for letter in letters:
yield letter
elif code == 'lat':
- symbol = 'a'
- counter = 0
- while counter < 26:
- yield symbol
- symbol = chr(ord(symbol) + 1)
- counter = counter + 1
+ lat_alphabet = "abcdefghijklmnopqrstuvwxyz"
+ for letter in lat_alphabet:
+ yield letter
+
elif code == 'bg':
- symbol = 'а'
- counter = 0
- while counter < 30:
- yield symbol
+ bg_alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
- symbol = chr(ord(symbol) + 1)
- if symbol == 'э' or symbol == 'ы':
- symbol = chr(ord(symbol) + 1)
- counter = counter + 1
+ for letter in bg_alphabet:
+ yield letter
def is_started(generators, sequence):
for generator_name in generators:
if generator_name == sequence:
return True
return False
-def get_parameters(items):
- keys = items.keys()
- arguments = ['sequence', 'length', 'code', 'letters']
-
- for arg in keys:
- keys.remove(arg)
-
- if keys != []:
- return keys
-
-
def intertwined_sequences(generator, generator_definitions=None):
generators = {}
- for current_item in generator:
- current_sequence = current_item['sequence']
- current_length = current_item['length']
+ for cur_item in generator:
+ keys = list(cur_item.keys())
+ cur_sequence = cur_item.pop('sequence')
+ cur_length = cur_item.pop('length')
- if current_sequence == 'fibonacci':
- if not is_started(generators, current_sequence):
- generators[current_sequence] = fibonacci()
+ if cur_sequence == 'fibonacci':
+ if not is_started(generators, cur_sequence):
+ generators[cur_sequence] = fibonacci()
- fibonacci_numbers = generators[current_sequence]
- while current_length > 0:
+ fibonacci_numbers = generators[cur_sequence]
+ while cur_length > 0:
yield next(fibonacci_numbers)
- current_length = current_length - 1
+ cur_length = cur_length - 1
- elif current_sequence == 'primes':
- if not is_started(generators, current_sequence):
- generators[current_sequence] = primes()
+ elif cur_sequence == 'primes':
+ if not is_started(generators, cur_sequence):
+ generators[cur_sequence] = primes()
- prime_numbers = generators[current_sequence]
- while current_length > 0:
+ prime_numbers = generators[cur_sequence]
+ while cur_length > 0:
yield next(prime_numbers)
- current_length = current_length - 1
+ cur_length = cur_length - 1
- elif current_sequence == 'alphabet':
- current_alphabet = 'lat'
+ elif cur_sequence == 'alphabet':
+ cur_alphabet = 'lat'
- if current_item.get('code') is not None:
- current_code = current_item['code']
- current_alphabet = current_sequence + str(current_code)
+ if cur_item.get('code') is not None:
+ cur_code = cur_item.pop('code')
+ cur_alphabet = cur_sequence + str(cur_code)
- if not is_started(generators, current_alphabet):
- generators[current_alphabet] = alphabet(code=current_code)
+ if not is_started(generators, cur_alphabet):
+ generators[cur_alphabet] = alphabet(code=cur_code)
else:
- letters = current_item['letters']
- current_alphabet = current_sequence + str(letters)
+ letters = cur_item.pop('letters')
+ cur_alphabet = cur_sequence + str(letters)
if not is_started(generators,
- current_alphabet):
- generators[current_alphabet] = alphabet(letters=letters)
+ cur_alphabet):
+ generators[cur_alphabet] = alphabet(letters=letters)
- alphabet_symbols = generators[current_alphabet]
- while current_length > 0:
+ alphabet_symbols = generators[cur_alphabet]
+ while cur_length > 0:
yield next(alphabet_symbols)
- current_length = current_length - 1
+ cur_length = cur_length - 1
else:
- if not is_started(generators, current_sequence):
- current_generator = generator_definitions[current_sequence]
- parameters = get_parameters(current_item)
+ if not is_started(generators, cur_sequence):
+ cur_gen = generator_definitions[cur_sequence]
- if parameters is not None:
- argument = current_item[parameters]
- generators[current_sequence] = current_generator(argument)
+ parameters = list(cur_item.keys())
+ if parameters != []:
+ generators[cur_sequence] = iter(cur_gen(**cur_item))
else:
- generators[current_sequence] = current_generator()
+ generators[cur_sequence] = iter(cur_gen())
- custom_generator = generators[current_sequence]
- while current_length > 0:
+ custom_generator = generators[cur_sequence]
+ while cur_length > 0:
yield next(custom_generator)
- current_length = current_length - 1
+ cur_length = cur_length - 1