0
java version "1.8.0_92"

Hi,

I am trying to convert this function that uses iteration to a recursive one.

  public static String bitConversion(int x) {
        List<Integer> binaryList = new ArrayList<Integer>();

        while(x > 0) {
            binaryList.add(x % 2);
            x /= 2;
        }

        StringBuilder stringBuilder = new StringBuilder();
        for(Integer binary : binaryList) {
            stringBuilder.append(binary.toString());
        }

        return stringBuilder.toString();
    }

My attempt is this

  public static String bitConversion(int x) {
        List<Integer> binaryList = new ArrayList<Integer>();

        if(x <= 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for(Integer binary : binaryList) {
                stringBuilder.append(binary.toString());
            }

            return stringBuilder.toString();
        }
        else {
            binaryList.add(x % 2);
            return bitConvert(x / 2);
        }
    }

One thing is that I need to have the binaryList to add the Integer to. In the first condition when everything is finished I need to put them into a stringbuilder. And in the second condition I need to add them to the list. So the List need to be global to both condition. But as the function calls itself the List will be re-initialized each time.

Can any advice on the best way to write this recursive function?

Many thanks for any suggestions.

2
  • No, in a recursive solution you don't need the list at all. Commented Oct 14, 2016 at 16:54
  • You don't need a List in the iterative solution either. Just append to your StringBuilder directly in the first loop. Commented Oct 14, 2016 at 17:02

3 Answers 3

5

Basically with recursion you have 2 parts: 1) recursion termination condition 2) the part where you split the task at hand to smaller pieces and than compose their results.

public static String bitConversion(int x) {
    // just simplifying my life here as negative numbers can be represented
    // in a few ways and usually it's additive code I don't want to deal
    // with here :)
    if (x < 0) {
        throw new IllegalArgumentException("Not implemented for negatives");
    }

    // recursion termination condition for 0 and 1
    if (x <= 1) {
        return String.valueOf(x);
    }

    // recurision
    int leastSignificantBit = x % 2;
    String significantBits = bitConversion(x / 2);
    return significantBits + leastSignificantBit;
}
Sign up to request clarification or add additional context in comments.

Comments

3
public static String bitConversion(int x) {
    if (x <= 0) return "";
    else return x % 2 + bitConversion(x / 2);
}

How about something like this one above. The bad thing is that it doesn't use StringBuilder. But you could implement similar one which would return the StringBuilder and then wrap the call in .toString in another method.

Comments

2

Your recursion end condition is x == 0, then you return empty string. When x > 0, you should just recursively invoke method for x / 2.

So, something like this?

public static String bitConversion(int x) {
    if (x == 0) {
        return "";
    }
    return (x % 2) + bitConversion(x / 2);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.