combinatorics

Generating the Shortest Regex Dynamically from a source List of Strings

自古美人都是妖i 提交于 2019-12-18 21:13:51
问题 I have a bunch of SKUs (stock keeping units) that represent a series of strings that I'd like to create a single Regex to match for. So, for example, if I have SKUs: var skus = new[] { "BATPAG003", "BATTWLP03", "BATTWLP04", "BATTWSP04", "SPIFATB01" }; ...I'd like to automatically generate the Regex to recognize any one of the SKUs. I know that I could do simply do "BATPAG003|BATTWLP03|BATTWLP04|BATTWSP04|SPIFATB01" , but list of SKUs can be quite lengthy and I'd like to compress the resulting

Generating the Shortest Regex Dynamically from a source List of Strings

随声附和 提交于 2019-12-18 21:13:27
问题 I have a bunch of SKUs (stock keeping units) that represent a series of strings that I'd like to create a single Regex to match for. So, for example, if I have SKUs: var skus = new[] { "BATPAG003", "BATTWLP03", "BATTWLP04", "BATTWSP04", "SPIFATB01" }; ...I'd like to automatically generate the Regex to recognize any one of the SKUs. I know that I could do simply do "BATPAG003|BATTWLP03|BATTWLP04|BATTWSP04|SPIFATB01" , but list of SKUs can be quite lengthy and I'd like to compress the resulting

python: Generating integer partitions

好久不见. 提交于 2019-12-18 12:29:45
问题 I need to generate all the partitions of a given integer. I found this algorithm by Jerome Kelleher for which it is stated to be the most efficient one: def accelAsc(n): a = [0 for i in range(n + 1)] k = 1 a[0] = 0 y = n - 1 while k != 0: x = a[k - 1] + 1 k -= 1 while 2*x <= y: a[k] = x y -= x k += 1 l = k + 1 while x <= y: a[k] = x a[l] = y yield a[:k + 2] x += 1 y -= 1 a[k] = x + y y = x + y - 1 yield a[:k + 1] reference: http://homepages.ed.ac.uk/jkellehe/partitions.php By the way, it is

Maximize sum of table where each number must come from unique row and column

≯℡__Kan透↙ 提交于 2019-12-18 11:13:20
问题 Suppose we have a table of numbers like this (we can assume it is a square table): 20 2 1 3 4 5 1 14 8 9 15 12 17 17 11 16 1 1 15 18 20 13 15 5 11 Your job is to calculate the maximum sum of n numbers where n is the number of rows or columns in the table. The catch is each number must come from a unique row and column. For example, selecting the numbers at (0,0), (1,1), (2,2), (3,3), and (4,4) is acceptable, but (0,0), (0,1), (2,2), (3,3), and (4,4) is not because the first two numbers were

Maximize sum of table where each number must come from unique row and column

て烟熏妆下的殇ゞ 提交于 2019-12-18 11:13:16
问题 Suppose we have a table of numbers like this (we can assume it is a square table): 20 2 1 3 4 5 1 14 8 9 15 12 17 17 11 16 1 1 15 18 20 13 15 5 11 Your job is to calculate the maximum sum of n numbers where n is the number of rows or columns in the table. The catch is each number must come from a unique row and column. For example, selecting the numbers at (0,0), (1,1), (2,2), (3,3), and (4,4) is acceptable, but (0,0), (0,1), (2,2), (3,3), and (4,4) is not because the first two numbers were

How to get all the unique n-long combinations of a set of duplicatable elements?

白昼怎懂夜的黑 提交于 2019-12-18 08:54:58
问题 I have found many solutions giving a collection elements combined in all possible orders but they all use every element just once in every result while I need them to be treated as reusable. For example if input elements are {"a", "b", "c"} and the number is 2 the output is to be {"a", "a"}, {"a", "b"}, {"a", "c"}, {"b", "a"}, {"b", "b"}, {"b", "c"}, {"c", "a"}, {"c", "b"}, {"a", "c"}. 回答1: Lets say you've got N input elements, and you want a K-long combination. All you need to do is to count

Computing the Factoradic Rank of a Permutation (N choose K)

可紊 提交于 2019-12-18 05:12:50
问题 I've recently learnt about CNS and FNS, and since they look so elegant to me, I decided to try and implement methods to generate combinations and permutations using those techniques. I finished my method to convert from n choose k combinations to a CSN rank and vice-versa but I'm banging my head against the wall trying to do the same with n choose k (unique) permutations. Thanks to @Joshua I got the unranking (FNS to permutation) method working: function Pr_Unrank($n, $k, $rank) { // rank

Array combinatorics in PHP

会有一股神秘感。 提交于 2019-12-18 02:46:07
问题 Consider following array: $a = [['x'], ['y', 'z', 'w'], ['m', 'n']]; How can generate following array from it: $output=[ [[x][y][m]], [[x][z][n]], [[x][w][m]], [[x][y][n]], [[x][z][m]], [[x][w][n]], ]; I am searching for a more efficient code than mine. (My current code is presented as an answer below) 回答1: Here we go. Assuming: $array = [['x'], ['y', 'z', 'w'], ['m', 'n']]; EDIT: After some performance testing, I concluded the solution I posted before is about 300% slower than OP's code,

Combinations and Permutations in F#

懵懂的女人 提交于 2019-12-17 22:35:03
问题 I've recently written the following combinations and permutations functions for an F# project, but I'm quite aware they're far from optimised. /// Rotates a list by one place forward. let rotate lst = List.tail lst @ [List.head lst] /// Gets all rotations of a list. let getRotations lst = let rec getAll lst i = if i = 0 then [] else lst :: (getAll (rotate lst) (i - 1)) getAll lst (List.length lst) /// Gets all permutations (without repetition) of specified length from a list. let rec getPerms

Permutations of parameters (i.e. Cartesian product) into a multi-dimensional array

断了今生、忘了曾经 提交于 2019-12-17 22:00:57
问题 I am interested in calculating a function on a bunch of permutations of parameter values. I want to keep it generic to N dimensions, but let me write it out in 3 dimensions to start with. Generating the permutations is easy enough with meshgrid , but I can't figure out how to reshape the resulting array back to the multidimensions? Here is a starting point: %These are the 3 variations of parameters, with some values. params1 = [100, 200, 300];%Picking these so it is easy to correlate to the