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 *
> >
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
Shivlal Sharma wrote:
> 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
Rewrite the AD
All the numbers in the nums list don't matter and aren't used. Only the first
number, and how many there are.
https://docs.python.org/3.8/library/functools.html#functools.reduce
Basically it's doing
ADDS(1, 2) which returns 2
that 2 gets fed back into
ADDS(2, 3) which returns 3
that 3 gets fed ba
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