# How to generate the combinations of an array and divide the numbers in each combination by the product of the numbers in the same combination?

Given an array of N size, I want to return all possible permutations of the array. For each permutation of the array I want to return the value of each number in that combination divided by the sum of the other numbers in the combination including the dividend.

Example: Give the array 123, you find all the possible combinations which are (1,2) (1,3) (2,3) (1,2,3). You then take each combination and divide it as such: (1,2) = (1/1+2) = .33333. (2,1) = (2/1+2) = .66666.

import math import itertools from itertools import combinations import numpy as np n = int(input("Enter The Amount of Numbers in the List: ") ) z = [] q = [] for i in range(0,n): x = float(input("Enter the numbers: ")) q.append(x) h = n+1 permutation_list = [] for i in range(1, len(q)+1): permutations = combinations(q, i) for perm in permutations: perm_prod = 1 for j in range(len(perm)): perm_prod *= perm[j] for j in range(len(perm)-1): for k in range(1,len(perm)): prod_without_this = perm[j]+perm[k] this_num_div_other_nums = perm[j] / prod_without_this second_num = perm[k] / prod_without_this permutation_list.append(this_num_div_other_nums) permutation_list.append(second_num) permutation_list[h:] print(permutation_list)

This is what I have so far but I am having trouble dividing with combinations with length greater than 2. Any help would be appreciated.

## Answer

You could do something like this, where you loop over each combination for each element in the combo.

from itertools import combinations, chain l = [1,2,3] c = list(chain.from_iterable([list(combinations(l,i)) for i in range(2,len(l)+1)])) results = [] for x in c: for n in range(len(x)): results.append(x[n]/sum(x))

Results

[0.3333333333333333, 0.6666666666666666, 0.25, 0.75, 0.4, 0.6, 0.16666666666666666, 0.3333333333333333, 0.5]