Write python generator: There are always positive values for those parameters. d
ID: 3833013 • Letter: W
Question
Write python generator:
There are always positive values for those parameters.
def slice_gen(iterable, start, stop, step):
pass
Explanation / Answer
def slice_gen(iterable, start, stop, step):
it = iter(iterable)
if start < 0 or stop < 0 or step <= 0:
raise AssertionError
count = -1
nextI = start
while True:
count += 1
try:
item = next(it)
if count == stop:
break
elif count == nextI:
nextI = nextI + step
yield item
except:
return
for i in slice_gen('abcdefghijk', 3, 7, 1):
print(i, end='')
print("")
# code link: https://paste.ee/p/7Uox9
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.