combinatorics

How to generate 5-character strings combinations (1 digit, two equal letters and two different equal letters) without duplication

房东的猫 提交于 2020-01-06 10:21:28
问题 I am trying to generate combinations of a 5-character strings consisting of four letters (exactly two are equal and another repeated two are equal) and one digit. Example for CORRECT combinations: 1aabb b1aab ca3ac Example for INCORRECT combinations: 1aaaa -> incorrect because there are more than 2 equal letters 1aaab -> Same as the previous 1abcd -> No 2 equal letters + 2 equal different letters This is the code I am using: from itertools import combinations, permutations, product LETTERS =

Creating combination of numbers

[亡魂溺海] 提交于 2020-01-06 08:29:09
问题 Task is to find the permutations of numbers {1,2,3...N}. N <=16. The problem is in conditions: each number must be used only one time and permutation should be of N-size . And the each permutation should be array, because it will be tested. As input is given one permutation. The main task is to find all the permutations, that have the same set of difference as the first one has. For example, is given 12 3 6 1 9 11 8 5 2 4 7 10 Difference is -9,3,-5,8,2,-3,-3,-3,2,3,10 So, there are 12

What does a matrix array look like for a Johnson graph?

强颜欢笑 提交于 2020-01-05 05:10:01
问题 I can't seem to find any examples (or much information at all) of what a matrix array looks like for a Johnson graph. Can anyone send me an example of what their arrays look like? 回答1: This directly uses the definition of a Johnson Graph given here Wiki Johnson Graph johnsonmatrix[n_, k_] := Module[{s=Select[Subsets[Range[n]], Length[#]==k&]}, {s, MatrixForm[Table[If[Length[Intersection[s[[i]], s[[j]]]]==k-1, 1, 0], {i, Length[s]}, {j, Length[s]}]]}] and generates the list of subsets which

Given a group of people, where each pair has a value, how can I find the configuration of pairings with the least total value?

我们两清 提交于 2020-01-04 05:15:12
问题 It's very much like the Assignment Problem, except with a complete undirected graph rather than a bipartite graph. The dumbest, most brute force-iest solution is something like this: Get all possible configurations of pairs... groups = people.combination(2).to_a.combination(people.size/2).to_a ...rejecting all of the configurations that contain the same person more than once. groups.reject! { |group| group.flatten.uniq.size < people.size } Then find the configuration with the minimum value.

PHP nested array combinations / permutations

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 02:33:29
问题 I thought my problem had been solved using this solution, using the answer by VolkerK, but it doesn't seem to be working correctly. What I want is a function that returns all possible combinations of values contained in nested arrays. For example, if I pass in [ ['a', 'b'], ['a', 'b'], ['a', 'b'], ['a'], ['a'], ['a'] ] It would return a, a, a, a, a, a b, b, b, a, a, a a, a, b, a, a, a a, b, a, a, a, a b, a, a, a, a, a a, b, b, a, a, a b, b, a, a, a, a b, a, b, a, a, a The problem with using

All N Combinations of All Subsets

瘦欲@ 提交于 2020-01-03 09:05:15
问题 Given a vector of elements, I would like to obtain a list of all possible n -length combinations of subsets of elements. For example, given the (simplest) sequence 1:2 , I would like to obtain a list object of the form { {{1},{1}}, {{1},{2}}, {{2},{2}}, {{1},{1,2}}, {{2},{1,2}}, {{1,2},{1,2}} } when n=2 . I was able to generate a list of all non-empty subsets using the following: listOfAllSubsets <- function (s) { n <- length(s) unlist(lapply(1:n, function (n) { combn(s, n, simplify=FALSE) })

Given a list of numbers, find all matrices such that each column and row sum up to 264

不打扰是莪最后的温柔 提交于 2020-01-03 08:42:17
问题 Let's say I have a list of 16 numbers. With these 16 numbers I can create different 4x4 matrices. I'd like to find all 4x4 matrices where each element in the list is used once, and where the sum of each row and each colum is equal to 264. First I find all combination of elements within the list that sum up to 264 numbers = [11, 16, 18, 19, 61, 66, 68, 69, 81, 86, 88, 89, 91, 96, 98, 99] candidates = [] result = [x for x in itertools.combinations(numbers, 4) if sum(x) == 264] result becomes a

Number of subsets of {1,2,3,…,N} containing at least 3 consecutive elements

六眼飞鱼酱① 提交于 2020-01-02 07:14:52
问题 Suppose we have a set like {1,2,3} then there is only one way to choose 3 consecutive numbers... it's the set {1,2,3}... For a set of {1,2,3,4} we have 3 ways: 123 234 1234 (technically these are unordered sets of numbers, but writing them consecutively helps) f(5) ; {1,2,3,4,5} -> 8 ways: 123 1234 1235 12345 234 2345 345 1345 f(6) ; {1,2,3,4,5,6} -> 20 ways: ... f(7) ; {1,2,3,4,5,6,7} -> 47 ways: ... So for a given N, I can get the answer by applying brute force, and calculating all such

Find all subsets of size N in an array using Ruby

六眼飞鱼酱① 提交于 2020-01-02 04:44:06
问题 Given an array ['a', 'b', 'c', 'd', 'e', 'f'] , how would I get a list of all subsets containing two, three, and four elements? I'm quite new to Ruby (moving from C#) and am not sure what the 'Ruby Way' would be. 回答1: Check out Array#combination Then something like this: 2.upto(4) { |n| array.combination(n) } 回答2: Tweaking basicxman's a little bit: 2.upto(4).flat_map { |n| array.combination(n).to_a } #=> [["a", "b"], ["a", "c"], ["a", "d"], ..., ["c", "d", "e", "f"]] 来源: https://stackoverflow

Every possible combination of X split into N stacks

家住魔仙堡 提交于 2020-01-02 01:59:07
问题 I am sure this problem has a formal name, and knowing that name would probably help me find the solution, but I don't know it, and wording the problem for Google keeps pointing me to the Knapsack Problem, which isn't the same thing. I want to take some value X and find every possible combination of splitting that value into N stacks of whole integers. In case my wording is confusing, here is an example of X = 4, N = 3 Stack -> 1 | 2 | 3 | ---------------------- #1-----> 4 | 0 | 0 | ----------