Question

IN PYTHON 3 LANGUAGE The windows generator takes an iterable and two ints (call them n...

IN PYTHON 3 LANGUAGE

The windows generator takes an iterable and two ints (call them n and m; with m’s default value 1) as parameters: it produces lists of n values: the first list contains the first n values; every subsequent list drops the first m from the previous list and adds the next m values from the iterable, until there are fewer than n values to put in the returned list.

call iter and next directly, and use a list that always contains n values, so it doesn’t violate the conditions for using iterables

SAMPLE OUTPUT:

For example:

for i in windows('abcdefghijk', 4,2):

print(i,end='')

prints ['a','b','c','d'] ['c','d','e','f'] ['e','f','g','h'] ['g','h','i','j'].


for i in windows('abcdefghijk', 3,2):

print(i,end='')

prints ['a', 'b', 'c'], ['c', 'd', 'e'], ['e', 'f', 'g'], ['g', 'h', 'i'], ['i', 'j', 'k']

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def windows(itr, n, m):
    lst = []
    index = 0
    for i in range(index, len(itr)):
        l = []
        for j in range(n):
            if j + index < len(itr):
                l.append(itr[j + index])
        if len(l) == n:
            lst.append(l)
        index += m
    return lst


for i in windows('abcdefghijk', 4, 2):
    print(i, end='')

print()

for i in windows('abcdefghijk', 3, 2):
    print(i, end='')

Add a comment
Know the answer?
Add Answer to:
IN PYTHON 3 LANGUAGE The windows generator takes an iterable and two ints (call them n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42,...

    Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42, 27, -1, 2.0, 'hello', [2, 4], 23] print("L1 =",L1) odds =[] evens=[] list=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',',','.'] no=0 for i in L1: i=str(i) for j in list: if i.find(j)>=0: no=1 if no==1: None else: i=int(i) if i%2==0: evens.append(i) else: odds.append(i) no=0      ...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : You are given a non-decreasing sequence of n positive integers a1,a2,…,an. Print the number of distinct values in the sequence. For example, if the sequence is 1,2,2,2,3,4,4,5,7,10, the answer is 6 since distinct values are 1,2,3,4,5,7,10. Input The first line contains a positive integer n (1≤n≤1000) — the length of the sequence. The second line contains n space-separated positive integers a1,a2,…,an (1≤ai≤1000)...

  • Create a graph in python for a given input series without using any libraries

    I was asked this question:Given any input series a corresponding graph must be generated without the use of any libraries.After, trying my best, I arrived at this solution to which they replied it had a logical issue.# Create the matrix print("Enter the sequence with spaces: ") arr = list(map(int, input().split())) count = len(arr) rows = int(sum(arr))  cols = int(sum(arr) + 4) content = [[" "]*cols for _ in range(rows)] maxq = 0 maxp = 0 content[0][0] = "/" # Apply the positions in the matrix p = 0 q = arr[0] k = 0 for l in range(q):     if (k != q):         content[k][p] = "/"         p = p + 1         k = k + 1 p = q flag = 1 i = 0 j = 0 k = 0 c = 0 temp = 0 r = 0 flag = 1 for i,j in enumerate(arr):     c = c + 1     if c < count:         k = arr[i+1]     else:         k = 0     if arr[i]:         if flag == 1:             content[q][p] = "/\\"             if maxq < q:                 maxq = q                 maxp = p             qori = q             pori = p             p = p + k             temp = q - k...

  • Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will...

    Create two Java classes called Recursive, RecursiveDemo. Write four methods a- int sum_sqr_rec(stack<int> stk) which will receive a stack of "int" and output the sum of the squares of the elements in the stack. b- int plus_minus_rec(stack<int> stk) which will receive a stack of "int" (example: {a,b,c,d,e,f,g,h,i,j}) and output the sum of the elements in the stack as follows: a - b + c - d + e - f + g - h + i -j c- void prt_chars_rev_rec(stack<char>...

  • A stock is currently selling for $68.96. A call option expiring in 194 days with a strike price o...

    A stock is currently selling for $68.96. A call option expiring in 194 days with a strike price of $65.00 is selling for $8.12; A put option expiring in 133 days with a strike price of $70.00 is selling for $4.57. What is the call option's exercise value? a. $0.00 b. $0.20 c. $0.43 d. $0.61 e. $0.69 f. $1.04 g. $2.22 h. $2.49 i. $3.12 j. $3.53 k. $3.55 l. 3.96 m. $4.16 n. $4.57 o. $5.00 p. $6.74...

  • Ints) For the following mechanism: a. Determine the number of links in the mechanism b. Determine...

    ints) For the following mechanism: a. Determine the number of links in the mechanism b. Determine the total joint order in the mechanism c. Determine the number of loops required for this mechanism d. Determine the mobility of this mechanism e. Draw an appropriate vector loop for this mechanism f. Write the vector loop equation(s) in vector form. g. Write the scalar components of the vector loop position equations. h. Determine any geometric constraint equations. i. Determine the scalar known(s)....

  • 1) Suppose that a directed graph contains the following edges. Find the strongly connected components. {(h,...

    1) Suppose that a directed graph contains the following edges. Find the strongly connected components. {(h, i), (i, j), (j, k), (k, h), (l, m), (m, n), (n, p), (p, l), (f, i), (c, e), (j, b), (k, l), (a, b), (b, c), (c, a), (d, e), (e, f), (f, g), (g, d)}. a) How many vertices are there in the component having the smallest number of vertices? b) How many vertices are there in the component having the second...

  • using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100....

    using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100. If n is out of range then print out an error message and ask for another input. This process repeats until a valid value for n is obtained. b) Inputs two 1D arrays of doubles A and B (of size n) from the keyboard. c) Inputs an integer k (from 1 to 3) from the keyboard. d) If k = 1 then it calculates...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • write the solution of the program by python 3 language : I need the program using...

    write the solution of the program by python 3 language : I need the program using list : Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT