combinatorics

Variable amount of nested for loops

雨燕双飞 提交于 2019-11-27 17:37:52
Edit: I'm sorry, but I forgot to mention that I'll need the values of the counter variables. So making one loop isn't a solution I'm afraid. I'm not sure if this is possible at all, but I would like to do the following. To a function, an array of numbers is passed. Each number is the upper limit of a for loop, for example, if the array is [2, 3, 5] , the following code should be executed: for(var a = 0; a < 2; a++) { for(var b = 0; b < 3; b++) { for(var c = 0; c < 5; c++) { doSomething([a, b, c]); } } } So the amount of nested for loops is equal to the length of the array. Would there be any

What does this list permutations implementation in Haskell exactly do?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 17:13:15
I am studying the code in the Data.List module and can't exactly wrap my head around this implementation of permutations: permutations :: [a] -> [[a]] permutations xs0 = xs0 : perms xs0 [] where perms [] _ = [] perms (t:ts) is = foldr interleave (perms ts (t:is)) (permutations is) where interleave xs r = let (_,zs) = interleave' id xs r in zs interleave' _ [] r = (ts, r) interleave' f (y:ys) r = let (us,zs) = interleave' (f . (y:)) ys r in (y:us, f (t:y:us) : zs) Can somebody explain in detail how these nested functions connect/work with each other? Twan van Laarhoven Sorry about the late

How to generate all variations with repetitions of a string?

无人久伴 提交于 2019-11-27 16:47:48
问题 I want to generate all variations with repetitions of a string in C++ and I'd highly prefer a non-recursive algorithm. I've come up with a recursive algorithm in the past but due to the complexity (r^n) I'd like to see an iterative approach. I'm quite surprised that I wasn't able to find a solution to this problem anywhere on the web or on StackOverflow. I've come up with a Python script that does what I want as well: import itertools variations = itertools.product('ab', repeat=4) for

How to calculate the lexicographical rank of a given permutation

走远了吗. 提交于 2019-11-27 16:34:22
问题 For example there are 6 chairs in the room and there are 4 girls and 2 boys. There is 15 unique possible ways they can sit on this chairs 6!/(4!*2!)=15 . My problem is to find efficient way to calculate position of possibility they choose to sit. By position I mean following: BBGGGG - possible position #1 BGBGGG - possible position #2 BGGBGG - possible position #3 BGGGBG - possible position #4 BGGGGB - possible position #5 GBBGGG - possible position #6 GBGBGG - possible position #7 GBGGBG -

Getting the subsets of a set in Python

不羁岁月 提交于 2019-11-27 15:38:57
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) + ' is not a set.' if not s: return [set()] element = s.pop() rest = subsets(s) s.add(element) It has to

How to (cheaply) calculate all possible length-r combinations of n possible elements

…衆ロ難τιáo~ 提交于 2019-11-27 14:55:48
What is the fastest way to calculate all possible length-r combinations of n possible elements without resorting to brute force techniques or anything that requires STL? While working on an Apriori algorithm for my final project in my data structures class, I developed an interesting solution that uses bit-shifting and recursion, which i will share in an answer below for anyone who is interested. However, is this the fastest way of achieving this (without using any common libraries)? I ask more out of curiosity than anything else, as the algorithm i currently have works just fine for my

Code for Variations with repetition (combinatorics)?

允我心安 提交于 2019-11-27 14:16:47
问题 Does anyone have Java code for generating all VARIATIONS WITH REPETITION? There are plenty of permutation and combination examples available, and variations must be the easiest one... It feels stupid to waste time to reinvent the wheel (it must be plenty of code written for this). An example of VARIATIONS WITH REPETITION could be like this: (tupletSize=3, input= A, B) AAA, AAB, ABA, BAA, ABB, BAB, BBA, BBB Thanks! 回答1: This works as is, and it's the easiest for you to study. public class Main

Compute rank of a combination?

最后都变了- 提交于 2019-11-27 12:53:25
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, 64, 33] >>> a[magic((0,1,2))] 78 What would magic be? Initially I thought to just store it as a 3-d

Creating combinations that have no more one intersecting element

徘徊边缘 提交于 2019-11-27 12:27:45
I am looking to create a special type of combination in which no two sets have more than one intersecting element. Let me explain with an example: Let us say we have 9 letter set that contains A, B, C, D, E, F, G, H, and I If you create the standard non-repeating combinations of three letters you will have 9C3 sets. These will contain sets like ABC, ABD, BCD, etc. I am looking to create sets that have at the most only 1 common letter. So in this example, we will get following sets: ABC, ADG, AEI, AFH, BEH, BFG, BDI, CFI, CDH, CEG, DEF, and GHI - note that if you take any two sets there are no

Generate all combinations in SQL

荒凉一梦 提交于 2019-11-27 11:50:45
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) ); DECLARE @com table ( [index] int ); INSERT @set VALUES ('1'),('2'),('3'),('4'),('5'),('6'); SELECT