combinatorics

how to get all possible combination of items from 2-dimensional list in python?

百般思念 提交于 2019-12-09 00:01:50
问题 I didn't find a better way to phrase this question in the title. If you can, please edit. I have a list of lists like this: a = [['a','b'],[1,2]] now, I'd like a function that spits out all possible combination like this: [['a',1],['a',2],['b',1],['b',2]] where nor the number of lists in a is known in advance, nor is the length of each of the sub lists known in advance, but all the combinations that come out should contain 1 item from every sublist. 回答1: You need itertools.product(): >>> list

Combinatorial algorithm for assigning people to groups

一个人想着一个人 提交于 2019-12-08 19:39:37
问题 A coworker came to me with an interesting problem, a practical one having to do with a "new people in town" group she's a part of. 18 friends want to have dinner in groups for each of the next 4 days. The rules are as follows: Each day the group will split into 4 groups of 4, and a group of 2. Any given pair of people will only see each other at most once over the course of the 4 days. Any given person will only be part of the size 2 group at most once. A brute force recursive search for a

Number of possible combinations of fork

别来无恙 提交于 2019-12-08 16:45:37
int main(void) { int id = 0; for(int i = 1; i < 4; i++) { if(fork() == 0) { id = i; } else { printf("Process %d created child %d\n", id, i); } } return 0; } In the code above, multiple ordering of the output (printf statements) can be generated based on how the operating system schedules processes for execution. How many different orderings are possible? You may assume that all fork and printf calls succeed. I'm trying to help my students understand how to approach this problem, however I got a nice 0 on this question when I wrote the exam. I was hoping someone could explain how to go about it

Creating tuples of all possible combinations of items from two lists, without duplicating items within tuples

柔情痞子 提交于 2019-12-08 15:18:47
问题 I would like to be able to take a range of numbers and return a list containing triples without duplicates. Each element of x should appear once in each position of the triples. The goal is to get something like the following: get_combinations_without_duplicates(3) = [(0, 1, 2), (1, 2, 0), (2, 0, 1)] For range(3) this is just a list rotation, but for higher ranges, there are more possible combinations. I would like to be able to randomly generate a list of triples that satisfies these

How to print all the possible combinations of a N digit number without using arrays?

自作多情 提交于 2019-12-08 14:06:49
问题 I'm stressing on the point of without using an array. Is there any mathematics derivation for this? 回答1: You can use "Gosper's hack" manipulating binary numbers but you are limited by the word length of the machine on the size of combinations you can generate/represent this way. // find next k-combination bool next_combination(unsigned long& x) // assume x has form x'01^a10^b in binary { unsigned long u = x & -x; // extract rightmost bit 1; u = 0'00^a10^b unsigned long v = u + x; // set last

Making combinations in python

被刻印的时光 ゝ 提交于 2019-12-08 10:04:31
I have four values age = 23 gender = "M" city ="Delhi" religion = "Muslim" I need these arranged by every combination with empty values like - 23 * * * 23 M * * 23 M Delhi * 23 M Delhi Muslim * M * * * M Delhi * * M Delhi Muslim * * Delhi * * * Delhi Muslim * * * Muslim * * * * I need this arranged by the number of dimensions in ascending order in a list. So combinations with one value should be on top. I have some 30+ attributes, so i need an automated way to do this in Python Any ideas ? Gareth Rees NPE's answer solves the problem by constructing the full list of subsets in memory and then

Generate all possible array combinations in C - Optimal Graph Colouring

随声附和 提交于 2019-12-08 09:29:23
问题 I need to generate arrays with all possible combinations, like this question I've found here: Combinatorics: generate all "states" - array combinations I'm doing a simple work of optimal Graph Coloring , so, I'm trying to generate all possible color combinations (the array represents the color for each node). This code is working but, is also doing unnecessary work. In this situation, [1, 1, 2] is the same thing of [2, 2, 1], I don't need to test if this is a valid graph again . I can't think

Number of possible combinations of fork

怎甘沉沦 提交于 2019-12-08 08:08:05
问题 int main(void) { int id = 0; for(int i = 1; i < 4; i++) { if(fork() == 0) { id = i; } else { printf("Process %d created child %d\n", id, i); } } return 0; } In the code above, multiple ordering of the output (printf statements) can be generated based on how the operating system schedules processes for execution. How many different orderings are possible? You may assume that all fork and printf calls succeed. I'm trying to help my students understand how to approach this problem, however I got

Generation of a Hamming Series

好久不见. 提交于 2019-12-08 07:14:20
问题 I have been trying to solve a programming problem, one of the modules of which requires me to generate Hamming sequences. The function takes input two numbers first a binary number N and another a decimal number K. It should now generate all possible numbers having a Hamming distance of up to K from N. It would be really helpful if you provide me with an algorithm about how to solve this problem. Thanks in advance. 回答1: The algorithm is pretty simple. You just need to chose all possible

Generate valid combinations of numbers in array of digits

半腔热情 提交于 2019-12-08 03:13:14
问题 I’m trying to generate all valid combinations of numbers from an array of digits. Let’s assume we have the following: let arr = [1, 2, 9, 4, 7]; We need to output something like this: 1 2 9 4 7 1 2 9 47 1 2 94 7 1 2 947 1 29 4 7 1 29 47 1 294 7 1 2947 12 9 4 7 12 9 47 12 94 7 12 947 129 4 7 129 47 1294 7 12947 An invalid number would be 91, 497, 72 and so on. I tried this but I’m not satisfied with the result: const combination = (arr) => { let i, j, temp; let result = []; let arrLen = arr