Решение на Генератори и итератори от Калоян Витанов

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

Към профила на Калоян Витанов

Резултати

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

Код

def fibonacci():
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes():
x = 3
yield 2
while True:
yield x
indicator = 0
x = x+1
while indicator == 0:
a = x
y = x//2
for i in range(2, y+1):
if x % i == 0:
x = x+1
break
if a == x:
indicator = 1
def alphabet(*, letters=False, code=False):
if letters:
the_yield = iter(letters)
while True:
yield next(the_yield)
elif code == 'bg':
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
the_yield = iter(bg)
while True:
yield next(the_yield)
elif code == 'lat':
lat = 'abcdefghijklmnopqrstuvwxyz'
the_yield = iter(lat)
while True:
yield next(the_yield)
#
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(the_data, *, generator_definitions=False):
# k_g_f_dict -> known_generator_functions_dict
k_g_f_dict = {
'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet
}
# i_g_d -> instanced_generators_dict
i_g_dict = {}
# c_c_dict -> corresponding_count_dict
c_c_dict = {}
infinite_generator_alert = False
terminator = 0
if generator_definitions:
for key, value in generator_definitions.items():
k_g_f_dict[key] = value
import types
if isinstance(the_data, types.GeneratorType):
infinite_generator_alert = True
while terminator == 0:
if infinite_generator_alert:
helping_list = []
helping_list.append(next(the_data))
data = helping_list
lenght_of_range = 1
else:
data = the_data
lenght_of_range = len(data)
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
if type_of_sequence not in i_g_dict.keys():
indicator = 0
argument_dict = {}
for key, value in data[i].items():
if key not in ['sequence', 'length']:
argument_dict[key] = value
indicator = 1
if indicator == 1:
i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence](**argument_dict))
c_c_dict[type_of_sequence] = 0
else:
i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence]())
c_c_dict[type_of_sequence] = 0
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
counter = c_c_dict[type_of_sequence]
c_c_dict[type_of_sequence] += data[i]['length']
while counter < c_c_dict[type_of_sequence]:
yield next(i_g_dict[type_of_sequence])
counter = counter + 1
if not infinite_generator_alert:
terminator = 1

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

.......E......
======================================================================
ERROR: test_infinite_intertwined (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.104s

FAILED (errors=1)

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

Калоян обнови решението на 31.03.2015 22:02 (преди почти 9 години)

+def fibonacci():
+ x, y = 0, 1
+ while True:
+ yield y
+ x, y = y, x+y
+
+
+def primes():
+ x = 3
+ yield 2
+ while True:
+ yield x
+ indicator = 0
+ x = x+1
+ while indicator == 0:
+ a = x
+ y = x//2
+ for i in range(2, y+1):
+ if x % i == 0:
+ x = x+1
+ break
+ if a == x:
+ indicator = 1
+
+
+def alphabet(*, letters=False, code=False):
+ if letters:
+ the_yield = iter(letters)
+ while True:
+ yield next(the_yield)
+ elif code == 'bg':
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ the_yield = iter(bg)
+ while True:
+ yield next(the_yield)
+ elif code == 'lat':
+ lat = 'abcdefghijklmnopqrstuvwxyz'
+ the_yield = iter(lat)
+ while True:
+ yield next(the_yield)
+ else:
+ raise ValueError('Incorrect input!')
+
+
+#
+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(the_data, generator_definitions=False):
+ # k_g_f_dict -> known_generator_functions_dict
+ k_g_f_dict = {
+ 'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet
+ }
+ # i_g_d -> instanced_generators_dict
+ i_g_dict = {}
+ # c_c_dict -> corresponding_count_dict
+ c_c_dict = {}
+
+ infinite_generator_alert = False
+ terminator = 0
+
+ if generator_definitions:
+ for key, value in generator_definitions.items():
+ k_g_f_dict[key] = value
+
+ import types
+ if isinstance(the_data, types.GeneratorType):
+ infinite_generator_alert = True
+
+
+ while terminator == 0:
+
+ if infinite_generator_alert:
+ helping_list = []
+ helping_list.append(next(the_data))
+ data = helping_list
+ lenght_of_range = 1
+ else:
+ data = the_data
+ lenght_of_range = len(data)
+
+ for i in range(lenght_of_range):
+ type_of_sequence = data[i]['sequence']
+ if type_of_sequence not in i_g_dict.keys():
+ indicator = 0
+ for key in data[i].keys():
+ argument_list = []
+ if key not in ['sequence', 'length']:
+ if type_of_sequence == 'alphabet':
+ if 'code' in data[i].keys():
+ i_g_dict[type_of_sequence] = alphabet(code=data[i]['code'])
+ elif 'letters' in data[i].keys():
+ i_g_dict[type_of_sequence] = alphabet(letters=data[i]['letters'])
+ c_c_dict[type_of_sequence] = 0
+ indicator = 1
+ else:
+ # caution for argument position
+ argument_list.append(data[i][key])
+ i_g_dict[type_of_sequence] = k_g_f_dict[type_of_sequence](*argument_list)
+ c_c_dict[type_of_sequence] = 0
+ indicator = 1
+
+ if indicator == 0:
+ i_g_dict[type_of_sequence] = k_g_f_dict[type_of_sequence]()
+ c_c_dict[type_of_sequence] = 0
+
+ for i in range(lenght_of_range):
+ type_of_sequence = data[i]['sequence']
+ counter = c_c_dict[type_of_sequence]
+ c_c_dict[type_of_sequence] += data[i]['length']
+
+ while counter < c_c_dict[type_of_sequence]:
+ yield next(i_g_dict[type_of_sequence])
+ counter = counter + 1
+
+ if not infinite_generator_alert:
+ terminator = 1

Калоян обнови решението на 31.03.2015 22:03 (преди почти 9 години)

def fibonacci():
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes():
x = 3
yield 2
while True:
yield x
indicator = 0
x = x+1
while indicator == 0:
a = x
y = x//2
for i in range(2, y+1):
if x % i == 0:
x = x+1
break
if a == x:
indicator = 1
def alphabet(*, letters=False, code=False):
if letters:
the_yield = iter(letters)
while True:
yield next(the_yield)
elif code == 'bg':
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
the_yield = iter(bg)
while True:
yield next(the_yield)
elif code == 'lat':
lat = 'abcdefghijklmnopqrstuvwxyz'
the_yield = iter(lat)
while True:
yield next(the_yield)
else:
raise ValueError('Incorrect input!')
#
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(the_data, generator_definitions=False):
# k_g_f_dict -> known_generator_functions_dict
k_g_f_dict = {
'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet
}
# i_g_d -> instanced_generators_dict
i_g_dict = {}
# c_c_dict -> corresponding_count_dict
c_c_dict = {}
-
+
infinite_generator_alert = False
terminator = 0
if generator_definitions:
for key, value in generator_definitions.items():
k_g_f_dict[key] = value
import types
if isinstance(the_data, types.GeneratorType):
infinite_generator_alert = True
-
while terminator == 0:
if infinite_generator_alert:
helping_list = []
helping_list.append(next(the_data))
data = helping_list
lenght_of_range = 1
else:
data = the_data
lenght_of_range = len(data)
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
if type_of_sequence not in i_g_dict.keys():
indicator = 0
- for key in data[i].keys():
- argument_list = []
+ argument_dict = {}
+ for key, value in data[i].items():
if key not in ['sequence', 'length']:
- if type_of_sequence == 'alphabet':
- if 'code' in data[i].keys():
- i_g_dict[type_of_sequence] = alphabet(code=data[i]['code'])
- elif 'letters' in data[i].keys():
- i_g_dict[type_of_sequence] = alphabet(letters=data[i]['letters'])
- c_c_dict[type_of_sequence] = 0
- indicator = 1
- else:
- # caution for argument position
- argument_list.append(data[i][key])
- i_g_dict[type_of_sequence] = k_g_f_dict[type_of_sequence](*argument_list)
- c_c_dict[type_of_sequence] = 0
- indicator = 1
-
- if indicator == 0:
- i_g_dict[type_of_sequence] = k_g_f_dict[type_of_sequence]()
+ argument_dict[key] = value
+ indicator = 1
+ if indicator == 1:
+ i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence](**argument_dict))
+ c_c_dict[type_of_sequence] = 0
+ else:
+ i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence]())
c_c_dict[type_of_sequence] = 0
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
counter = c_c_dict[type_of_sequence]
c_c_dict[type_of_sequence] += data[i]['length']
while counter < c_c_dict[type_of_sequence]:
yield next(i_g_dict[type_of_sequence])
counter = counter + 1
if not infinite_generator_alert:
terminator = 1

Калоян обнови решението на 03.04.2015 00:17 (преди почти 9 години)

def fibonacci():
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes():
x = 3
yield 2
while True:
yield x
indicator = 0
x = x+1
while indicator == 0:
a = x
y = x//2
for i in range(2, y+1):
if x % i == 0:
x = x+1
break
if a == x:
indicator = 1
def alphabet(*, letters=False, code=False):
if letters:
the_yield = iter(letters)
while True:
yield next(the_yield)
elif code == 'bg':
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
the_yield = iter(bg)
while True:
yield next(the_yield)
elif code == 'lat':
lat = 'abcdefghijklmnopqrstuvwxyz'
the_yield = iter(lat)
while True:
yield next(the_yield)
- else:
- raise ValueError('Incorrect input!')
#
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(the_data, generator_definitions=False):
# k_g_f_dict -> known_generator_functions_dict
k_g_f_dict = {
'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet
}
# i_g_d -> instanced_generators_dict
i_g_dict = {}
# c_c_dict -> corresponding_count_dict
c_c_dict = {}
infinite_generator_alert = False
terminator = 0
if generator_definitions:
for key, value in generator_definitions.items():
k_g_f_dict[key] = value
import types
if isinstance(the_data, types.GeneratorType):
infinite_generator_alert = True
while terminator == 0:
if infinite_generator_alert:
helping_list = []
helping_list.append(next(the_data))
data = helping_list
lenght_of_range = 1
else:
data = the_data
lenght_of_range = len(data)
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
if type_of_sequence not in i_g_dict.keys():
indicator = 0
argument_dict = {}
for key, value in data[i].items():
if key not in ['sequence', 'length']:
argument_dict[key] = value
indicator = 1
if indicator == 1:
i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence](**argument_dict))
c_c_dict[type_of_sequence] = 0
else:
i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence]())
c_c_dict[type_of_sequence] = 0
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
counter = c_c_dict[type_of_sequence]
c_c_dict[type_of_sequence] += data[i]['length']
while counter < c_c_dict[type_of_sequence]:
yield next(i_g_dict[type_of_sequence])
counter = counter + 1
if not infinite_generator_alert:
terminator = 1

Калоян обнови решението на 03.04.2015 14:53 (преди почти 9 години)

def fibonacci():
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes():
x = 3
yield 2
while True:
yield x
indicator = 0
x = x+1
while indicator == 0:
a = x
y = x//2
for i in range(2, y+1):
if x % i == 0:
x = x+1
break
if a == x:
indicator = 1
def alphabet(*, letters=False, code=False):
if letters:
the_yield = iter(letters)
while True:
yield next(the_yield)
elif code == 'bg':
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
the_yield = iter(bg)
while True:
yield next(the_yield)
elif code == 'lat':
lat = 'abcdefghijklmnopqrstuvwxyz'
the_yield = iter(lat)
while True:
yield next(the_yield)
#
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(the_data, generator_definitions=False):
+def intertwined_sequences(the_data, *, generator_definitions=False):
# k_g_f_dict -> known_generator_functions_dict
k_g_f_dict = {
'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet
}
# i_g_d -> instanced_generators_dict
i_g_dict = {}
# c_c_dict -> corresponding_count_dict
c_c_dict = {}
infinite_generator_alert = False
terminator = 0
if generator_definitions:
for key, value in generator_definitions.items():
k_g_f_dict[key] = value
import types
if isinstance(the_data, types.GeneratorType):
infinite_generator_alert = True
while terminator == 0:
if infinite_generator_alert:
helping_list = []
helping_list.append(next(the_data))
data = helping_list
lenght_of_range = 1
else:
data = the_data
lenght_of_range = len(data)
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
if type_of_sequence not in i_g_dict.keys():
indicator = 0
argument_dict = {}
for key, value in data[i].items():
if key not in ['sequence', 'length']:
argument_dict[key] = value
indicator = 1
if indicator == 1:
i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence](**argument_dict))
c_c_dict[type_of_sequence] = 0
else:
i_g_dict[type_of_sequence] = iter(k_g_f_dict[type_of_sequence]())
c_c_dict[type_of_sequence] = 0
for i in range(lenght_of_range):
type_of_sequence = data[i]['sequence']
counter = c_c_dict[type_of_sequence]
c_c_dict[type_of_sequence] += data[i]['length']
while counter < c_c_dict[type_of_sequence]:
yield next(i_g_dict[type_of_sequence])
counter = counter + 1
if not infinite_generator_alert:
terminator = 1