Sponsored By

Sign in to follow this  
davalend

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

Share this post


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

Share this post


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

Share this post


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


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

Share this post


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


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

Share this post


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?

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this