# Summation Code

## Recommended Posts

How to write the code for summation of alist of number

eg ( 0, 1, 2, 3, 4)

it means

(0)+(0 + 1)+(0+1+2)+(0+1+2+3)+(0+1+2+3+4)

tks

#### Share this post

##### Share on other sites

That sounds like a homework question, and you didn't even specify a language.

I'll give you a hint, the obvious solution is to use two loops, one nested in the other. But if you know the length of the list before starting, you can do it with just one loop.

Heh, on second thought you don't even need to know the length of the list.

Edited by Hai-Etlik

#### Share this post

##### Share on other sites
That sounds like a homework question, and you didn't even specify a language.

I'll give you a hint, the obvious solution is to use two loops, one nested in the other. But if you know the length of the list before starting, you can do it with just one loop.

Heh, on second thought you don't even need to know the length of the list.

the language is python

#### Share this post

##### Share on other sites

Hint:

`sum(list)`

Edited by jcl

#### Share this post

##### Share on other sites

this puzzle kind of sucked me in. My code took me almost 40 minutes to write, so I am going to post it. It seems way overcomplicated. I am guessing their is an easier , cleaner way to do this

`mylist = (1,2,3,4)number_elements = len(mylist)sumation_list = []for i in range(number_elements):	sumation_list.append(mylist[0:i+1])answer = 0for i in sumation_list:	answer += sum(i)print answer`

#### Share this post

##### Share on other sites
`def ssum(list):	def scan():		acc = 0		for elt in list:			acc += elt			yield acc	return sum(scan())`

Edited by jcl

#### Share this post

##### Share on other sites

why am I getting a syntax error with help

`>>> help(yield)  File "<stdin>", line 1	help(yield)`

edit added later//

I seem to have to put it in quotes. I can use do this command without quotes and it works

`help(sum)`

Edited by shanenin

#### Share this post

##### Share on other sites

Try help() to enter the help system and then 'yield' at the prompt.

Edit: Or put in quotes, I guess

Edited by jcl

#### Share this post

##### Share on other sites

this is a cleaned up version

`def summ(list):		sum_list = []	for i in range(len(list)):		sum_list.append(list[0:i+1])	answer = 0	for i in sum_list:		answer += sum(i)	return answer`

the only thing I do not like about it is you need to give the list of numbers as a tuple. I can't see an easy way to just give it a list of numbers not in tuple form

this works

`summ((1,2,3,4))`

I would like to do it like this

`summ(1,2,3,4)`

Edited by shanenin

#### Share this post

##### Share on other sites

`def summ(*args):`

args is a tuple containing all of the arguments.

#### Share this post

##### Share on other sites

so changing this line

`def summ(list):`

to

`def summ(*list):`

that sure was a mininmal amount of code to fix the problem :-)

I am not sure I fully understand. if you add an "*" it takes multiple arguments and changes it into a single tuple. Does this principle have a name?

#### Share this post

##### Share on other sites
I am not sure I fully understand. if you add an "*" it takes multiple arguments and changes it into a single tuple. Does this principle have a name?

Functions with variable-length argument lists are sometimes called variadic functions but I don't know if that term has been adopted by any language. The Python docs don't seem refer to the feature by name. Common Lisp uses the term 'rest parameter' to refer to the parameter that takes the 'rest' of the arguments. Bit nicer than Python's "[identifier] initialized to a tuple receiving any excess positional parameters".

Python also allows you to use to double-asterisk parameters to collect keyword arguments into a dictionary.

Edited by jcl

#### Share this post

##### Share on other sites

thanks for the explanation :-)

#### Share this post

##### Share on other sites

why does it tell me the function only takes 0 arguments?

`>>> def test(*inputt):...   return inputt...>>> test('d')('d',)>>> def test(**inputt):...   return inputt...>>> test('d','g')Traceback (most recent call last):  File "<stdin>", line 1, in ?TypeError: test() takes exactly 0 arguments (2 given)`

I guess I was expecting output like this

`{'d': 'g'}`

Edited by shanenin

#### Share this post

##### Share on other sites

Crummy error message. test() takes zero positional arguments and zero or more keyword arguments.

`>>> def test(**foo):...	 return foo...>>> test(one=1){'one': 1}>>> test(one=1, two=2){'two': 2, 'one': 1}`

Edited by jcl

Thanks.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×