Creating a list of all possible combinations from a set of items for n combination sizes
A recursion can do the job. The idea is to choose a letter, print it as a possibility and combine it with all letters after it:
#include <bits/stdc++.h>
using namespace std;
string letters[] = {"A", "B", "C", "D", "E"};
int alphabetSize = 5;
int combSizeLim = 3;
void gen(int index = 0, int combSize = 0, string comb = ""){
if(combSize > combSizeLim) return;
cout<<comb<<endl;
for(int i = index; i < alphabetSize; i++){
gen(i + 1, combSize + 1, comb + letters[i]);
}
}
int main(){
gen();
return 0;
}
OUTPUT:
A
AB
ABC
ABD
ABE
AC
ACD
ACE
AD
ADE
AE
B
BC
BCD
BCE
BD
BDE
BE
C
CD
CDE
CE
D
DE
E
How to get every possible combination of list integers?
Try itertools.permutations
:
from itertools import permutations
L = [3, 4, 1, 5, 9]
for i in range(1, len(L) + 1):
for p in permutations(L, i):
print(int("".join(map(str, p))))
Prints:
3
4
1
5
9
34
31
35
...
95431
95413
95134
95143
How to create all possible combinations of a list which has lists inside of it?
Possible solution is the following:
from itertools import combinations
lst = [['Macau', 'United Kingdom', 'India'], ['United States', 'China'], ['China', 'Australia']]
print([list(combinations(group, 2)) for group in lst])
Returns
[[('Macau', 'United Kingdom'),
('Macau', 'India'),
('United Kingdom', 'India')],
[('United States', 'China')],
[('China', 'Australia')]]
How to get a the number of all possible combinations?
You can use separate out the zeros and then use itertools.product
-
from itertools import product
x = '0011'
perm_elements = [('0', '1') if digit == '0' else ('1', ) for digit in x]
print([''.join(x) for x in product(*perm_elements)])
['0011', '0111', '1011', '1111']
If you only need the number of such combinations, and not the list itself - that should just be 2 ** x.count('0')
All possible combinations and permutations of size n algorithm
Your task (all permutations of all combinations) can be easily solved using regular recursive function (as I did below) without any fancy algorithm.
Try it online!
#include <vector>
#include <functional>
#include <iostream>
void GenCombPerm(size_t n, size_t k, auto const & outf) {
std::vector<bool> used(n);
std::vector<size_t> path;
std::function<void()> Rec =
[&]{
if (path.size() >= k) {
outf(path);
return;
}
for (size_t i = 0; i < used.size(); ++i) {
if (used[i])
continue;
used[i] = true;
path.push_back(i);
Rec();
path.pop_back();
used[i] = false;
}
};
Rec();
}
int main() {
std::vector<size_t> a = {1, 2, 3};
GenCombPerm(a.size(), 2, [&](auto const & v){
std::cout << "[";
for (auto i: v)
std::cout << a[i] << ", ";
std::cout << "], ";
});
}
Output:
[1, 2, ], [1, 3, ], [2, 1, ], [2, 3, ], [3, 1, ], [3, 2, ],
Related Topics
How to Write Python Array (Data = []) to Excel
How to Adjust Padding With Cutoff or Overlapping Labels
Json.Loads() Decodes Only With Raw String Literal
Python: How to Calculate the Sum of Numbers from a File
How to Find Consecutive Numbers in a Python List
Ssl: Certificate_Verify_Failed With Python3
How to Generate and Open an Outlook Email With Python (But Do Not Send)
How to Concisely Replace Column Values Given Multiple Conditions
How to Install a Conda Environment When Offline
Find Similar List Value Inside Dictionary
Grab a Number After a String in a File
How to Call a Classes Method from Another Class Without Initialising the First Class
How to Block Comment Code in the Ipython Notebook
Python3 Tkinter Set Image Size
Python Check Multi-Level Dict Key Existence
How to Compute Mean() for Particular Column in Pandas Dataframe Without Considering Nan Values