Решение на Генератори и итератори от Атанас Янчевски

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

Към профила на Атанас Янчевски

Резултати

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

Код

def fibonacci():
value1, value2 = 1, 1
while True:
yield value1
value2 += value1
value1 = value2 - value1
def is_prime(value):
sum = 0
for d in range(1, value+1):
if value % d == 0:
sum += d
return sum == value+1
def primes():
value = 2
while True:
if is_prime(value):
yield value
value += 1
BG_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
ENG_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def alphabet(code='', letters=''):
value = 0
if letters:
while True:
yield letters[value]
value += 1
else:
if code == 'bg':
while True:
yield BG_ALPHABET[value]
value += 1
else:
while True:
yield ENG_ALPHABET[value]
value += 1
BASIC_GENERATOR_DEFINITIONS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def intertwined_sequences(iterable_object, generator_definitions={}):
generator_definitions.update(BASIC_GENERATOR_DEFINITIONS)
used_generators = {}
for element in iterable_object:
arguments = {key: value for key, value in element.items()
if key != 'sequence' and key != 'length'}
if element['sequence'] not in used_generators:
generator = iter(generator_definitions[element['sequence']](**arguments))
used_generators.update({element['sequence']: generator})
print(type(used_generators[element['sequence']]))
value = 0
while value < element['length']:
yield next(used_generators[element['sequence']])
value += 1

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

.E..........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_for_larger_prime (test.TestPrimes)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 4.351s

FAILED (errors=2)

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

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

+def fibonacci():
+ value1, value2 = 1, 1
+ while True:
+ yield value1
+ value2 += value1
+ value1 = value2 - value1
+
+
+def is_prime(value):
+ sum = 0
+ for d in range(1, value+1):
+ if value % d == 0:
+ sum += d
+ return sum == value+1
+
+
+def primes():
+ value = 2
+ while True:
+ if is_prime(value):
+ yield value
+ value += 1
+
+
+BG_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ENG_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
+
+
+def alphabet(code='', letters=''):
+ value = 0
+ if letters:
+ while True:
+ yield letters[value]
+ value += 1
+ else:
+ if code == 'bg':
+ while True:
+ yield BG_ALPHABET[value]
+ value += 1
+ else:
+ while True:
+ yield ENG_ALPHABET[value]
+ value += 1
+
+
+BASIC_GENERATOR_DEFINITIONS = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+}
+
+
+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
+
+
+def intertwined_sequences(iterable_object, generator_definitions={}):
+ generator_definitions.update(BASIC_GENERATOR_DEFINITIONS)
+ used_generators = {}
+ for element in iterable_object:
+ arguments = {key: value for key, value in element.items()
+ if key != 'sequence' and key != 'length'}
+ if element['sequence'] not in used_generators:
+ generator = iter(generator_definitions[element['sequence']](**arguments))
+ used_generators.update({element['sequence']: generator})
+ print(type(used_generators[element['sequence']]))
+ value = 0
+ while value < element['length']:
+ yield next(used_generators[element['sequence']])
+ value += 1

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

def fibonacci():
value1, value2 = 1, 1
while True:
yield value1
value2 += value1
value1 = value2 - value1
def is_prime(value):
sum = 0
for d in range(1, value+1):
if value % d == 0:
sum += d
return sum == value+1
def primes():
value = 2
while True:
if is_prime(value):
yield value
value += 1
BG_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
ENG_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def alphabet(code='', letters=''):
value = 0
if letters:
while True:
yield letters[value]
value += 1
else:
if code == 'bg':
while True:
yield BG_ALPHABET[value]
value += 1
else:
while True:
yield ENG_ALPHABET[value]
value += 1
BASIC_GENERATOR_DEFINITIONS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
-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
-
-
def intertwined_sequences(iterable_object, generator_definitions={}):
generator_definitions.update(BASIC_GENERATOR_DEFINITIONS)
used_generators = {}
for element in iterable_object:
arguments = {key: value for key, value in element.items()
if key != 'sequence' and key != 'length'}
if element['sequence'] not in used_generators:
generator = iter(generator_definitions[element['sequence']](**arguments))
used_generators.update({element['sequence']: generator})
print(type(used_generators[element['sequence']]))
value = 0
while value < element['length']:
yield next(used_generators[element['sequence']])
value += 1