combinatorics

Replace duplicate values in array with new randomly generated values

陌路散爱 提交于 2019-12-03 20:10:52
问题 I have below a function (from a previous question that went unanswered) that creates an array with n amount of values. The sum of the array is equal to $max. function randomDistinctPartition($n, $max) { $partition= array(); for ($i = 1; $i < $n; $i++) { $maxSingleNumber = $max - $n; $partition[] = $number = rand(1, $maxSingleNumber); $max -= $number; } $partition[] = $max; return $partition; } For example: If I set $n = 4 and $max = 30. Then I should get the following. array(5, 7, 10, 8);

Generating all 5 card poker hands

余生颓废 提交于 2019-12-03 18:24:19
问题 This problem sounds simple at first glance, but turns out to be a lot more complicated than it seems. It's got me stumped for the moment. There are 52c5 = 2,598,960 ways to choose 5 cards from a 52 card deck. However, since suits are interchangeable in poker, many of these are equivalent - the hand 2H 2C 3H 3S 4D is equivalent to 2D 2S 3D 3C 4H - simply swap the suits around. According to wikipedia, there are 134,459 distinct 5 card hands once you account for possible suit recolorings. The

Generate All Unique Combinations of Elements of a IEnumerable(Of T)

 ̄綄美尐妖づ 提交于 2019-12-03 17:04:00
This question is virtually the same as this SO post , only I'm looking for a VB.NET (.NET 4) solution. I've spun my wheels long enough trying to come up with a generic solution to solving this "power set" problem. Given: Dim choices As IEnumerable(Of String) = {"Coffee", "Tea", "Milk", "Cookies"} Dim choiceSets = choices.CombineAll() I'm looking for choiceSets to be an IEnumerable(Of IEnumerable(Of T)) so that I can do something like: For each choiceSet in choiceSets Console.WriteLine(String.Join(", ", choiceSet)) Next And get results that look like: Coffee Tea Milk Cookies Coffee, Tea Coffee,

Removing items from unevenly distributed set

雨燕双飞 提交于 2019-12-03 12:19:45
I have a website where users submit questions (zero, one or multiple per day), vote on them and answer one question per day (more details here ). A user can see the question only once either by submitting, voting or answering it. I have a pool of questions that players have already seen. I need to remove 30 questions from the pool each month. I need to pick questions to remove in such way that I maximize the number of available questions left in the pool for player with least available questions. Example with pool of 5 questions (and need to remove 3): player A has seen questions 1, 3 and 5

Given a permutation's lexicographic number, is it possible to get any item in it in O(1)

不打扰是莪最后的温柔 提交于 2019-12-03 10:57:23
问题 I want to know whether the task explained below is even theoretically possible, and if so how I could do it. You are given a space of N elements (i.e. all numbers between 0 and N-1 .) Let's look at the space of all permutations on that space, and call it S . The i th member of S , which can be marked S[i] , is the permutation with the lexicographic number i . For example, if N is 3, then S is this list of permutations: S[0]: 0, 1, 2 S[1]: 0, 2, 1 S[2]: 1, 0, 2 S[3]: 1, 2, 0 S[4]: 2, 0, 1 S[5]

bin packing with overlapping objects

≯℡__Kan透↙ 提交于 2019-12-03 09:03:32
I have some bins with different capacities and some objects with specified size. The goal is to pack these objects in the bins. Until now it is similar to the bin-packing problem. But the twist is that each object has a partial overlap with another. So while object 1 and 2 has sizes s1 and s2, when I put them in the same bin the filled space is less than s1+s2. Supposing that I know this overlapping value for each pair of objects, is there any approximation algorithm like the ones for original bin-packing for this problem too? Masood_mj The answer is to use a kind of tree that captures the

Dynamic T-SQL approach for combinatorics/knapsack

本秂侑毒 提交于 2019-12-03 07:42:37
I guess my question has to do with a variant of the knapsack problem, but I can't really come up with a solution for this: Let's say you are in a hardware store and need to buy 21 screws. They only offer them in bags: Bag X - 16 Screws - 1.56$ per screw - 25$ Total Bag Y - 8 Screws - 2.25$ per screw - 18$ Total Bag Z - 4 Screws - 1.75$ per screw - 7$ Total Now you have to figure out which Bags you should buy to get your 21 screws (or more!) for the lowest possible price. So what I got is a table with all the bags and a variable to define the required amount. What I need as a result should be a

Secret Santa - Generating 'valid' permutations

眉间皱痕 提交于 2019-12-03 06:38:26
问题 My friends invited me home to play the game of Secret Santa, where we are supposed to draw a lot & play the role of 'Santa' for a friend in the group. So, we write all our names and pick a name randomly. If any of us ends up having their own name picked, then we reshuffle and pick names all over again (the rationale being that one can not be one's own Santa). There are seven of us while playing so I thought of the final 'Santa-allocation' as a permutation of (1:7) onto itself, with some

number to unique permutation mapping of a sequence containing duplicates

时光毁灭记忆、已成空白 提交于 2019-12-03 06:04:59
问题 I am looking for an algorithm that can map a number to a unique permutation of a sequence. I have found out about Lehmer codes and the factorial number system thanks to a similar question, Fast permutation -> number -> permutation mapping algorithms, but that question doesn't deal with the case where there are duplicate elements in the sequence. For example, take the sequence 'AAABBC'. There are 6! = 720 ways that could be arranged, but I believe there are only 6! / (3! * 2! * 1!) = 60 unique

Finding a number of maximally different binary vectors from a set

折月煮酒 提交于 2019-12-03 05:17:47
Consider the set, S , of all binary vectors of length n where each contains exactly m ones; so there are n-m zeros in each vector. My goal is to construct a number, k , of vectors from S such that these vectors are as different as possible from each other. As a simple example, take n =4, m =2 and k =2, then a possible solution is: [1,1,0,0] and [0,0,1,1]. It seems that this is an open problem in the coding theory literature (?). Is there any way (i.e. algorithm) to find a suboptimal yet good solution ? Is Hamming distance the right performance measure to use in this case ? Some thoughts: In