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.
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))
Python 2.7 Write a function cumsum() that takes a list l as argument and returns the...
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
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 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...
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 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 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 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 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...
Python recursive function: Given an ordered list L. A permutation of L is a rearrangement of its elements in some order. For example (1,3, 2) and (3, 2, 1) are two different permutations of L=(1,2,3). Implement the following function: def permutations (lst, low, high) The function is given a list 1st of integers, and two indices: low and high (lows high), which indicate the range of indices that need to be considered The function should return a list containing all...
Python Write a function list_copy(l) that takes a list as a parameter and returns a copy of the list using a list comprehension. please provide unittest