combinations

Get sorted combinations

£可爱£侵袭症+ 提交于 2019-12-22 07:44:57
问题 I have a input like A = [2,0,1,3,2,2,0,1,1,2,0]. Following I remove all the duplicates by A = list(Set(A)) A is now [0,1,2,3] . Now I want all the pair combinations that I can make with this list, however they do not need to be unique... thus [0,3] equals [3,0] and [2,3] equals [3,2] . In this example it should return [[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]] How do I achieve this? I looked in the iteratools lib. But couldn't come up with a solution. 回答1: >>> A = [2,0,1,3,2,2,0,1,1,2,0] >>> A =

SQL and unique n-column combinations

Deadly 提交于 2019-12-22 05:03:40
问题 Is there a simple way on Oracle to query unique combinations of n fields. I Have very simple two-field solution: CREATE TABLE combinations AS SELECT 1 AS n FROM DUAL UNION ALL SELECT 2 FROM DUAL; Querying for unique combinations: SELECT LEAST(x.a, x.b), GREATEST(x.a,x.b) FROM (SELECT c1.n a, c2.n b FROM combinations c1 CROSS JOIN combinations c2 WHERE c1.n <> c2.n) x GROUP BY LEAST(x.a, x.b), GREATEST(x.a,x.b); From this query 1,2 and 2,1 are considered the same. Unfortunately it doesn't work

Enumeration Combinations of K elements using Java 8

拥有回忆 提交于 2019-12-22 04:48:24
问题 Given an instance of List<E> , using Java 8 features, how is it possible to build a List<List<E>> enumerating all possible combinations of k elements of the original List? 回答1: This is an algorithm that I wrote for solving some Euler Project problems : public static <E> Stream<List<E>> combinations(List<E> l, int size) { if (size == 0) { return Stream.of(Collections.emptyList()); } else { return IntStream.range(0, l.size()).boxed(). <List<E>> flatMap(i -> combinations(l.subList(i+1, l.size())

All combinations of r elements from given array php

偶尔善良 提交于 2019-12-22 01:26:35
问题 Given an array such as the following $array = ('1', '2', '3', '4', '5', '6', '7'); I'm looking for a method to generate all possible combinations, with a minimum number of elements required in each combination r. (eg if r = 5 then it will return all possible combinations containing at least 5 elements) 回答1: A combination can be expressed as n C r = n! / (r! - (n - r)!) First, we determine $n as the number of elements in the array. And $r is the minimum number of elements in each combination.

maximum number combinations

末鹿安然 提交于 2019-12-21 19:45:44
问题 I am trying to generate a list of all possible number combinations within a set of four numbers using all numbers from 0 through 9. I'm getting close but the output doesn't show every possible combination starting from 0000 all the way to 9999. Any clues as to why the following code is dropping certain combinations? def permgen(items, n): if n==0: yield [] else: for i in range(len(items)): for cc in permgen(items[:i]+items[i+1:],n-1): yield [items[i]]+cc if __name__=="__main__": for c in

Calculate possible permutations of an array of numbers

筅森魡賤 提交于 2019-12-21 17:39:27
问题 I have an NSArray with the numbers {0,1,2,3} Calculating the factorial of 4 (the count of the array), I have 24 possible permutations of 0,1,2,3 I would like to know if there is a way to calculate all of these possible permutations and place them in a separate array. For example, given the numbers above, {0,1,2,3}, the resulting permutations would be: 0123, 0132, 0213, 0231, 0312, 0321, 1023, 1032, 1203, 1230, 1302, 1320, 2013, 2031, 2103, 2130, 2301, 2310, 3012, 3021, 3102, 3120, 3201, 3210

Most mutually distant k elements (clustering?)

[亡魂溺海] 提交于 2019-12-21 13:48:12
问题 I have a simple machine learning question: I have n (~110) elements, and a matrix of all the pairwise distances. I would like to choose the 10 elements that are most far apart. That is, I want to Maximize: Choose 10 different elements. Return min distance over (all pairings within the 10). My distance metric is symmetric and respects the triangle inequality. What kind of algorithm can I use? My first instinct is to do the following: Cluster the n elements into 20 clusters. Replace each

Get the most efficient combination of a large List of objects based on a field

匆匆过客 提交于 2019-12-21 11:57:40
问题 I'm looking to maximize the number of stars given a certain budget and max limit on the combination. Example question: With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible. I'm looking to write an efficient algorithm, that could potentially process 1 million Restaurant instances for up to 10 max Restaurants. Note, this is a cross post from a question I asked yesterday: Java: Get the most efficient combination of a large

C++ STL Next Permutation with Combination

一世执手 提交于 2019-12-21 09:30:10
问题 I know that I can use std::next_permutation on some container containing the elements [1, 2, 3] which would generate 6 permutations of this sequence. What I would like to do is given some set [1, 2, 3, 4, 5, 6] generate all possible permutations of size 3. So for this example, [4, 3, 2] would be one of the permutations resulting from this criteria. I'm looking for an STL way of doing this (if possible) rather than writing my own combinations function. Any particular STL implementation I

Stuck with Combination Problem

旧街凉风 提交于 2019-12-21 06:44:02
问题 I'm having a problem w/ my program. I have extracted a set of data and I would like to test if there is a combination for a particular number. For example, I have an array of int, 1 2 3 4 5, I would like to know if there is a combination for 7 maybe, and it must answer yes there is 3 + 4. I figured out that I need to use the combination formula. So I thought that the outer loop may go like 5C1..5C2..5C3..etc, starting to "take 1" then "take 2" at a time to find out all the possible