Решение на Генератори и итератори от Илиян Якимов

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

Към профила на Илиян Якимов

Резултати

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

Код

def fibonacci():
first = 1
second = 1
while True:
yield first
temp = first
first = second
second += temp
def is_prime(n):
n = abs(n)
i = 2
if n == 1:
return False
while i <= (n + 1) // 2:
if n % i == 0:
return False
i += 1
return True
def next_prime(n):
if is_prime(n):
return n
else:
return next_prime(n + 1)
def primes():
first_prime = 2
while True:
current = next_prime(first_prime)
first_prime = current + 1
yield current
def alphabet(code="", letters=""):
bulgarian = "абвгдежзийклмнопрстуфхцчшщъьюя"
latin = "abcdefghijklmnopqrstuvwxyz"
while True:
if letters:
for x in letters:
yield x
raise StopIteration
elif code == 'bg':
for x in bulgarian:
yield x
raise StopIteration
else:
for x in latin:
yield x
raise StopIteration
def intertwined_sequences(catalogue, generator_definitions={}):
primes_numbers = primes()
fibonacci_numbers = fibonacci()
lettering_bg = alphabet('bg')
lettering_lat = alphabet('lat')
whose_alphabet = []
generator_with_arg = {}
arguments_gen = {}
iterators = {}
for condition in catalogue:
if condition['sequence'] == 'alphabet' and not whose_alphabet:
if 'code' in condition:
if condition['code'] == 'bg':
whose_alphabet.append(lettering_bg)
else:
whose_alphabet.append(lettering_lat)
else:
whose_alphabet.append(alphabet("", condition['letters']))
if condition['sequence'] == 'alphabet':
if 'code' in condition:
if 'code' == 'bg':
for x in range(1, condition['length'] + 1):
yield next(whose_alphabet[0])
else:
for x in range(1, condition['length'] + 1):
yield next(whose_alphabet[0])
else:
for x in range(condition['length']):
yield next(whose_alphabet[0])
elif condition['sequence'] == 'fibonacci':
for x in range(1, condition['length'] + 1):
yield next(fibonacci_numbers)
elif condition['sequence'] == 'primes':
for x in range(1, condition['length'] + 1):
yield next(primes_numbers)
else:
if len(condition.keys()) > 2 and \
condition['sequence'] not in generator_with_arg:
for x in condition:
if x != 'sequence' and x != 'length':
arguments_gen[x] = condition[x]
generator_with_arg[condition['sequence']] = arguments_gen
if len(condition.keys()) <= 2:
temp_iterator = generator_definitions[condition['sequence']]()
iterator = iter(temp_iterator)
if condition['sequence'] not in iterators:
iterators[condition['sequence']] = iterator
else:
caller = generator_definitions[condition['sequence']]
called = generator_with_arg[condition['sequence']]
iterator = iter(caller(**called))
if condition['sequence'] not in iterators:
iterators[condition['sequence']] = iterator
for x in range(condition['length']):
yield next(iterators[condition['sequence']])

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

..............
----------------------------------------------------------------------
Ran 14 tests in 1.966s

OK

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

Илиян обнови решението на 30.03.2015 13:45 (преди над 9 години)

+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ temp = first
+ first = second
+ second += temp
+
+
+def is_prime(n):
+ n = abs(n)
+ i = 2
+ if n == 1:
+ return False
+ while i <= (n + 1) // 2:
+ if n % i == 0:
+ return False
+ i += 1
+ return True
+
+
+def next_prime(n):
+ if is_prime(n):
+ return n
+ else:
+ return next_prime(n + 1)
+
+
+def primes():
+ first_prime = 2
+ while True:
+ current = next_prime(first_prime)
+ first_prime = current + 1
+ yield current
+
+
+def alphabet(code = "", letters = ""):
+ bulgarian = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ latin = "abcdefghijklmnopqrstuvwxyz"
+ while True:
+ if letters:
+ for x in letters:
+ yield x
+
+ elif code == 'bg':
+ for x in bulgarian:
+ yield x
+
+ else:
+ for x in latin:
+ yield x
+
+
+def intertwined_sequences(catalogue, generator_definitions={}):
+ primes_numbers = primes()
+ fibonacci_numbers = fibonacci()
+ lettering_bg = alphabet('bg')
+ lettering_lat = alphabet('lat')
+ whose_alphabet = []
+ generator_with_arg = {}
+
+ for condition in catalogue:
+ if condition['sequence'] == 'alphabet' and not whose_alphabet:
+ if 'code' in condition:
+ if condition['code'] == 'bg':
+ whose_alphabet.append(lettering_bg)
+ else:
+ whose_alphabet.append(lettering_lat)
+ else:
+ whose_alphabet.append(alphabet("", condition['letters']))
+
+ if condition['sequence'] == 'alphabet':
+ if 'code' in condition:
+ if 'code' == 'bg':
+ for x in range(1, condition['length'] + 1):
+ yield next(whose_alphabet[0])
+ else:
+ for x in range(1, condition['length'] + 1):
+ yield next(whose_alphabet[0])
+ else:
+ for x in range(condition['length']):
+ yield next(whose_alphabet[0])
+
+ elif condition['sequence'] == 'fibonacci':
+ for x in range(1, condition['length'] + 1):
+ yield next(fibonacci_numbers)
+
+ elif condition['sequence'] == 'primes':
+ for x in range(1, condition['length'] + 1):
+ yield next(primes_numbers)
+
+ else:
+ if 'num' in condition:
+ generator_with_arg[condition['sequence']] = condition['num']
+
+ if condition['sequence'] in generator_with_arg:
+ arg = generator_with_arg[condition['sequence']]
+ generator = generator_definitions[condition['sequence']](arg)
+ for x in range(condition['length']):
+ yield next(generator)
+ else:
+ generator = generator_definitions[condition['sequence']]()
+ for x in range(condition['length']):
+ yield next(generator)

Илиян обнови решението на 31.03.2015 19:46 (преди над 9 години)

def fibonacci():
first = 1
second = 1
while True:
yield first
temp = first
first = second
second += temp
def is_prime(n):
n = abs(n)
i = 2
if n == 1:
return False
while i <= (n + 1) // 2:
if n % i == 0:
return False
i += 1
return True
def next_prime(n):
if is_prime(n):
return n
else:
return next_prime(n + 1)
def primes():
first_prime = 2
while True:
current = next_prime(first_prime)
first_prime = current + 1
yield current
-def alphabet(code = "", letters = ""):
+def alphabet(code="", letters=""):
bulgarian = "абвгдежзийклмнопрстуфхцчшщъьюя"
latin = "abcdefghijklmnopqrstuvwxyz"
while True:
if letters:
for x in letters:
yield x
-
+
elif code == 'bg':
for x in bulgarian:
yield x
-
+
else:
for x in latin:
yield x
def intertwined_sequences(catalogue, generator_definitions={}):
primes_numbers = primes()
fibonacci_numbers = fibonacci()
lettering_bg = alphabet('bg')
lettering_lat = alphabet('lat')
whose_alphabet = []
generator_with_arg = {}
+ arguments_gen = {}
+ iterators = {}
for condition in catalogue:
if condition['sequence'] == 'alphabet' and not whose_alphabet:
if 'code' in condition:
if condition['code'] == 'bg':
whose_alphabet.append(lettering_bg)
else:
whose_alphabet.append(lettering_lat)
else:
- whose_alphabet.append(alphabet("", condition['letters']))
-
+ whose_alphabet.append(alphabet("", condition['letters']))
+
if condition['sequence'] == 'alphabet':
if 'code' in condition:
if 'code' == 'bg':
for x in range(1, condition['length'] + 1):
yield next(whose_alphabet[0])
else:
for x in range(1, condition['length'] + 1):
yield next(whose_alphabet[0])
else:
for x in range(condition['length']):
yield next(whose_alphabet[0])
elif condition['sequence'] == 'fibonacci':
for x in range(1, condition['length'] + 1):
yield next(fibonacci_numbers)
-
+
elif condition['sequence'] == 'primes':
for x in range(1, condition['length'] + 1):
yield next(primes_numbers)
else:
- if 'num' in condition:
- generator_with_arg[condition['sequence']] = condition['num']
-
- if condition['sequence'] in generator_with_arg:
- arg = generator_with_arg[condition['sequence']]
- generator = generator_definitions[condition['sequence']](arg)
- for x in range(condition['length']):
- yield next(generator)
+ if len(condition.keys()) > 2 and \
+ condition['sequence'] not in generator_with_arg:
+ for x in condition:
+ if x != 'sequence' and x != 'length':
+ arguments_gen[x] = condition[x]
+ generator_with_arg[condition['sequence']] = arguments_gen
+
+ if len(condition.keys()) <= 2:
+ temp_iterator = generator_definitions[condition['sequence']]()
+ iterator = iter(temp_iterator)
+ if condition['sequence'] not in iterators:
+ iterators[condition['sequence']] = iterator
else:
- generator = generator_definitions[condition['sequence']]()
- for x in range(condition['length']):
+ caller = generator_definitions[condition['sequence']]
- yield next(generator)
+ called = generator_with_arg[condition['sequence']]
+ iterator = iter(caller(**called))
+ if condition['sequence'] not in iterators:
+ iterators[condition['sequence']] = iterator
+
+ for x in range(condition['length']):
+ yield next(iterators[condition['sequence']])

Илиян обнови решението на 01.04.2015 21:31 (преди над 9 години)

def fibonacci():
first = 1
second = 1
while True:
yield first
temp = first
first = second
second += temp
def is_prime(n):
n = abs(n)
i = 2
if n == 1:
return False
while i <= (n + 1) // 2:
if n % i == 0:
return False
i += 1
return True
def next_prime(n):
if is_prime(n):
return n
else:
return next_prime(n + 1)
def primes():
first_prime = 2
while True:
current = next_prime(first_prime)
first_prime = current + 1
yield current
def alphabet(code="", letters=""):
bulgarian = "абвгдежзийклмнопрстуфхцчшщъьюя"
latin = "abcdefghijklmnopqrstuvwxyz"
while True:
if letters:
for x in letters:
yield x
-
+ raise StopIteration
elif code == 'bg':
for x in bulgarian:
yield x
-
+ raise StopIteration
else:
for x in latin:
yield x
+ raise StopIteration
def intertwined_sequences(catalogue, generator_definitions={}):
primes_numbers = primes()
fibonacci_numbers = fibonacci()
lettering_bg = alphabet('bg')
lettering_lat = alphabet('lat')
whose_alphabet = []
generator_with_arg = {}
arguments_gen = {}
iterators = {}
for condition in catalogue:
if condition['sequence'] == 'alphabet' and not whose_alphabet:
if 'code' in condition:
if condition['code'] == 'bg':
whose_alphabet.append(lettering_bg)
else:
whose_alphabet.append(lettering_lat)
else:
whose_alphabet.append(alphabet("", condition['letters']))
if condition['sequence'] == 'alphabet':
if 'code' in condition:
if 'code' == 'bg':
for x in range(1, condition['length'] + 1):
yield next(whose_alphabet[0])
else:
for x in range(1, condition['length'] + 1):
yield next(whose_alphabet[0])
else:
for x in range(condition['length']):
yield next(whose_alphabet[0])
elif condition['sequence'] == 'fibonacci':
for x in range(1, condition['length'] + 1):
yield next(fibonacci_numbers)
elif condition['sequence'] == 'primes':
for x in range(1, condition['length'] + 1):
yield next(primes_numbers)
else:
if len(condition.keys()) > 2 and \
condition['sequence'] not in generator_with_arg:
for x in condition:
if x != 'sequence' and x != 'length':
arguments_gen[x] = condition[x]
generator_with_arg[condition['sequence']] = arguments_gen
if len(condition.keys()) <= 2:
temp_iterator = generator_definitions[condition['sequence']]()
iterator = iter(temp_iterator)
if condition['sequence'] not in iterators:
iterators[condition['sequence']] = iterator
else:
caller = generator_definitions[condition['sequence']]
called = generator_with_arg[condition['sequence']]
iterator = iter(caller(**called))
if condition['sequence'] not in iterators:
iterators[condition['sequence']] = iterator
for x in range(condition['length']):
yield next(iterators[condition['sequence']])