python bit full search

Method 1

>>> from itertools import product
>>> n = 3
>>> [[i for i, j in zip(range(n), mask) if j] for mask in product((0, 1), repeat=n)]
[[], [2], [1], [1, 2], [0], [0, 2], [0, 1], [0, 1, 2]]

>>> n = 5
>>> [[i for i, j in zip(range(n), mask) if j] for mask in product((0, 1), repeat=n)]
[[], [4], [3], [3, 4], [2], [2, 4], [2, 3], [2, 3, 4], [1], [1, 4], [1, 3], [1, 3, 4], [1, 2], [1, 2, 4], [1, 2, 3], [1, 2, 3, 4], [0], [0, 4], [0, 3], [0, 3, 4], [0, 2], [0, 2, 4], [0, 2, 3], [0, 2, 3, 4], [0, 1], [0, 1, 4], [0, 1, 3], [0, 1, 3, 4], [0, 1, 2], [0, 1, 2, 4], [0, 1, 2, 3], [0, 1, 2, 3, 4]]

Method 2

>>> n = 3
>>> [[i for i in range(bit) if bit & (1<<i)] for bit in range(1<<n)]
[[], [0], [1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2]]

>>> n = 5
>>> [[i for i in range(bit) if bit & (1<<i)] for bit in range(1<<n)]
[[], [0], [1], [0, 1], [2], [0, 2], [1, 2], [0, 1, 2], [3], [0, 3], [1, 3], [0, 1, 3], [2, 3], [0, 2, 3], [1, 2, 3], [0, 1, 2, 3], [4], [0, 4], [1, 4], [0, 1, 4], [2, 4], [0, 2, 4], [1, 2, 4], [0, 1, 2, 4], [3, 4], [0, 3, 4], [1, 3, 4], [0, 1, 3, 4], [2, 3, 4], [0, 2, 3, 4], [1, 2, 3, 4], [0, 1, 2, 3, 4]]

case: cpp

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N = 3;

  map<int, vector<int>> mp;
  rep(bit, (1 << N)) rep(i, bit) if (bit & (1 << i)) mp[bit].push_back(i);

  for (auto x : mp) {
    cout << x.first << " : ";
    for (int n : x.second) cout << n << ' ';
    cout << endl;
  }
}

output



1 : 0 
2 : 1 
3 : 0 1 
4 : 2 
5 : 0 2 
6 : 1 2 
7 : 0 1 2 

Recommended Posts

python bit full search
Confirm bit full search
Bit full search with Go
Learn search with Python # 2bit search, permutation search
PYTHON2.7 64bit version
Bit full search and direct product set
Sequential search with Python
Python Exercise 1-Breadth-first search
Binary search in Python
[Python] Search (NumPy) ABC165C
Binary search (python2.7) memo
[Python] Binary search ABC155D
Linear search in Python
Binary search with python
Binary search with Python3
Search Twitter using Python
Binary search in Python (binary search)
Use Search Tweets: Full Archive / Sandbox in Python
[Python] BFS (breadth-first search) ABC168D
Search for strings in Python
Breadth-first search / bidirectional search (Python version)
Search algorithm using word2vec [python]
Homebrew Python --Youtube Search Program
[Python] DFS (Depth-first Search) ATC001A
Solve with Python [100 selected past questions that beginners and intermediates should solve] (010 --014 Full search: Bit full search)
Binary search in Python / C ++
Algorithm in Python (binary search)
[Python] DFS (Depth-first Search) ABC157D
Learn exploration in Python # 1 Full exploration
Search engine work with python
Search twitter tweets with python
[Python] Depth-first search and breadth-first search
Full understanding of Python debugging
[Python] BFS (breadth-first search) ABC007C
Streamline web search with python
Write a binary search in Python
Installing Kivy on Windows10 64bit Python3.5
Python
Put MeCab in "Windows 10; Python3.5 (64bit)"
Install python2.7 on windows 32bit environment
Algorithm in Python (depth-first search, dfs)
I want to do a full text search with elasticsearch + python
Master linear search! ~ Python implementation version ~
Installing Kivy-Designer on Windows10 64bit Python3.5
Learning by ABC173C (bit full search, copying of multidimensional list, one-dimensionalization of multidimensional list)
Build mlpy with python3.3 (64bit) (windows 64bit)
Write a depth-first search in Python
[2021 version] Python installation Windows 10 (64bit) edition
[Python] Although it is a humanities, I will do my best to understand bit full search
Solve the subset sum problem with a full search in Python
Reproduce One-Touch Search on Python 3.7.3. (Windows 10)
Depth-first search using stack in Python
Python 2-minute search and its derivation
About bit full search that often appears in competition pros From the eyes of beginners with python
Algorithm learned with Python 10th: Binary search
Homebrew Python Part 3-Amazon Product Search Program
Change Python 64bit environment to 32bit environment with Anaconda
Python hand play (calculated full of mordred)
Algorithm in Python (ABC 146 C Binary Search
A super introduction to Python bit operations
Search and play YouTube videos in Python