Ask Question

The slice_gen generator takes one iterable and a start, stop and step values (all int, with the same meanings as the values in a slice: [start:stop:step], except start and stop must be non-negative and step must be positive; raise an Assetio nerror exception if any is not). It produces all the values in what would be the slice (without every putting all the values in a list and slicing it). For example

+5
Answers (1)
  1. 31 March, 15:47
    0
    See Explaination

    Explanation:

    def slice_gen (iterable, start, stop, step):

    it = iter (iterable)

    if start < 0 or stop < 0 or step < = 0:

    raise Assertio nerror

    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 ("")
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “The slice_gen generator takes one iterable and a start, stop and step values (all int, with the same meanings as the values in a slice: ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers