Question

what is an algorithm that generates a new list where after the first element subsequent elements...

what is an algorithm that generates a new list where after the first element subsequent elements are determined from previous elements

python

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

SOURCE CODE IN PYTHON:

#function to create a new list from given list such that

#each element is sum of previous elements upto that position

def listChange(list):

newList=[list[0]] #creating new list with first element of current list

sum=list[0] #to store sum upto current element

for i in range(1,len(list)):

sum+=list[i] #updating sum

newList.append(sum) #appending to new list

return newList #returning new list

#testing the function

print(listChange([1,2,3,4]))

print(listChange([1,-2,3,-4]))

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
what is an algorithm that generates a new list where after the first element subsequent elements...
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
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