Skip to content

Instantly share code, notes, and snippets.

@irfanabduhu
Created March 6, 2022 07:07
Show Gist options
  • Save irfanabduhu/562565614d1a7a042f54b0332514af54 to your computer and use it in GitHub Desktop.
Save irfanabduhu/562565614d1a7a042f54b0332514af54 to your computer and use it in GitHub Desktop.
Boolean Table
#include <iostream>
#include <vector>
using namespace std;
void helper(int n, int i, vector<bool> res);
void binary(int n) {
vector<bool> res(n);
helper(n, 0, res);
}
void helper(int n, int i, vector<bool> res) {
if (i == n) {
for (int k = 0; k < res.size(); k++)
cout << res[k] << " ";
cout << endl;
} else {
res[i] = false;
helper(n, i + 1, res);
res[i] = true;
helper(n, i + 1, res);
}
}
int main(void) {
int n;
cin >> n;
binary(n);
}
def boolean_table(n):
flags = [0] * n
def helper(n, i, flags):
if i == n:
print(flags)
else:
flags[i] = 0
helper(n, i + 1, flags)
flags[i] = 1
helper(n, i + 1, flags)
helper(n, 0, flags)
n = int(input("Number of boolean variables: "))
boolean_table(n)
boolean_table(n):
flags <- boolean array of size n
helper(n, 0, flags)
helper(n, i, flags):
if i == n:
print flags
else:
flags[i] <- False
helper(n, i + 1, flags)
flags[i] <- True
helper(n, i + 1, flags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment