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 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).
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).
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).
Challenge: Finding the length of the largest run
- Is this readable?
- Does it need commenting or is it self-descriptive enough?
- Are there any efficiency tweaks this could have?
- Is this a good way to test?
- Which is the preferred/conventional syntax?
public class Standford2 {
public static void main(String[] args) {
System.out.println("Test 1: " + (1 == maxRun("123")));
System.out.println("Test 12: " + (2 == maxRun("1223")));
System.out.println("Test 13: " + (2 == maxRun("112233")));
System.out.println("Test 14: " + (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;
}
}
- Is this readable?
- Does it need commenting or is it self-descriptive enough?
- Are there any efficiency tweaks this could have?
- Which is the preferred/conventional syntax?
public class Standford2 {
public static void main(String[] args) {
System.out.println("Test 1: " + (1 == maxRun("123")));
System.out.println("Test 1: " + (2 == maxRun("1223")));
System.out.println("Test 1: " + (2 == maxRun("112233")));
System.out.println("Test 1: " + (3 == maxRun("1112233")));
}
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;
}
}
- Is this readable?
- Does it need commenting or is it self-descriptive enough?
- Are there any efficiency tweaks this could have?
- Is this a good way to test?
- Which is the preferred/conventional syntax?
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;
}
}