Didn't understand the output of the following Python 3 code with reduce function?
I have seen this code on one of competative programming site but I didn't get it, Why output is 9? from functools import * def ADDS(a,b): return a+1 nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] add = reduce(ADDS, nums) print(add) output: 9 -- https://mail.python.org/mailman/listinfo/python-list
Re: Didn't understand the output of the following Python 3 code with reduce function?
On Saturday, 29 August 2020 at 02:47:56 UTC+5:30, Ben Bacarisse wrote: Thanks you all, I was really confused in this code. > Shivlal Sharma writes: > > > I have seen this code on one of competative programming site but I > > didn't get it, Why output is 9? > > > > from functools import * > > > > def ADDS(a,b): > > return a+1 > > nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] > > add = reduce(ADDS, nums) > > print(add) > > > > output: 9 > Hint: > > reduce(f, [e1, e2]) is f(e1, e2) > reduce(f, [e1, e2, e3]) is f(f(e1, e2), e3) > reduce(f, [e1, e2, e3, e4]) is f(f(f(e1, e2), e3), e4) > > Replace f with a function that adds one to its first argument. Does > that help? > > -- > Ben. -- https://mail.python.org/mailman/listinfo/python-list
Error in lambda function..!
from functools import* nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] add = reduce(lambda a : a + 1, nums) print(add) error: - TypeError Traceback (most recent call last) in () 1 from functools import* 2 nums = [1, 2, 3, 4, 5, 6, 7, 8, 9] > 3 add = reduce(lambda a : a + 1, nums) 4 print(add) TypeError: () takes 1 positional argument but 2 were given -- https://mail.python.org/mailman/listinfo/python-list
Re: What this error want to say? Can't we use return without function?
On Monday, 7 September 2020 at 12:57:16 UTC+5:30, Python wrote: > Shivlal Sharma wrote: > > N = int(input("enter a positive integer:")) > > coun = 1 > > while (N > 0): > > coun = coun * N > > N = N - 1 > > return coun > > nice = ntime(N) > > print(nice) > > > > > > error: return outside of the function > What did you expect return out of a function to even mean? I mean that can't we use return keyword outside of the function? -- https://mail.python.org/mailman/listinfo/python-list
Re: What this error want to say? Can't we use return without function?
On Monday, 7 September 2020 at 13:08:04 UTC+5:30, Chris Angelico wrote: > On Mon, Sep 7, 2020 at 5:36 PM Shivlal Sharma wrote: > > > > On Monday, 7 September 2020 at 12:57:16 UTC+5:30, Python wrote: > > > Shivlal Sharma wrote: > > > > N = int(input("enter a positive integer:")) > > > > coun = 1 > > > > while (N > 0): > > > > coun = coun * N > > > > N = N - 1 > > > > return coun > > > > nice = ntime(N) > > > > print(nice) > > > > > > > > > > > > error: return outside of the function > > > What did you expect return out of a function to even mean? > > I mean that can't we use return keyword outside of the function? > Well... no. What do you expect it to mean? It's like asking "why can't > I use 'else' without 'if'?" - there'd need to be some meaning for it. > > ChrisA Ohh, got it. you mean we can't you return without a function, right? -- https://mail.python.org/mailman/listinfo/python-list
Re: What this error want to say? Can't we use return without function?
On Monday, 7 September 2020 at 13:10:57 UTC+5:30, Shivlal Sharma wrote: > On Monday, 7 September 2020 at 13:08:04 UTC+5:30, Chris Angelico wrote: > > On Mon, Sep 7, 2020 at 5:36 PM Shivlal Sharma wrote: > > > > > > On Monday, 7 September 2020 at 12:57:16 UTC+5:30, Python wrote: > > > > Shivlal Sharma wrote: > > > > > N = int(input("enter a positive integer:")) > > > > > coun = 1 > > > > > while (N > 0): > > > > > coun = coun * N > > > > > N = N - 1 > > > > > return coun > > > > > nice = ntime(N) > > > > > print(nice) > > > > > > > > > > > > > > > error: return outside of the function > > > > What did you expect return out of a function to even mean? > > > I mean that can't we use return keyword outside of the function? > > Well... no. What do you expect it to mean? It's like asking "why can't > > I use 'else' without 'if'?" - there'd need to be some meaning for it. > > > > ChrisA > Ohh, got it. you mean we can't you return without a function, right? Thank you guys! -- https://mail.python.org/mailman/listinfo/python-list
What this error want to say? Can't we use return without function?
N = int(input("enter a positive integer:")) coun = 1 while (N > 0): coun = coun * N N = N - 1 return coun nice = ntime(N) print(nice) error: return outside of the function -- https://mail.python.org/mailman/listinfo/python-list