Question

Hi May I ask how can I do this: provided a list [1,2,3,4] and return a...

Hi

May I ask how can I do this:

provided a list [1,2,3,4] and

return a list as each number added the number before it,

for example, the final list should return as [(1+0),(1+2),(1+2+3),(1+2+3+4)] = [1,3,6,10]

please do in python 3.7, thank you!!!

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

Given below is the code in python:

listUsed = [1,2,3,4] # Our initial list
listCopy = [1,2,3,4] # a list copy because the changes made in the orginal will reflect in the next iteration

listLen = len(listUsed) # find length of the list
sumList = 0

for i in range(listLen): # Run loop from 0 to listLen-1
for j in range(i+1): # Run loop from 0 to i+1
sumList = listUsed[j] + sumList # find sum of all the previous numbers and current number
  
listCopy[i] = sumList # Assign this sum to the list copy made
sumList = 0 # reinitialise sum as 0 for the next iteration

print(listCopy) # print listCopy

Add a comment
Know the answer?
Add Answer to:
Hi May I ask how can I do this: provided a list [1,2,3,4] and return a...
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
  • Python Assignment: Print a list of four animals. Number the list without using the numbers 1,2,3,4....

    Python Assignment: Print a list of four animals. Number the list without using the numbers 1,2,3,4. Use only the number 9 in your program to print 1,2,3,4. Example Output: Four cool animals: 1 donkey 2 monkey 3 lion 4 turkey

  • HI I would like to ask how to write the function when my list is [[1,2,4],[0,3,2]]....

    HI I would like to ask how to write the function when my list is [[1,2,4],[0,3,2]]. Like merging them. I managed to do it separately only. The mean for [1,2,4] is 2.33 and so xbar return me [-1.33,-0.33,1.67] (is like [1,2,4] minus the 2.33. I need help for more lists. Thanks >>> def mean(Ist): return sum(Ist)/len(Ist) >>> def xbar(data): i = 0 a = mean(data) for i in range(len(data)): data[i] -= a i += 1 return data >>> xbar([1,2,4]) [-1.3333333333333335,...

  • How to do this code? HW13.11. Create an HTML list from a Python list The function...

    How to do this code? HW13.11. Create an HTML list from a Python list The function below takes one parameter: a list, that contains only strings. Complete the function to create a unordered HTML list, as a string, and returns it. An empty list should return an empty HTML list with no list items. Do not add any extraneous whitespace to the HTML. For example, if given the list ["one", "two"], the function should return "<ul><li>one</li><li>two</li> </ul>". student.py IT TI...

  • May I please know how to program this question in python step by step? Thank you...

    May I please know how to program this question in python step by step? Thank you You are to write a function called arrange. Your function will reorder a given list, where even indexed elements are arranged by descending order (based on index order) from 0 to n/2 of the list and odd indexed elements arranged in ascending order from (n/2) + 1 to n 1. Your function does not need to return the list but to change the existing...

  • hi there, may i ask how to prove the generalized version of the basic counting principle...

    hi there, may i ask how to prove the generalized version of the basic counting principle by mathematical induction method?

  • I want to replace true with the number of comparisons that were made. For example, if...

    I want to replace true with the number of comparisons that were made. For example, if I have a list of [1,4,5,6,7,8] and I ask the program to find 6. is it possible to tell me how many comparisons it took to find 6?  PYTHON here is my code: def linear_search(vals,target):     for val in vals:         if val == target:             return True     return -1 def binary_search(vals,target):     lo = 0     hi = len(vals)-1     mid(lo+hi)//2     while lo <= hi and vals [mid]!=target:         if target...

  • hi, please help in c++. I dont understand how to do this, and a lot of...

    hi, please help in c++. I dont understand how to do this, and a lot of the ways on the internet is confusing me, i am a beginner. with all steps and explantions, will rate! Write a program that determines the frequency of each char in an input file. Your program should . read in an unknown number of chars from an input file named input.txt, • using a function written by you, determine the frequency of occurrence of each...

  • Python HELP How can a get the diagonals of an n x n list without using...

    Python HELP How can a get the diagonals of an n x n list without using numpy. For example if have a list: L = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Then I can get the diagonals: 1, 6, 11, 16 2, 7 , 12 3, 8 5 , 10 ,15 9, 14 2, 5 3, 6, 9 4, 7, 10, 13 8, 11, 14 12,...

  • Python HELP How can a get the diagonals of an n x n list without using...

    Python HELP How can a get the diagonals of an n x n list without using numpy. For example if have a list: L = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Then I can get the diagonals: 1, 6, 11, 16 2, 7 , 12 3, 8 5 , 10 ,15 9, 14 2, 5 3, 6, 9 4, 7, 10, 13 8, 11, 14 12,...

  • hi i need help with this question but in details please how do i get each...

    hi i need help with this question but in details please how do i get each number??? i posted it before but the answers were not clear and when i tried to calculate it myself it resulted different note: without using excel 3. (30 points) - Rate of returns for A and B companies in different states of nature are provided in the following table: State of nature Probability Rate of return on A's share (% Rate of return on...

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