combinatorics

Combining two regular expressions A and B into C = (A and not B)

强颜欢笑 提交于 2019-12-24 04:24:09
问题 Let's say I have one regular expression A and another regular expression B as input. I want to create a new regular expression C which matches a line if and only if A matches the line and B does not match the line. I am able to manually create C for very simple cases of A and B : Let's say A is x and B is y , then C = ^[^y]*x[^y]*$ would be a valid solution. Obviously, the problem gets harder as A and B get more complex. Is there a generic algorithm for creating such a regular expression C

How to generate a set of all tuples of given length and sum of elements?

落花浮王杯 提交于 2019-12-24 03:44:10
问题 I would like to have a function that generates a set (or a list) of all possible tuples with a given length and sum of their elements. The elements of tuples should be not negative integer. For example for the following input get_tuple(length=3, total=2) I would like to get the following output: [(1, 0, 1), (2, 0, 0), (1, 1, 0), (0, 0, 2), (0, 1, 1), (0, 2, 0)] Is the a standard library in Python that can do that? If not, how to write a function that can do it? 回答1: You can create a recursive

Permutations of 3 elements within 6 positions

夙愿已清 提交于 2019-12-23 14:54:14
问题 I'm looking to permute (or combine) c("a","b","c") within six positions under the condition to have always sequences with alternate elements, e.g abcbab . Permutations could easily get with: abc<-c("a","b","c") permutations(n=3,r=6,v=abc,repeats.allowed=T) I think is not possible to do that with gtools, and I've been trying to design a function for that -even though I think it may already exist. 回答1: Since you're looking for permutations, expand.grid can work as well as permutations . But

Permutations of 3 elements within 6 positions

馋奶兔 提交于 2019-12-23 14:54:01
问题 I'm looking to permute (or combine) c("a","b","c") within six positions under the condition to have always sequences with alternate elements, e.g abcbab . Permutations could easily get with: abc<-c("a","b","c") permutations(n=3,r=6,v=abc,repeats.allowed=T) I think is not possible to do that with gtools, and I've been trying to design a function for that -even though I think it may already exist. 回答1: Since you're looking for permutations, expand.grid can work as well as permutations . But

All possible combinations of numbers of length 5 in a 4X4 matrix

爷,独闯天下 提交于 2019-12-23 05:14:15
问题 i have a matrix 1 9 2 3 5 0 0 6 8 4 4 8 2 3 7 8 I need to find all possible combinations of numbers of length 5. constraints: Starting form a any position in the matrix you can only move to your next immediate neighbor i.e if u start form say (0,0) your neighbor must be (0,1),(1,1),(1,0) and if you pick a position then form that position you can only move to its immediate neighbor and so on. The length of the number must be 5 digit's i.e for example if i start from (0,0) with value 1 i can

Number of all possible groupings of a set of values?

左心房为你撑大大i 提交于 2019-12-22 15:50:13
问题 I want to find a combinatorial formula that given a certain number of integers, I can find the number of all possible groupings of these integers (such that all values belong to a single group) Say I have 3 integers, 1, 2, 3 There would be 5 groupings: 1 2 3 1|2|3| 1 2|3 1|2 3 2|1 3 I have calculated these computationally for N = 3 to 11, but I am trying to theoretically assertain. These values are: (I believe they are correct) num_integers num_groupings 3 5 4 15 5 52 6 203 7 877 8 4140 9

How to do Combinatorics on the Fly

落花浮王杯 提交于 2019-12-22 09:41:57
问题 I have a very weird problem which has some constrains that make it difficult to solve. I have a list of lists and I want to do the combinations of all items in those lists. Each item has a name and a value. Here is an example: Main List: List 01: Item 01: name:name01, value:value01 Item 02: name:name02, value:value02 List 02: Item 01: name:name03, value:value03 List 03: Item 01: name:name04, value:value04 Item 02: name:name05, value:value05 The end result should look like this: Some List:

Generating a list of repetitions regardless of the order

橙三吉。 提交于 2019-12-22 05:52:45
问题 I want to generate combinations that associate indices in a list with "slots". For instance, (0, 0, 1) means that 0 and 1 belong to the same slot while 2 belongs to an other. (0, 1, 1, 1) means that 1, 2, 3 belong to the same slot while 0 is by itself. In this example, 0 and 1 are just ways of identifying these slots but do not carry information for my usage. Consequently, (0, 0, 0) is absolutely identical to (1, 1, 1) for my purposes, and (0, 0, 1) is equivalent to (1, 1, 0) . The classical

all permutations of +-r, +-s

大憨熊 提交于 2019-12-22 04:21:50
问题 Given two numbers r and s , I would like to get a list of all permutations of n +-r and m +-s . For example (with r=3.14 and s=2.71 ), n = 1 m = 1 out = [ (+r, +s), (+r, -s), (-r, +s), (-r, -s), (+s, +r), (+s, -r), (-s, +r), (-s, -r) ] n = 1 m = 2 out = [ (+r, +s, +s), (+r, -s, +s), (-r, +s, +s), (-r, -s, +s), ... (+s, +r, +s), (-s, +r, +s), (+s, -r, +s), (-s, -r, +s), ... ... ] With itertools.product([+r, -r], repeat=n) I can get the list of the r s and s s separately, and I'd only need to

Calculate multinomial coefficient

廉价感情. 提交于 2019-12-21 20:25:47
问题 I want to calculate multinomial coefficient mod 1e9 + 7. It equals: n! / (k0! * k1! * k2 * ... * km!) In my case m = 3, k0 + k1 + k2 = n, so it would be: n! / (k0! * k1! * k2!) My code for this: .... long long k2 = n - k1 - k0; long long dans = fact[n] % MOD; long long tmp = fact[i] % MOD; tmp = (tmp * fact[j]) % MOD; tmp = (tpm * fact[k]) % MOD; res = (fact[n] / tmp) % MOD; // mb mistake is here... cout << res; fact[i] - factorial of i mod 1e9+7 It does not work on big tests 回答1: I hope I'm