combinatorics

Get a list of combinations of lists' elements

隐身守侯 提交于 2019-12-17 20:58:12
问题 Suppose I have 3 lists: ['q','w'], ['a','s'], ['z','x']. How to get a list of possible combinations out of these lists? So I get a list [['q','a','z'],['q','s','z']] and such. I made a method for two, but can't figure one for N lists: static <E> ArrayList combine(ArrayList<E> one,ArrayList<E> two) { ArrayList<ArrayList<E>> combs=new ArrayList<ArrayList<E>>(); for(E e:one) { for(E e2:two) { ArrayList ps=new ArrayList(); ps.add(e); ps.add(e2); combs.add(ps); } } return combs; } I found out that

Generating natural schedule for a sports league

你。 提交于 2019-12-17 19:48:30
问题 I'm looking for an algorithm to generate a schedule for a set of teams. For example, imagine a sports season in which each team plays each other, one time as home team and the other as a visitor team on another teams field. To generate a set of all games in the season is easy, if teams is a list of teams the following would do: set((x, y) for x in teams for y in teams if x != y) But I also want to ORDER the games in chronological order in such a way that it satisfies the constraint of a valid

How to find multiplicative partitions of any integer?

自闭症网瘾萝莉.ら 提交于 2019-12-17 18:43:10
问题 I'm looking for an efficient algorithm for computing the multiplicative partitions for any given integer. For example, the number of such partitions for 12 is 4, which are 12 = 12 x 1 = 4 x 3 = 2 x 2 x 3 = 2 x 6 I've read the wikipedia article for this, but that doesn't really give me an algorithm for generating the partitions (it only talks about the number of such partitions, and to be honest, even that is not very clear to me!). The problem I'm looking at requires me to compute

Getting the subsets of a set in Python

余生长醉 提交于 2019-12-17 13:59:18
问题 Suppose we need to write a function that gives the list of all the subsets of a set. The function and the doctest is given below. And we need to complete the whole definition of the function def subsets(s): """Return a list of the subsets of s. >>> subsets({True, False}) [{False, True}, {False}, {True}, set()] >>> counts = {x for x in range(10)} # A set comprehension >>> subs = subsets(counts) >>> len(subs) 1024 >>> counts in subs True >>> len(counts) 10 """ assert type(s) == set, str(s) + '

Compute rank of a combination?

浪子不回头ぞ 提交于 2019-12-17 10:47:12
问题 I want to pre-compute some values for each combination in a set of combinations. For example, when choosing 3 numbers from 0 to 12, I'll compute some value for each one: >>> for n in choose(range(13), 3): print n, foo(n) (0, 1, 2) 78 (0, 1, 3) 4 (0, 1, 4) 64 (0, 1, 5) 33 (0, 1, 6) 20 (0, 1, 7) 64 (0, 1, 8) 13 (0, 1, 9) 24 (0, 1, 10) 85 (0, 1, 11) 13 etc... I want to store these values in an array so that given the combination, I can compute its and get the value. For example: >>> a = [78, 4,

Generate all combinations in SQL

被刻印的时光 ゝ 提交于 2019-12-17 10:33:47
问题 I need to generate all combinations of size @k in a given set of size @n . Can someone please review the following SQL and determine first if the following logic is returning the expected results, and second if is there a better way? /*CREATE FUNCTION dbo.Factorial ( @x int ) RETURNS int AS BEGIN DECLARE @value int IF @x <= 1 SET @value = 1 ELSE SET @value = @x * dbo.Factorial( @x - 1 ) RETURN @value END GO*/ SET NOCOUNT ON; DECLARE @k int = 5, @n int; DECLARE @set table ( [value] varchar(24)

PHP take all combinations

不羁的心 提交于 2019-12-17 09:51:15
问题 I saw this algorithm that will take numbers or words and find all possible combinations And I'm using it, but it does NOT return all "real" combinations. PHP: <?php require_once 'Math/Combinatorics.php'; $words = array('cat', 'dog', 'fish'); $combinatorics = new Math_Combinatorics; foreach($combinatorics->permutations($words, 2) as $p) { echo join(' ', $p), "\n"; } ?> And it returns: cat dog dog cat cat fish fish cat dog fish fish dog But these are not all real combinations, all real

Calculating the Amount of Combinations

安稳与你 提交于 2019-12-17 09:00:13
问题 Cheers, I know you can get the amount of combinations with the following formula (without repetition and order is not important): // Choose r from n n! / r!(n - r)! However, I don't know how to implement this in C++, since for instance with n = 52 n! = 8,0658175170943878571660636856404e+67 the number gets way too big even for unsigned __int64 (or unsigned long long ). Is there some workaround to implement the formula without any third-party "bigint" -libraries? 回答1: Here's an ancient

Finding all the unique permutations of a string without generating duplicates

一个人想着一个人 提交于 2019-12-17 06:09:10
问题 Finding all the permutations of a string is by a well known Steinhaus–Johnson–Trotter algorithm. But if the string contains the repeated characters such as AABB, then the possible unique combinations will be 4!/(2! * 2!) = 6 One way of achieving this is that we can store it in an array or so and then remove the duplicates. Is there any simpler way to modify the Johnson algorithm, so that we never generate the duplicated permutations. (In the most efficient way) 回答1: Use the following

Shuffle list, ensuring that no item remains in same position

匆匆过客 提交于 2019-12-17 04:32:27
问题 I want to shuffle a list of unique items, but not do an entirely random shuffle. I need to be sure that no element in the shuffled list is at the same position as in the original list. Thus, if the original list is (A, B, C, D, E), this result would be OK: (C, D, B, E, A), but this one would not: (C, E, A, D, B) because "D" is still the fourth item. The list will have at most seven items. Extreme efficiency is not a consideration. I think this modification to Fisher/Yates does the trick, but