combinatorics

all possible numerical expressions

我与影子孤独终老i 提交于 2020-02-29 07:26:47
问题 one totally useless question: I bought a numerical game, it's made of two black dice plus 5 coloured ones. the two black ones form a 2 digits number, ranging from 11 to 66, the other 5 are the numbers you can use, combining them in all possible numerical expressions with the task to obtain the target number. for example, black 40 + 2: target 42 coloured 5 3 6 2 4, you can obtain the target by 5 3 + 6 * 4 2 + - (using RPN because it avoids brackets). now I'd like to use my pocket computer to

Cartesian product over a list of lists in Haskell

自古美人都是妖i 提交于 2020-02-02 03:20:29
问题 Given a list of lists of length x where all the sublists have the same length y , output the y^x lists of length x that contain one item from each sublist. Example ( x = 3 , y = 2 ): [ [1, 2], [3, 4], [5, 6] ] Output ( 2^3 == 8 different outputs): [ [1, 3, 5], [1, 4, 5], [1, 3, 6], [1, 4, 6], [2, 3, 5], [2, 4, 5], [2, 3, 6], [2, 4, 6] ] My research / Work Ruby I wrote actual code to perform this task, but in Ruby, as it is the language I am most comfortable with. def all_combinations(lst) lst

Next permutation in lexicographic order n k

孤街醉人 提交于 2020-01-25 10:11:48
问题 I'm want to write function that returns next permutation in lexicographic order n choose k. So far I found following algorithm class Permutation { public function swap(& $a, $first, $second) { $tmp = $a[$first]; $a[$first] = $a[$second]; $a[$second] = $tmp; } function nextPermutation(& $a, $n, $k) { do { $first = $n - 2; while ($first != -1 && $a[$first] >= $a[$first + 1]) $first--; if ($first == -1) return false; $second = $n - 1; while ($a[$first] >= $a[$second]) $second--; $this->swap($a,

Next permutation in lexicographic order n k

梦想与她 提交于 2020-01-25 10:10:11
问题 I'm want to write function that returns next permutation in lexicographic order n choose k. So far I found following algorithm class Permutation { public function swap(& $a, $first, $second) { $tmp = $a[$first]; $a[$first] = $a[$second]; $a[$second] = $tmp; } function nextPermutation(& $a, $n, $k) { do { $first = $n - 2; while ($first != -1 && $a[$first] >= $a[$first + 1]) $first--; if ($first == -1) return false; $second = $n - 1; while ($a[$first] >= $a[$second]) $second--; $this->swap($a,

How to efficiently generate all combinations (at all depths) whose sum is within a specified range

爱⌒轻易说出口 提交于 2020-01-24 12:03:10
问题 Suppose you have a set of values ( 1 , 1 , 1 , 12 , 12 , 16 ) how would you generate all possible combinations (without repetition) whose sum is within a predefined range [min,max] . For example, here are all the combinations (of all depths) that have a range between 13 and 17 : 1 12 1 1 12 1 1 1 12 16 1 16 This assumes that each item of the same value is indistinguishable, so you don't have three results of 1 12 in the final output. Brute force is possible, but in situations where the number

getting all possible combinations of a list in a form of sublists

不打扰是莪最后的温柔 提交于 2020-01-24 04:08:26
问题 I wonder if someone can help with the following task: What is the way to get all combinations a list can be split into sublists, when order does not matter? Let's say I have a list of 4 items: import itertools as it a = [1, 2, 3, 4] print(list(it.combinations(a, 2))) That will give me a list of 6 possible pairs: [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] How to make (out of it? or any other way) a set of lists that contain original [1, 2, 3, 4] sequence in any order? So for this example

Hungarian algorithm in Python

柔情痞子 提交于 2020-01-21 02:59:05
问题 Is there good implementation of Hungarian algorithm in standard python libraries? 回答1: I just tried: pip install munkres and it worked. Here you can find a short explanation on how to use it. I got an error trying to install "hungarian". 回答2: Check this munkres out 回答3: There are multiple Options: pip install munkres Documentation here pip install hungarian Documentation here pip install scipy scipy.optimize.linear_sum_assignment Documentation here 来源: https://stackoverflow.com/questions

Bit hack to generate all integers with a given number of 1s

心不动则不痛 提交于 2020-01-18 22:52:12
问题 I forgot a bit hack to generate all integers with a given number of 1s. Does anybody remember it (and probably can explain it also)? 回答1: From Bit Twiddling Hacks Update Test program Live On Coliru #include <utility> #include <iostream> #include <bitset> using I = uint8_t; auto dump(I v) { return std::bitset<sizeof(I) * __CHAR_BIT__>(v); } I bit_twiddle_permute(I v) { I t = v | (v - 1); // t gets v's least significant 0 bits set to 1 // Next set to 1 the most significant bit to change, // set

Bit hack to generate all integers with a given number of 1s

一笑奈何 提交于 2020-01-18 22:39:59
问题 I forgot a bit hack to generate all integers with a given number of 1s. Does anybody remember it (and probably can explain it also)? 回答1: From Bit Twiddling Hacks Update Test program Live On Coliru #include <utility> #include <iostream> #include <bitset> using I = uint8_t; auto dump(I v) { return std::bitset<sizeof(I) * __CHAR_BIT__>(v); } I bit_twiddle_permute(I v) { I t = v | (v - 1); // t gets v's least significant 0 bits set to 1 // Next set to 1 the most significant bit to change, // set

Bit hack to generate all integers with a given number of 1s

久未见 提交于 2020-01-18 22:38:47
问题 I forgot a bit hack to generate all integers with a given number of 1s. Does anybody remember it (and probably can explain it also)? 回答1: From Bit Twiddling Hacks Update Test program Live On Coliru #include <utility> #include <iostream> #include <bitset> using I = uint8_t; auto dump(I v) { return std::bitset<sizeof(I) * __CHAR_BIT__>(v); } I bit_twiddle_permute(I v) { I t = v | (v - 1); // t gets v's least significant 0 bits set to 1 // Next set to 1 the most significant bit to change, // set