Soluție HackerRank pentru Tell the Average, subdomeniul Algebra, în Python 3. Include cerința formatată, exemple, explicația pașilor și cod sursă.
- Problemă: Tell the Average
- Domeniu: Algebra
- Limbaj: Python 3
Challenge: Tell the Average
Subdomeniu: Algebra (algebra)
Scor cont: 25.0 / 25
Submission status: Accepted
Submission score: 1.0
Submission ID: 464724125
Limbaj: python3
Link challenge: https://www.hackerrank.com/challenges/tell-the-average/problem
Cerință
James is very naive in Mathematics, He always makes new things out of a given list of integers. Today he is given a list L, so he creates a value S out of it.
S from a given list can be calculated as follows.
value_of_S(list L)
{
while ((number of elements in L) > 1)
{
a = L[0]
b = L[1]
delete L[1]
L[0] = a+b+ab
}
return L[0] % 1000000007
}
James has an ample amount of time, so he calculates the values of S for all the permutations of the given list L and finds their average value. Then he asks you to report that value.
Input Format
The first line contains an integer N, the number of integers in the list.
The second line contains N integral values, L[0], …, L[N-1], separated by single spaces.
Output Format
Print the [floor](http://en.wikipedia.org/wiki/Floor_and_ceiling_functions) of the average value.
Constraints
2 ≤ N ≤ 10^4
2 ≤ L[i] ≤ 10^6
Sample Input
2
2 3
Sample Output
11
Explanation
The S value corresponding to the two permutations of the given list is 11.
Cod sursă
from functools import reduce
MOD = 1_000_000_007
def _value_of_S_reduce(x:int,y:int)->int:
return (x+y+x*y) % MOD
def value_of_S(l:list)->int:
return reduce(_value_of_S_reduce,l)
if __name__=='__main__':
_ = input()
print(value_of_S(list(map(int,input().split()))))
