Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Implement a function that accepts a list of n + 6 numbers and returns a list of n numbers.

0 votes
asked Jan 25, 2020 by dare
Implement a function that accepts a list of n + 6 numbers and returns a list of n numbers. Each item in the new list is calculated by the formula v [i] + v [i + 1] + v [i + 2] + v [i + 3] + v [i + 4] + v [i + 5], where is in the input list.

1 Answer

0 votes
answered Feb 11, 2022 by Kokoz (340 points)
I'm not sure if I understand the task correctly, but here is my solution in Python:

def function(lst):
    new_lst = []
    for i in range(len(lst)-6):
        new_lst.append(sum(lst[i:i+6]))
        
    return new_lst

Or you can use map and it looks like this:

def function2(lst):
    new_lst = list(map(sum, [lst[i:i+6] for i in range(len(lst)-6)]))
        
    return new_lst

Hope this helps
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...