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

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

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

Резултати

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

Код

def fibonacci():
x1 = 0
x2 = 1
while True:
yield x2
x1, x2 = x2, x1 + x2
def primes():
prime = 2
while True:
for i in range(2, int(prime / 2) + 1):
if prime % i == 0:
break
else:
yield prime
prime += 1
def alphabet(code = "", letters = ""):
if letters == "":
if code == "lat":
alphabet = "abcdefghijklmnopqrstuvwxyz"
elif code == "bg":
alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
else:
print("Invalid input")
else:
alphabet = letters
i = 0
while True:
yield alphabet[i]
i += 1
def intertwined_sequences(data):
intertwined = []
funcs = []
funcvars = []
for i in data:
if i["sequence"] not in funcs:
funcs.append(i["sequence"])
if (len(i)) > 2:
for e in i:
if e != "sequence" and e != "length":
funcvars.append(i[e])
else:
funcvars.append("")
for n in funcs:
index = funcs.index(n)
c = funcvars[funcs.index(n)]
if c != "":
funcs[index] = eval(n)(c)
else:
funcs[index] = eval(n)()
for i in data:
if n == i["sequence"]:
(i["sequence"]) = funcs[index]
for i in data:
x = 0
while x < i["length"]:
yield(next(i["sequence"]))
x += 1

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

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

======================================================================
ERROR: test_infinite_intertwined (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_kwargs_generator (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 8.868s

FAILED (errors=4)

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

Васил обнови решението на 03.04.2015 10:06 (преди около 9 години)

+def fibonacci():
+ x1 = 0
+ x2 = 1
+ while True:
+ yield x2
+ x1, x2 = x2, x1 + x2
+
+def primes():
+ prime = 2
+ while True:
+ for i in range(2, int(prime / 2) + 1):
+ if prime % i == 0:
+ break
+ else:
+ yield prime
+ prime += 1
+
+def alphabet(code = "", letters = ""):
+ if letters == "":
+ if code == "lat":
+ alphabet = "abcdefghijklmnopqrstuvwxyz"
+ elif code == "bg":
+ alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ else:
+ print("Invalid input")
+ else:
+ alphabet = letters
+ i = 0
+ while True:
+ yield alphabet[i]
+ i += 1
+
+def intertwined_sequences(data):
+ intertwined = []
+ funcs = []
+ funcvars = []
+ for i in data:
+ if i["sequence"] not in funcs:
+ funcs.append(i["sequence"])
+ if (len(i)) > 2:
+ for e in i:
+ if e != "sequence" and e != "length":
+ funcvars.append(i[e])
+ else:
+ funcvars.append("")
+ for n in funcs:
+ index = funcs.index(n)
+ c = funcvars[funcs.index(n)]
+ if c != "":
+ funcs[index] = eval(n)(c)
+ else:
+ funcs[index] = eval(n)()
+ for i in data:
+ if n == i["sequence"]:
+ (i["sequence"]) = funcs[index]
+ for i in data:
+ x = 0
+ while x < i["length"]:
+ yield(next(i["sequence"]))
+ x += 1

Васил обнови решението на 03.04.2015 10:12 (преди около 9 години)

def fibonacci():
- x1 = 0
- x2 = 1
- while True:
- yield x2
- x1, x2 = x2, x1 + x2
+ x1 = 0
+ x2 = 1
+ while True:
+ yield x2
+ x1, x2 = x2, x1 + x2
def primes():
- prime = 2
- while True:
- for i in range(2, int(prime / 2) + 1):
- if prime % i == 0:
- break
- else:
- yield prime
- prime += 1
+ prime = 2
+ while True:
+ for i in range(2, int(prime / 2) + 1):
+ if prime % i == 0:
+ break
+ else:
+ yield prime
+ prime += 1
def alphabet(code = "", letters = ""):
- if letters == "":
- if code == "lat":
- alphabet = "abcdefghijklmnopqrstuvwxyz"
- elif code == "bg":
- alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
- else:
- print("Invalid input")
- else:
- alphabet = letters
- i = 0
- while True:
- yield alphabet[i]
- i += 1
+ if letters == "":
+ if code == "lat":
+ alphabet = "abcdefghijklmnopqrstuvwxyz"
+ elif code == "bg":
+ alphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ else:
+ print("Invalid input")
+ else:
+ alphabet = letters
+ i = 0
+ while True:
+ yield alphabet[i]
+ i += 1
def intertwined_sequences(data):
- intertwined = []
- funcs = []
+ intertwined = []
- funcvars = []
+ funcs = []
- for i in data:
+ funcvars = []
- if i["sequence"] not in funcs:
+ for i in data:
- funcs.append(i["sequence"])
+ if i["sequence"] not in funcs:
- if (len(i)) > 2:
+ funcs.append(i["sequence"])
- for e in i:
+ if (len(i)) > 2:
- if e != "sequence" and e != "length":
+ for e in i:
- funcvars.append(i[e])
+ if e != "sequence" and e != "length":
- else:
+ funcvars.append(i[e])
- funcvars.append("")
+ else:
- for n in funcs:
+ funcvars.append("")
- index = funcs.index(n)
+ for n in funcs:
- c = funcvars[funcs.index(n)]
+ index = funcs.index(n)
- if c != "":
+ c = funcvars[funcs.index(n)]
- funcs[index] = eval(n)(c)
+ if c != "":
- else:
+ funcs[index] = eval(n)(c)
- funcs[index] = eval(n)()
+ else:
- for i in data:
+ funcs[index] = eval(n)()
- if n == i["sequence"]:
+ for i in data:
- (i["sequence"]) = funcs[index]
+ if n == i["sequence"]:
- for i in data:
+ (i["sequence"]) = funcs[index]
- x = 0
+ for i in data:
- while x < i["length"]:
+ x = 0
- yield(next(i["sequence"]))
+ while x < i["length"]:
- x += 1
+ yield(next(i["sequence"]))
+ x += 1

Ми мога само да изкоментирам, че като цяло генераторите ми работят, но с недостатъци.

Неможах да комбинирам всички условия на задачата. В името на това генератирите да продължават, за момента немога да модифицирам кода си, така че да разпознава повече от един генератори на азбука.

И втория е проблем са функции с повече от един аргумента. Немога да измисля достъпен начин за достъп и различаване на два аргумента при изградената до сега структура на кода ми.

Ще се радвам да чуя някакви насоки.