I have checked the output on some patterns up to 5 on bits (5 1s) and it looks correct. As far as I can tell, there should be 2^n output patterns where n=the number of on bits. Do I have that right? For my test pattern of 5 on bits, there are 2^5=25 output patterns.
This is the output organized for simplified analysis,
Code:
# for the input pattern
bk_00110000000000110001000
# 4 on bits (change 1 1 to 0)
bk_00010000000000110001000
bk_00100000000000110001000
bk_00110000000000010001000
bk_00110000000000100001000
bk_00110000000000110000000
# 3 on bits (change 2 1s to 0)
bk_000000000000001100011000
bk_00010000000000010001000
bk_00010000000000100001000
bk_00010000000000110000000
bk_00100000000000010001000
bk_00100000000000100001000
bk_00100000000000110000000
bk_00110000000000000001000
bk_00110000000000010000000
bk_00110000000000100000000
# 2 on bits (change 3 1s to 0)
bk_00000000000000010001000
bk_00000000000000100001000
bk_00000000000000110000000
bk_00100000000000010000000
bk_00100000000000100000000
bk_00100000000000000001000
bk_00010000000000010000000
bk_00010000000000100000000
bk_00010000000000000001000
bk_00110000000000000000000
# 1 on bit (change 4 1s to 0)
bk_00000000000000000001000
bk_00000000000000010000000
bk_00000000000000100000000
bk_00010000000000000000000
bk_00100000000000000000000
As far as I can tell, this is what the output should be. Please let me know if anyone sees anything amiss.
The next thing I need to do is to add a function to check each subset generated against a list of subsets and look for matches.
My revised script looks like,
Code:
#! /bin/bash
function check_against {
check_string=$1
pattern_match=0
# declare list of patterns to check agains
check_list=( bk_00110000000000000001000 \
bk_00110000000000000001000 \
bk_00010000000000000001000 \
bk_00010000000000000001000 \
bk_00010000000000000001000 \
bk_00010000000000000001000 \
bk_10001010011100000101000 \
bk_00001010111100000101000 \
bk_10001110000000000101010 \
bk_10001110000000110101000 \
bk_11001110000000110101000 \
bk_11110011011100110000000 \
bk_00110000000000110000010 )
# loop through check_list and compare each element to check_string
for check_against_string in "${check_list[@]}"
do
if [ "$check_string" == "$check_against_string" ]; then
pattern_match=$((pattern_match+1))
fi
done
# if any matches were found, output match and number of matches
if [ "$pattern_match" != "0" ]; then
echo -e "$check_string\t$pattern_match"
fi
}
# input string
input_string="${1:-"bk_00110000000000110001000"}"
# capture output of awk into string
subsets_list=$(
echo "$input_string" | awk -F1 '
# Compute 2**p - 1 for p >= 1
function two_e2m1(p, i, v) {
v = 0
for(i = 1; i < p; i++)
v = 2 * v + 1
return(v)
}
NF { printf("%s is input to be processed.\n", $0)
for(i = two_e2m1(NF) - 1; i > 0; i--) {
v = i
for(j = 1; j < NF; j++) {
d[NF - j] = v % 2
v /= 2
}
for(j = 1; j < NF; j++)
printf("%s%d", $j, d[j])
print $NF
}
}'
)
# parse subsets_list on newline
IFS=$'\n' read -rd '' -a check_list <<<"$subsets_list"
for element in "${check_list[@]}"
do
# pass each element to check against function
check_against $element
done
This is a bit of a hack, but I capture the output of Don Cragun's awk code in a string variable and then parse that into an array on newline. I then iterate through the array and pass each element to a function that compares the element against an array and counts the number of matches found. If any matches are found, the matching pattern is printed along with the number of matches.
This gives me the output I need as far as I can tell. Please let me know if anyone sees problems with this approach or has other suggestions.
Having problem in using the grep command to select all possible combinations a number in a file. Example: 123, I would like to grep the numbers 123,132,213,231,312 and 321. (2 Replies)
Hello All,
i have two files, one of the format
A 123
B 124
C 234
D 345
And the other
A 678
B 789
C 689
D 567
I would like to combine them into one file with three columns:
A 123 678
B 124 789
C 234 689 (4 Replies)
Hi,
I am using the Korn-Shell (ksh) and would like to know all the shortcut keys. For example: Shift + Insert etc.
Thank you very much.
Take care (0 Replies)
Hi,
I need to generate all combinations upto n-1 level,
if the input file looks like say,
A
B
C
D
.
.
....
I need to generate all combinations such that first value remains constant and the remaning are combined with all possible ways.
Output
A
AB
AC
AD
ABC (1 Reply)
HI I have a series(sorted), which i require to create combinations. I am not getting the good code for doing this. My series should generate the following combinations... Please help me in getting this in C++. Thanks for your help.
A:
A
A B:
A
B
A B
A B C:
A
... (1 Reply)
Hi Team
when i do, echo on my host box it returns (see below)
# echo $PS1
\$
I need to set a color comination of my own
for \u means for user : red
for \h means for hostname: blue
for \W means present working directory: pink
for $ means for wht prompt : yellow
Do i need to... (1 Reply)
Hello Scrutinizer / Group ,
The shell script of awk that Scrutinizer made calculate all possible permutations in this case 3125 (5 numbers) but i want to have only the 126 possible combination. For now it does not matter the specific order of the combination numbers.
I would appreciate it you... (1 Reply)
How can I get all combinations of 5 words from 10 words.
For example I have 3 words and I want to get all combinations of 2 words.
"A", "B", "C" it would like AB, BC, AC.
Maybe you know some usefull code or example.
Thanx a lot.
P.S. Sorry if I'm not right enough cause I don't know English... (2 Replies)
Hi,
I have an input file like this
a
b
c
d
I want to print all possible combinations between these records in the following way
aVSb
aVSc
aVSd
bVSc
bVSd
cVSd
VS indicates versus. All thoughts are appreciated. (5 Replies)
hello,
I'm reading this thread, in which there is this code :awk '
function comb(v,i) {
for(i in A) {
delete A;
if(length(A))
comb((v?v"+":x)i)
else print v"+"i
A;
}
}
{ A }
END {
comb();
} ' infilebut I can't understand where does v come... (5 Replies)