combinations

Java program to calculate the combinations function

南楼画角 提交于 2021-02-08 09:12:04
问题 For my programming class I have to make a program that calculates the combinations function C(n,k) = n!/(k!*(n-k)!) by using a main method to deal with input/output, a method to compute the factorial, and a method to compute the combinations function. Here's my code: import java.util.Scanner; public class Combinations { public static void factorials (int set, int objects) { Scanner keyboard = new Scanner(System.in); int n = set; int k = objects; int c = (n-k); int factorial1 = 1; int

Python - Get all unique combinations with replacement from lists of list with unequal length

久未见 提交于 2021-02-07 08:52:57
问题 Note : This is not a duplicate question as the title might say If I have a list of list , I need to get all combinations from it with replacement. import itertools l = [[1,2,3] ,[1,2,3], [1,2,3]] n = [] for i in itertools.product(*l): if sorted(i) not in n: n.append(sorted(i)) for i in n: print(i) [1, 1, 1] [1, 1, 2] [1, 1, 3] [1, 2, 2] [1, 2, 3] [1, 3, 3] [2, 2, 2] [2, 2, 3] [2, 3, 3] [3, 3, 3] Thanks to @RoadRunner and @Idlehands. Above code is perfect with 2 problems : For large list,

Generate all unique combinations of Items

扶醉桌前 提交于 2021-02-07 08:21:34
问题 I trying to generate all possible unique combination of items. Ex: item1, item2, item3 Combinations: item1+item2+item3 item1+item2 item1+item3 item2+item3 item1 item2 item3 I am unable to get an idea on how to solve this? for(int i=0;i<size;i++){ for(int j=i+1;j<size;j++){ System.out.println(list.item(i)+list.item(j)); } } The above code certainly works for all unique combination of two elements. But not for 3 element pair and more.. 回答1: If you have N items, count from 1 to 2^N-1. Each

Generate all unique combinations of Items

六月ゝ 毕业季﹏ 提交于 2021-02-07 08:19:48
问题 I trying to generate all possible unique combination of items. Ex: item1, item2, item3 Combinations: item1+item2+item3 item1+item2 item1+item3 item2+item3 item1 item2 item3 I am unable to get an idea on how to solve this? for(int i=0;i<size;i++){ for(int j=i+1;j<size;j++){ System.out.println(list.item(i)+list.item(j)); } } The above code certainly works for all unique combination of two elements. But not for 3 element pair and more.. 回答1: If you have N items, count from 1 to 2^N-1. Each

PHP - Generate all combinations of items in array

*爱你&永不变心* 提交于 2021-02-05 12:24:20
问题 I have a PHP array like this... $myarray = array( 'red', 'yellow', 'green, 'blue' ); Order does not matter to me so I think I am trying to calculate combinations rather than permutations . I am wanting to get this back... $finalarray = array( 'Red', 'Green', 'Red', 'Yellow', 'Red', 'Blue', 'Green', 'Yellow', 'Green', 'Blue', 'Yellow', 'Blue' ); Is there a built in PHP feature for achieving this or is there a way to do it with loops? 回答1: You can of course with permutation libraries, but that

PHP - Generate all combinations of items in array

心已入冬 提交于 2021-02-05 12:24:02
问题 I have a PHP array like this... $myarray = array( 'red', 'yellow', 'green, 'blue' ); Order does not matter to me so I think I am trying to calculate combinations rather than permutations . I am wanting to get this back... $finalarray = array( 'Red', 'Green', 'Red', 'Yellow', 'Red', 'Blue', 'Green', 'Yellow', 'Green', 'Blue', 'Yellow', 'Blue' ); Is there a built in PHP feature for achieving this or is there a way to do it with loops? 回答1: You can of course with permutation libraries, but that

How to create a co-occurrence matrix calculated from combinations by ID/row in R?

♀尐吖头ヾ 提交于 2021-02-05 07:00:14
问题 Update Thanks to @jazzurro for his anwer. It made me realize that the duplicates may just complicate things. I hope by keeping only unique values/row simplifies the task.* df <- data.frame(ID = c(1,2,3,4,5), CTR1 = c("England", "England", "England", "China", "Sweden"), CTR2 = c("England", "China", "China", "England", NA), CTR3 = c("USA", "USA", "USA", "USA", NA), CTR4 = c(NA, NA, NA, NA, NA), CTR5 = c(NA, NA, NA, NA, NA), CTR6 = c(NA, NA, NA, NA, NA)) ID CTR1 CTR2 CTR3 CTR4 CTR5 CTR6 1

Python generate all possible combinations of matrix

百般思念 提交于 2021-02-05 05:37:06
问题 I need to generate all combinations of a matrix in Python. The input will be two integers n and m, and I need to generate all possible states of that matrix with 1 and 0 as possible values. For example: n = 3 m = 2 [[0 0 0] [1 0 0] [1 1 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0],[0 0 0],[0 0 0] . . . . . ] Is there a clean and efficient way to do this given I will not know the values for n and m until runtime? The highest value used will be n = 16 m = 16. 回答1: One way is by generating all binary

Python generate all possible combinations of matrix

◇◆丶佛笑我妖孽 提交于 2021-02-05 05:34:07
问题 I need to generate all combinations of a matrix in Python. The input will be two integers n and m, and I need to generate all possible states of that matrix with 1 and 0 as possible values. For example: n = 3 m = 2 [[0 0 0] [1 0 0] [1 1 0] [0 0 0] [0 0 0] [0 0 0] [0 0 0],[0 0 0],[0 0 0] . . . . . ] Is there a clean and efficient way to do this given I will not know the values for n and m until runtime? The highest value used will be n = 16 m = 16. 回答1: One way is by generating all binary

R: all possible combinations from a vector of elements with 2 possible conditions (+/-)

自作多情 提交于 2021-02-04 17:39:06
问题 I have a vector of elements named markers of the form: markers <- LETTERS[1:5] Each element in markers is of Boolean type with two possible conditions + and - . I would like a fast an efficient way to obtain all possible combinations, so that the two conditions are considered (a marker with cannot be paired with itself even if the condition is different). The result would ideally be just a character vector or a list, where its elements are the marker combinations separated by / . The elements