List of square roots python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: List of square roots python (/thread-2738.html) |
List of square roots python - py7 - Apr-05-2017 So the question is: I have to do a little program where the function receives a argument x, who is an integer and returns a list of the sum of the first n square roots. Like [sqrt(1), sqrt(1) + sqrt(2), sqrt(1) + sqrt(2) + sqrt(3), ....] I've made this line of code but it is just returning the sum return sum([x**0.5 for x in range(n+1)]) RE: List of square roots python - ichabod801 - Apr-06-2017 To get the cumulative sums of a list of length n: [sum(the_list[:index]) for index in range(1, n+1)]One problem is that your list is n+1 long. That's because you're including the square root of zero. RE: List of square roots python - py7 - Apr-08-2017 (Apr-06-2017, 01:39 AM)ichabod801 Wrote: To get the cumulative sums of a list of length n: RE: List of square roots python - wavic - Apr-08-2017 It returns what you want. Here is the sum of the list without any additional math: >>> sum([x for x in range(11)]) 55Here is the sum of list's elements with x**0.5 applied on each of them >>> sum([x**0.5 for x in range(11)]) 22.4682781862041Notice the result? RE: List of square roots python - ichabod801 - Apr-08-2017 (Apr-08-2017, 08:33 PM)py7 Wrote: And what is 'the_list' The list of square roots that you've already generated. RE: List of square roots python - py7 - Apr-08-2017 (Apr-08-2017, 09:39 PM)ichabod801 Wrote: 1 it only returns a list --> [0,0,0] RE: List of square roots python - ichabod801 - Apr-08-2017 The cumulative sums of the items in the list. That's what you're homework is asking for, just with square roots, not integers.
|