Question

Python 2.7 Write a function cumsum() that takes a list l as argument and returns the...

Python 2.7

Write a function cumsum() that takes a list l as argument and returns the cumulative sum (also known as the prefix sum) of l, which is a list, say cs of the same length as l such that each element cs[i] is equal to the sum of the first i + 1 elements of l, i.e.,

cs[i] == l[0] + l[1] + l[2] + ... + l[i]

You should not modify the argument list l in any way. Also, you do not need to assume that l contains numbers; your function should work with any elements that support +. Practice goal: Although not required, try to solve this without using a nested loop. In this case, you’ll have to write a loop that runs two accumulations in tandem. The first is the standard sum-so-far that we’ve seen several times. The second is an accumulation of the intermediate sum-so-far values into a list (e.g., via an .append() operation); the final value of this list is the desired result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def cumsum(l):
if all(isinstance(item, int) for item in l) or all(isinstance(item, float) for item in l):
     list = []
     for i in range(len(l)):
         sum = 0;
         for j in range(i+1):
             sum = sum + l[j]
         list.append(sum)
else:
     list = []
     for i in range(len(l)):
         str = "";
         for j in range(i+1):
             str = str + l[j]
         list.append(str)
return list

l1 = [1,2,3,5]
l2 = [2.3, 4.5, 6.7,5.6]
l3 = ['a','b','c','d']
print(cumsum(l1))
print(cumsum(l2))
print(cumsum(l3))

Add a comment
Know the answer?
Add Answer to:
Python 2.7 Write a function cumsum() that takes a list l as argument and returns the...
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
  • ​Write a function that takes a list as an argument and returns true if the list...

    ​Write a function that takes a list as an argument and returns true if the list is already sorted in increasing order. (You should not sort the list, and make sure that you just iterate over the list once! Only one loop!) WRITE THE CODE IN PYTHON

  • Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns...

    Problem 1. Write a Python function times_i_at_odd(L) that takes as arguments a list L and returns a list consisting of the elements of L multiplied by the index number of the element at odd positions. (Use list comprehensions) >>> times_i_at_odd([1,2,3,4,5,6,7,8,9,10]) [2, 12, 30, 56, 90] Problem 2. Write a recursive function sum_cols(grid, n) that takes a list of lists of integers grid and integer n and returns the sum of column n in grid. For example, the call sum_cols([[1,2,3,4], [10,20,30,40],...

  • Write a Python function fun3(e) to update a list of numbers e such that the first...

    Write a Python function fun3(e) to update a list of numbers e such that the first and last elements have been exchanged. The function needs to also return multiplication of elements of e. You should assume that the list e has at least 2 elements. Example: >>>lst = [4, 1, 2, -1] >>>fun3(list) -8 >>>lst [-1, 1, 2, 4] *We are an intro to CS class so please do not use anything too advance or fancy. Stick to basics. Right...

  • Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a...

    Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: The number parameter must be a default argument. -      If the default argument for number is given in the function call, only the first number of items are reversed. -      If the default argument for number is not provided in the function call, then the...

  • The problem is this Design a function that accepts a list as an argument and returns...

    The problem is this Design a function that accepts a list as an argument and returns the largest value in the list. The function should use recursion to find the largest item. My coding so far is this def main(): # Prints the largest value in list user_input = input('Enter a list of numbers seperated by a space: ') user_list = user_input.split() print('Largest number is ', max_Number(user_list), '.') # Recursion function that finds the maximum number in a sequence of...

  • Write a function count_vowels(s) that takes a string as an argument and returns the number of...

    Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find...

  • IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns...

    IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns a LIST consisting of all of the digits of n (as integers) in the same order. Name this function intToList(n). For example, intToList(123) should return the list [1,2,3].

  • Write a function maxList that takes in a list of integers as argument and returns their...

    Write a function maxList that takes in a list of integers as argument and returns their highest. You may not use the max() or the sorted() Python built-in functions.

  • Write a function called double_str that takes a string argument and returns a new string that...

    Write a function called double_str that takes a string argument and returns a new string that has all of the same characters as the argument, except each one is repeated twice. For example: double_str('dog') --> 'ddoogg' looking for python coding below is what I have so far double_str = 'robert' double_str 'robert' for i in range(len(double_str)): print(double_str[i])

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

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