Summation Code


Recommended Posts

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
Link to 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

Link to 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 = 0
for i in sumation_list:
answer += sum(i)

print answer

Link to 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
Link to 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
Link to 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?

Link to 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
Link to 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
Link to 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
Link to post
Share on other sites

Join the conversation

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

Guest
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...