3
\$\begingroup\$

Note: A run is a series of adjacent chars that are the same.

I just want to answer a few questions:

  1. Is this readable?
  2. Does it need commenting or is it self-descriptive enough?
  3. Are there any efficiency tweaks this could have?
  4. Is this a good way to test?
  5. Which is the preferred/conventional syntax?

int maxCheck = 0,
    maxRun = 1,
    currentRun = 1;`

vs.

int maxCheck = 0, maxRun = 1, currentRun = 1;

vs.

int maxCheck = 0;
int maxRun = 1,
int currentRun = 1;

I surmise this probably varies from individual to individual, but for this challenge in particular my instincts were to exclude spaces, but the challenge didn't specify this, and in the last challenge: my intuition, just as the first answer, also wanted me to include digits larger than 9, but that was actually 'wrong' and outside of the specifications of the challenge -- as was evidenced with failed tests (provided by the challenge).

Should I just take 'requirements' literally most of the time to avoid constructing technically 'incorrect' code? I ask since the tests in this case don't include spaces, and it's definitely something I'm wondering in case of larger, more abstract projects.

public class Standford2 {
    public static void main(String[] args) {
        System.out.println("Test 1: " + (1  == maxRun("123")));
        System.out.println("Test 2: " + (2  == maxRun("1223")));
        System.out.println("Test 3: " + (2  == maxRun("112233")));
        System.out.println("Test 4: " + (3  == maxRun("1112233")));
        System.out.println("Test 5: " + (2  == maxRun("hoopla")));
        System.out.println("Test 6: " + (3  == maxRun("hoopllla")));
        System.out.println("Test 7: " + (3  == maxRun("abbcccddbbbxx")));
        System.out.println("Test 8: " + (0  == maxRun("")));
        System.out.println("Test 9: " + (3  == maxRun("hhhooppoo")));
    }

    public static int maxRun(String str) {
        if (str.length() == 0) { return 0; }

        int maxCheck = 0,
            maxRun = 1,
            currentRun = 1;

        if (str.length() == 1) { return maxRun; }

        for (int i = 1; i < str.length(); i++) {
            if (str.charAt(i) == str.charAt(i - 1)) {
                currentRun++;
                if (currentRun > maxRun) { maxRun = currentRun; }
            }
            else { currentRun = 1; }
        }
        return maxRun;
    }
}
\$\endgroup\$
3
\$\begingroup\$

The maxCheck variable is unused. You also don't need the str.length() == 1 special case.

The whitespace around the else is unconventional.

The efficiency is fine. Your algorithm, like any reasonable algorithm for this problem, is O(L), where L is the length of the input string. You could try to squeeze out a small performance improvement, but it's almost certainly not worth it.

For a simple function like this, I don't think you need code comments. However, a JavaDoc comment would always be useful.

The code works equally well on any CharSequence, not just a String, and should therefore be generalized.

/**
 * Finds the length of the longest run of identical characters.
 */
public static int maxRun(CharSequence seq) {
    if (seq.length() == 0) { return 0; }

    int maxRun = 1,
        currentRun = 1;

    for (int i = 1; i < seq.length(); i++) {
        if (seq.charAt(i) == seq.charAt(i - 1)) {
            currentRun++;
            if (currentRun > maxRun) { maxRun = currentRun; }
        } else {
            currentRun = 1;
        }
    }
    return maxRun;
}

The tests that you wrote are fine for a beginner, but the de facto standard for Java is to use .

\$\endgroup\$
  • \$\begingroup\$ I definitely see the appeal of CharSequence over just String. Bookmarked JUnit for future perusal. Any comments on point 5 or is it up to preference/lacking standard convention? \$\endgroup\$ – Legato Jan 1 '15 at 21:02
  • 1
    \$\begingroup\$ The style of declaration/definition is a matter of personal preference. However, you need to ensure that you get the punctuation correct — two of your three samples don't compile. \$\endgroup\$ – 200_success Jan 1 '15 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.