Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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).

edited tags; edited title
Link
Legato
  • 9.9k
  • 4
  • 50
  • 118

Challenge: Finding the length of the largest run

Some test cases missed + 1 additional question
Source Link
Legato
  • 9.9k
  • 4
  • 50
  • 118
  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?
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;
 }
}
  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. 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;
 }
}
  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?
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;
 }
}
added 56 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
Legato
  • 9.9k
  • 4
  • 50
  • 118
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /