Getting the correct output but in the opposite order (hamming distance)

174 Views Asked by At

Good afternoon everyone, Just looking to see if anyone could help. I am getting the right output but it is in reverse order for some reason. I think it is something small in my code that I messed up but just trying to see if fresh eyes looking at it could help.

“Hamming,” will read in an integer “k” and a bit string “s” from the command line, calculate the “Hamming Distances,” and prints all bit strings that have Hamming distance of “k” from “s.”

Note: The Hamming Distance is equal to the number of bits in which the two strings differ.

A sample run would be as follows.

>java Hamming 2 0011
 1111 1001 1010 0101 0110 0000

My current code is as follows.

public class Hamming{

public static void main(String[] args) {
    int k = Integer.parseInt(args[0]); // first argument, hamming distance

    String s = args[1]; // second argument, binary string

    // int array of binary bits that make up the binary string
    int[] bitsString = new int[s.length()];

    // genBinaryStrings(length of string, array of bits, iterating value, hamming distance, binary string)  
    genBinaryStrings(s.length(), bitsString, 0, k, s);
}


//Recursive function that prints all binary strings that are 'h' hamming distance from original
 
private static void genBinaryStrings(int numBits, int binString[], int i, int h, String givenBinString) {

    if (i == numBits) { // base case
        // convert array of bits into binary string of bits
        String currResult = "";
        for (int bit : binString) {
            currResult += Integer.toString(bit);
        }
        
        // check the hamming distance between the current result and the original string
        if (h == getHamDistance(currResult, givenBinString)) {
            System.out.print(currResult + " "); // valid hamming distance, print
        }
        return;
    }
    // generate next possible binary strings
    // generate binary string where next value = 0
    binString[i] = 0;
    genBinaryStrings(numBits, binString, i+1, h, givenBinString);

    // generate binary string where next value = 1
    binString[i] = 1;
    genBinaryStrings(numBits, binString, i+1, h, givenBinString);
}



// This method will Compute Hamming Distance between two binary string
private static int getHamDistance(String resultBinString, String givenBinString) {
    int hamDist = 0; // assume distance is 0 initially

    // iterate thru current resulting binary string
    for (int i = 0; i < resultBinString.length(); i++) {
        // check for differing bit values
        if (resultBinString.charAt(i) != givenBinString.charAt(i))
            hamDist++; // binary string is 1 more bit different
    }
    return hamDist; // total distance
}

}

The current output I am getting for my code is:

>java Hamming 2 0011
 0000 0101 0110 1001 1010 1111

Any help would be greatly appreciated, thank you!

0

There are 0 best solutions below