davalend Posted February 6, 2006 Report Share Posted February 6, 2006 How to write the code for summation of alist of numbereg ( 0, 1, 2, 3, 4)it means(0)+(0 + 1)+(0+1+2)+(0+1+2+3)+(0+1+2+3+4)tks Quote Link to post Share on other sites
Hai-Etlik Posted February 6, 2006 Report Share Posted February 6, 2006 (edited) 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 February 6, 2006 by Hai-Etlik Quote Link to post Share on other sites
davalend Posted February 7, 2006 Author Report Share Posted February 7, 2006 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 Quote Link to post Share on other sites
jcl Posted February 7, 2006 Report Share Posted February 7, 2006 (edited) Hint:sum(list) Edited February 7, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted February 7, 2006 Report Share Posted February 7, 2006 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 thismylist = (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 Quote Link to post Share on other sites
jcl Posted February 7, 2006 Report Share Posted February 7, 2006 (edited) def ssum(list): def scan(): acc = 0 for elt in list: acc += elt yield acc return sum(scan()) Edited February 7, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted February 7, 2006 Report Share Posted February 7, 2006 (edited) 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 workshelp(sum) Edited February 8, 2006 by shanenin Quote Link to post Share on other sites
jcl Posted February 8, 2006 Report Share Posted February 8, 2006 (edited) Try help() to enter the help system and then 'yield' at the prompt.Edit: Or put in quotes, I guess Edited February 8, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted February 8, 2006 Report Share Posted February 8, 2006 (edited) this is a cleaned up versiondef 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 answerthe 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 formthis workssumm((1,2,3,4))I would like to do it like thissumm(1,2,3,4) Edited February 8, 2006 by shanenin Quote Link to post Share on other sites
jcl Posted February 8, 2006 Report Share Posted February 8, 2006 def summ(*args):args is a tuple containing all of the arguments. Quote Link to post Share on other sites
shanenin Posted February 8, 2006 Report Share Posted February 8, 2006 so changing this linedef summ(list):todef 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? Quote Link to post Share on other sites
jcl Posted February 8, 2006 Report Share Posted February 8, 2006 (edited) 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 February 8, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted February 8, 2006 Report Share Posted February 8, 2006 thanks for the explanation :-) Quote Link to post Share on other sites
shanenin Posted February 9, 2006 Report Share Posted February 9, 2006 (edited) 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 February 9, 2006 by shanenin Quote Link to post Share on other sites
jcl Posted February 9, 2006 Report Share Posted February 9, 2006 (edited) 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 February 9, 2006 by jcl Quote Link to post Share on other sites
shanenin Posted February 9, 2006 Report Share Posted February 9, 2006 Thanks. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.