- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to count average of all special values for all permutations of a list of items in Python
Suppose we have a list of elements we can calculate the value of S by the following algorithm.
while size of L > 1 is non-zero, do a := L b := L remove L L := a + b + a*b return L mod (10^9 + 7)
Here we shall have to find the average of all S values that are calculated from all possible combinations of L.
So, if the input is like L = [5,3,4], then the output will be 199, because for all permutation of L, the value of S is 119, so their average is also 119.
To solve this, we will follow these steps −
- m := 10^9+7
- li := a list of x+1 for all x in L
- prod := 1
- for each i in li, do
- prod := prod * i
- prod := prod mod m
- return (prod-1) mod m
Let us see the following implementation to get better understanding −
def solve(L): m = 10**9+7 li = [x+1 for x in L] prod = 1 for i in li: prod *= i prod %= m return (prod-1) % m L = [5,3,4] print(solve(L))
- How to generate all permutations of a list in Python?
- Python Program to print all permutations of a given string
- Python program to get all permutations of size r of a string
- C Program to print all permutations of a given string
- All possible permutations of N lists in Python
- Print all permutations of a given string
- All permutations of a string using iteration?
- Count occurrence of all elements of list in a tuple in Python
- Python program to print all sublists of a list.
- Python - Generate all possible permutations of words in a Sentence
- Print all permutations of a string in Java
- MongoDB Aggregate sum of values in a list of dictionaries for all documents?
- Python program to find the sum of all items in a dictionary
- Python - Create nested list containing values as the count of list items
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion