Howdy. 

Today: Homework Nine

Let's look through the book. 

Labs this week? 

Rest of semester? 

How would you approach the homework problem(s)? 

I have a second exercise for us today: 

  assume a file like this

  Mitt Romney 3
  Mitt Romney -7
  Barack Hussein Obama II 4
  Trump 6
  Mitt Romney 9
  Barack Hussein Obama II 5

  can you summarize it and print:

  (a) the candidate with the highest average (e.g., Trump above) 

  (b) the candidate with the highest sum (Obama)

  (c) the candidate that shows up most often (Romney)


So we start on the homework. 

We first write a class with a method to generate random data. 

It runs like this: 

-bash-4.1$ javac One.java
-bash-4.1$ java One
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at One.main(One.java:5)
-bash-4.1$ java One 6 20
[1, 6, 2, 5, 2, 3, 6, 4, 2, 4, 4, 1, 5, 1, 2, 6, 1, 2, 5, 1]
-bash-4.1$ java One 6 20
[6, 4, 1, 4, 6, 6, 1, 3, 5, 4, 2, 6, 4, 5, 6, 5, 3, 6, 2, 1]
-bash-4.1$ java One 6 20
[3, 6, 1, 4, 3, 5, 1, 3, 4, 2, 4, 1, 4, 2, 3, 5, 1, 6, 5, 4]
-bash-4.1$ java One 6 20
[1, 5, 3, 2, 5, 2, 4, 6, 4, 3, 6, 3, 3, 6, 6, 2, 5, 4, 2, 2]
-bash-4.1$ 

Here's how the code looks: 

-bash-4.1$ cat One.java
import java.util.*;

class One {
  public static void main(String[] args) {
    int range = Integer.parseInt(args[0]),
        number = Integer.parseInt(args[1]);
    ArrayList<Integer> numbers = One.generate(range, number);
    System.out.println( numbers );
  }
  public static ArrayList<Integer> generate(int range, int number) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < number; i++) {
      int value = (int) (Math.random() * range) + 1;
      result.add(value);
    }
    return result;
  }
}
-bash-4.1$

Now I add an abstraction (Inspector) and make it simulate what I want: 

-bash-4.1$ pico -w One.java
-bash-4.1$ javac One.java
-bash-4.1$ java One 6 10
[5, 5, 5, 5, 4, 3, 5, 4, 4, 5]
[]
-bash-4.1$ java One 6 10
[1, 5, 3, 6, 6, 1, 4, 3, 3, 1]
[]
-bash-4.1$ java One 6 10
[2, 2, 5, 5, 3, 6, 5, 2, 2, 4]
[]
-bash-4.1$ 

Finally I provide the functionality I want inside it: 

-bash-4.1$ pico -w One.java
-bash-4.1$ cat One.java
import java.util.*;

class One {
  public static void main(String[] args) {
    int range = Integer.parseInt(args[0]),
        number = Integer.parseInt(args[1]);
    ArrayList<Integer> numbers = One.generate(range, number);
    System.out.println( numbers );
    Inspector gadget = new Inspector( numbers );
    ArrayList<ArrayList<Integer>> answer = gadget.go();
    System.out.println( answer );
  }
  public static ArrayList<Integer> generate(int range, int number) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < number; i++) {
      int value = (int) (Math.random() * range) + 1;
      result.add(value);
    }
    return result;
  }
}

class Inspector {

  ArrayList<Integer> numbers;

  Inspector(ArrayList<Integer> numbers) {
    this.numbers = numbers;
  }

  ArrayList<ArrayList<Integer>> go() {
    ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

    while (this.numbers.size() > 0) { // for as long as you have data {
      Integer value = this.numbers.get(0);
      this.numbers.remove(0);
      ArrayList<Integer> row = new ArrayList<Integer>(); // my first run: empty
      row.add(value);
      while numbers the same {
        row.add(number);
      }
      result.add(row);
      row = new ....
    }

    return result;
  }
}

-bash-4.1$

This is unfinished but there's little left. 

Here's the finished code below and a sample run: 

bash-4.1$ javac One.java 
bash-4.1$ java One       
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
        at One.main(One.java:6)                                       
bash-4.1$ java One 6 20                                               
[5, 6, 6, 4, 2, 4, 2, 4, 2, 5, 3, 2, 3, 2, 5, 4, 4, 4, 4, 2]          
[[5], [6, 6], [4], [2], [4], [2], [4], [2], [5], [3], [2], [3], [2], [5], [4, 4, 4, 4], [2]]
bash-4.1$ java One 6 20                                                                     
[4, 3, 5, 4, 6, 6, 1, 1, 4, 1, 2, 6, 6, 1, 1, 1, 2, 4, 6, 3]                                
[[4], [3], [5], [4], [6, 6], [1, 1], [4], [1], [2], [6, 6], [1, 1, 1], [2], [4], [6], [3]]  
bash-4.1$ java One 6 20                                                                     
[3, 6, 1, 6, 3, 3, 6, 1, 2, 5, 2, 1, 5, 6, 3, 2, 6, 4, 2, 2]                                
[[3], [6], [1], [6], [3, 3], [6], [1], [2], [5], [2], [1], [5], [6], [3], [2], [6], [4], [2, 2]]
bash-4.1$ java One 6 20                                                                         
[1, 3, 1, 1, 1, 4, 2, 3, 4, 1, 2, 6, 6, 4, 6, 2, 5, 5, 4, 1]                                    
[[1], [3], [1, 1, 1], [4], [2], [3], [4], [1], [2], [6, 6], [4], [6], [2], [5, 5], [4], [1]]    
bash-4.1$ cat One.java                                                                         
import java.util.*;

class One {

  public static void main(String[] args) {
    int range = Integer.parseInt(args[0]),
        number = Integer.parseInt(args[1]);
    ArrayList<Integer> numbers = One.generate(range, number);
    System.out.println( numbers );

    Inspector gadget = new Inspector( numbers );

    ArrayList<Run> answer = gadget.go();

    System.out.println( answer );
  }

  public static ArrayList<Integer> generate(int range, int number) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < number; i++) {
      int value = (int) (Math.random() * range) + 1;
      result.add(value);
    }
    return result;
  }
}

class Run extends ArrayList<Integer> {

}

class Inspector {
  ArrayList<Integer> numbers;
  Inspector(ArrayList<Integer> numbers) {
    this.numbers = numbers;
  }
  Run row = new Run();

  ArrayList<Run> go() {
    ArrayList<Run> result = new ArrayList<Run>();
    while (this.hasRun()) {
      result.add(this.getRun());
    }
    return result;
  }

  boolean hasRun() {
    return (this.row.size() == 0 && this.numbers.size() != 0);
  }

  Run getRun() {
    Run row = new Run();
    row.add(this.numbers.remove(0));
    while (this.numbers.size() > 0 && row.get(0) == this.numbers.get(0)) {
      row.add( this.numbers.remove(0) );
    }
    return row;

  }


}

bash-4.1$

You noticed I modified a few types along the way. 

Can we do anything to justify Run? 

The answer is: yes. 

Take a look:

bash-4.1$ javac One.java 
bash-4.1$ java One 6 20  
[6, 3, 2, 5, 4, 5, 6, 6, 6, 3, 6, 1, 2, 3, 5, 5, 6, 1, 1, 3]
[6 , 3 , 2 , 5 , 4 , 5 , [6, 6, 6], 3 , 6 , 1 , 2 , 3 , [5, 5], 6 , [1, 1], 3 ]
bash-4.1$ java One 6 20                                                        
[1, 2, 6, 4, 6, 5, 4, 6, 6, 1, 3, 2, 4, 1, 4, 1, 4, 1, 6, 2]                   
[1 , 2 , 6 , 4 , 6 , 5 , 4 , [6, 6], 1 , 3 , 2 , 4 , 1 , 4 , 1 , 4 , 1 , 6 , 2 ]
bash-4.1$ java One 6 20                                                         
[5, 4, 4, 4, 5, 6, 1, 4, 6, 4, 1, 1, 4, 4, 5, 3, 4, 4, 1, 3]                    
[5 , [4, 4, 4], 5 , 6 , 1 , 4 , 6 , 4 , [1, 1], [4, 4], 5 , 3 , [4, 4], 1 , 3 ] 
bash-4.1$ java One 6 20                                                         
[1, 2, 3, 2, 1, 4, 1, 5, 4, 1, 1, 4, 1, 3, 5, 3, 1, 3, 4, 1]                    
[1 , 2 , 3 , 2 , 1 , 4 , 1 , 5 , 4 , [1, 1], 4 , 1 , 3 , 5 , 3 , 1 , 3 , 4 , 1 ]
bash-4.1$ java One 9 30
[1, 7, 8, 3, 8, 3, 5, 6, 2, 2, 4, 4, 8, 7, 7, 1, 3, 5, 8, 3, 6, 6, 3, 9, 4, 8, 5, 6, 2, 4]
[1 , 7 , 8 , 3 , 8 , 3 , 5 , 6 , [2, 2], [4, 4], 8 , [7, 7], 1 , 3 , 5 , 8 , 3 , [6, 6], 3 , 9 , 4 , 8 , 5 , 6 , 2 , 4 ]
bash-4.1$ java One 9 30                                                                                                 
[4, 3, 9, 9, 3, 8, 5, 2, 9, 9, 9, 9, 4, 5, 4, 9, 7, 7, 1, 5, 2, 4, 7, 7, 8, 2, 9, 3, 6, 4]                              
[4 , 3 , [9, 9], 3 , 8 , 5 , 2 , [9, 9, 9, 9], 4 , 5 , 4 , 9 , [7, 7], 1 , 5 , 2 , 4 , [7, 7], 8 , 2 , 9 , 3 , 6 , 4 ]  
bash-4.1$ java One 9 30                                                                                                 
[3, 8, 9, 5, 5, 7, 2, 6, 3, 1, 9, 6, 6, 9, 2, 2, 8, 1, 1, 7, 2, 2, 1, 1, 2, 5, 6, 4, 2, 2]                              
[3 , 8 , 9 , [5, 5], 7 , 2 , 6 , 3 , 1 , 9 , [6, 6], 9 , [2, 2], 8 , [1, 1], 7 , [2, 2], [1, 1], 2 , 5 , 6 , 4 , [2, 2]]
bash-4.1$ java One 9 30                                                                                                 
[1, 6, 1, 4, 9, 2, 5, 5, 3, 5, 7, 1, 9, 9, 3, 7, 3, 4, 5, 4, 9, 5, 5, 1, 4, 8, 5, 3, 5, 5]                              
[1 , 6 , 1 , 4 , 9 , 2 , [5, 5], 3 , 5 , 7 , 1 , [9, 9], 3 , 7 , 3 , 4 , 5 , 4 , 9 , [5, 5], 1 , 4 , 8 , 5 , 3 , [5, 5]]
bash-4.1$ java One 9 30                                                                                                 
[7, 1, 2, 4, 4, 1, 4, 2, 6, 4, 8, 4, 9, 9, 7, 2, 2, 3, 7, 9, 5, 2, 7, 9, 8, 5, 7, 5, 5, 6]                              
[7 , 1 , 2 , [4, 4], 1 , 4 , 2 , 6 , 4 , 8 , 4 , [9, 9], 7 , [2, 2], 3 , 7 , 9 , 5 , 2 , 7 , 9 , 8 , 5 , 7 , [5, 5], 6 ]
bash-4.1$ java One 9 30                                                                                                 
[4, 9, 7, 5, 4, 4, 1, 4, 9, 6, 4, 7, 5, 6, 9, 4, 6, 6, 8, 9, 1, 7, 8, 7, 6, 7, 2, 9, 6, 1]                              
[4 , 9 , 7 , 5 , [4, 4], 1 , 4 , 9 , 6 , 4 , 7 , 5 , 6 , 9 , 4 , [6, 6], 8 , 9 , 1 , 7 , 8 , 7 , 6 , 7 , 2 , 9 , 6 , 1 ]
bash-4.1$ java One 9 30                                                                                                 
[1, 3, 7, 1, 6, 3, 6, 9, 6, 1, 7, 3, 7, 3, 8, 4, 1, 7, 4, 7, 9, 1, 2, 6, 5, 1, 2, 3, 2, 9]                              
[1 , 3 , 7 , 1 , 6 , 3 , 6 , 9 , 6 , 1 , 7 , 3 , 7 , 3 , 8 , 4 , 1 , 7 , 4 , 7 , 9 , 1 , 2 , 6 , 5 , 1 , 2 , 3 , 2 , 9 ]
bash-4.1$ cat One.java                                                                                                  
import java.util.*;                                                                                                     

class One {

  public static void main(String[] args) {
    int range = Integer.parseInt(args[0]),
        number = Integer.parseInt(args[1]); 
    ArrayList<Integer> numbers = One.generate(range, number); 
    System.out.println( numbers );                            

    Inspector gadget = new Inspector( numbers ); 

    ArrayList<Run> answer = gadget.go(); 

    System.out.println( answer );
  }

  public static ArrayList<Integer> generate(int range, int number) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    for (int i = 0; i < number; i++) {
      int value = (int) (Math.random() * range) + 1;
      result.add(value);
    }
    return result;
  }
}

+--------------------------------------------+
| class Run extends ArrayList<Integer> {     |
|   public String toString() {               | 
|     if (this.size() > 1) {                 | 
|       return super.toString();             |
|     } else {                               |
|       String result = "";                  |
|       for (Integer num : this)             |
|         result += num + " ";               |
|       return result;                       |
                                             |
|     }                                      |
|   }                                        |
| }                                          |
+--------------------------------------------+


class Inspector {
  ArrayList<Integer> numbers;
  Inspector(ArrayList<Integer> numbers) {
    this.numbers = numbers;
  }
  Run row = new Run();

  ArrayList<Run> go() {
    ArrayList<Run> result = new ArrayList<Run>();
    while (this.hasRun()) {
      result.add(this.getRun());
    }
    return result;
  }

  boolean hasRun() {
    return (this.row.size() == 0 && this.numbers.size() != 0);
  }

  Run getRun() {
    Run row = new Run();
    row.add(this.numbers.remove(0));
    while (this.numbers.size() > 0 && row.get(0) == this.numbers.get(0)) {
      row.add( this.numbers.remove(0) );
    }
    return row;

  }


}

Now how do I determine the largest (max) run? 

Assume that the following characteristics come into play in that case: 

(a) the longest run is the largest 

(b) between two runs of same length the one with the bigger value is larger  

(c) when the value is the same the one that starts farthest away is larger 

Our goal is to identify the largest run only. 

Here's what we have now: 

bash-4.1$ javac One.java
bash-4.1$ java One 6 20
[5, 5, 1, 6, 1, 6, 3, 4, 4, 5, 6, 6, 6, 5, 2, 3, 4, 5, 4, 3]
[[5, 5], 1 , 6 , 1 , 6 , 3 , [4, 4], 5 , [6, 6, 6], 5 , 2 , 3 , 4 , 5 , 4 , 3 ]
The max run is: [6, 6, 6]                                                      
bash-4.1$ java One 6 20                                                        
[6, 6, 1, 3, 6, 4, 4, 4, 6, 6, 3, 1, 2, 6, 5, 1, 6, 6, 5, 4]                   
[[6, 6], 1 , 3 , 6 , [4, 4, 4], [6, 6], 3 , 1 , 2 , 6 , 5 , 1 , [6, 6], 5 , 4 ]
The max run is: [4, 4, 4]                                                      
bash-4.1$ java One 6 20                                                        
[1, 3, 5, 6, 3, 5, 2, 1, 2, 5, 3, 2, 3, 6, 6, 2, 6, 1, 1, 4]                   
[1 , 3 , 5 , 6 , 3 , 5 , 2 , 1 , 2 , 5 , 3 , 2 , 3 , [6, 6], 2 , 6 , [1, 1], 4 ]
The max run is: [6, 6]                                                          
bash-4.1$ java One 6 20                                                         
[3, 4, 4, 2, 3, 2, 1, 5, 3, 3, 2, 1, 4, 5, 2, 5, 2, 5, 5, 2]                    
[3 , [4, 4], 2 , 3 , 2 , 1 , 5 , [3, 3], 2 , 1 , 4 , 5 , 2 , 5 , 2 , [5, 5], 2 ]
The max run is: [5, 5]                                                          
bash-4.1$ java One 6 20                                                         
[4, 5, 4, 3, 5, 5, 4, 2, 1, 1, 4, 2, 1, 1, 3, 2, 3, 4, 1, 5]                    
[4 , 5 , 4 , 3 , [5, 5], 4 , 2 , [1, 1], 4 , 2 , [1, 1], 3 , 2 , 3 , 4 , 1 , 5 ]
The max run is: [5, 5]                                                          
bash-4.1$ java One 6 20                                                         
[3, 5, 1, 2, 1, 6, 5, 3, 1, 5, 4, 5, 6, 4, 2, 4, 1, 5, 2, 2]                    
[3 , 5 , 1 , 2 , 1 , 6 , 5 , 3 , 1 , 5 , 4 , 5 , 6 , 4 , 2 , 4 , 1 , 5 , [2, 2]]
The max run is: [2, 2]                                                          
bash-4.1$ java One 6 20                                                         
[3, 6, 2, 2, 6, 4, 6, 6, 6, 1, 3, 5, 2, 4, 4, 5, 5, 5, 6, 4]                    
[3 , 6 , [2, 2], 6 , 4 , [6, 6, 6], 1 , 3 , 5 , 2 , [4, 4], [5, 5, 5], 6 , 4 ]  
The max run is: [6, 6, 6]                                                       
bash-4.1$ java One 6 20                                                         
[4, 5, 5, 2, 2, 3, 4, 1, 3, 3, 3, 3, 4, 6, 3, 1, 4, 3, 6, 4]                    
[4 , [5, 5], [2, 2], 3 , 4 , 1 , [3, 3, 3, 3], 4 , 6 , 3 , 1 , 4 , 3 , 6 , 4 ]  
The max run is: [3, 3, 3, 3]                                                    
bash-4.1$ java One 6 20                                                         
[2, 5, 1, 4, 3, 4, 2, 4, 6, 6, 4, 4, 3, 4, 4, 4, 5, 6, 2, 1]                    
[2 , 5 , 1 , 4 , 3 , 4 , 2 , 4 , [6, 6], [4, 4], 3 , [4, 4, 4], 5 , 6 , 2 , 1 ] 
The max run is: [4, 4, 4]                                                       
bash-4.1$ java One 6 20                                                         
[6, 6, 5, 3, 2, 1, 3, 6, 5, 4, 1, 6, 5, 6, 4, 2, 5, 1, 5, 3]                    
[[6, 6], 5 , 3 , 2 , 1 , 3 , 6 , 5 , 4 , 1 , 6 , 5 , 6 , 4 , 2 , 5 , 1 , 5 , 3 ]
The max run is: [6, 6]                                                          
bash-4.1$ java One 6 20                                                         
[4, 4, 3, 1, 4, 4, 6, 4, 4, 4, 5, 5, 4, 1, 1, 3, 6, 5, 1, 4]                    
[[4, 4], 3 , 1 , [4, 4], 6 , [4, 4, 4], [5, 5], 4 , [1, 1], 3 , 6 , 5 , 1 , 4 ] 
The max run is: [4, 4, 4]                                                       
bash-4.1$ java One 6 20                                                         
[3, 2, 3, 4, 5, 3, 2, 6, 3, 5, 2, 6, 4, 1, 5, 4, 2, 4, 1, 4]                    
[3 , 2 , 3 , 4 , 5 , 3 , 2 , 6 , 3 , 5 , 2 , 6 , 4 , 1 , 5 , 4 , 2 , 4 , 1 , 4 ]
The max run is: 6                                                               
bash-4.1$ java One 6 20                                                         
[6, 2, 4, 5, 3, 3, 2, 6, 4, 5, 2, 4, 3, 5, 3, 3, 5, 4, 2, 5]                    
[6 , 2 , 4 , 5 , [3, 3], 2 , 6 , 4 , 5 , 2 , 4 , 3 , 5 , [3, 3], 5 , 4 , 2 , 5 ]
The max run is: [3, 3]                                                          
bash-4.1$ cat One.java                                                          
import java.util.*;                                                             

class One {

  public static void main(String[] args) {
    int range = Integer.parseInt(args[0]),
        number = Integer.parseInt(args[1]); 
    ArrayList<Integer> numbers = One.generate(range, number); 
    System.out.println( numbers );                            
    Inspector gadget = new Inspector( numbers );              
    ArrayList<Run> answer = gadget.go();                      
    System.out.println( answer );                             

    Run max = gadget.findMax(answer); 
    System.out.println( "The max run is: " + max ); 

  }

  public static ArrayList<Integer> generate(int range, int number) {
    ArrayList<Integer> result = new ArrayList<Integer>();           
    for (int i = 0; i < number; i++) {                              
      int value = (int) (Math.random() * range) + 1;                
      result.add(value);                                            
    }                                                               
    return result;                                                  
  }
}

class Run extends ArrayList<Integer> implements Comparable<Run> {
  public int compareTo(Run other) {
    if (this.size() > other.size()) {
      return -1;
    } else {
      if (other.size() > this.size()) {
        return 1;
      } else {
        return - this.get(0).compareTo(other.get(0));
      }
    }
  }
  public String toString() {
    if (this.size() > 1) {
      return super.toString();
    } else {
      String result = "";
      for (Integer num : this)
        result += num + " ";
      return result;
    }
  }
}

class Inspector {
  ArrayList<Integer> numbers;
  Inspector(ArrayList<Integer> numbers) {
    this.numbers = numbers;
  }
  Run row = new Run();

  ArrayList<Run> go() {
    ArrayList<Run> result = new ArrayList<Run>();
    while (this.hasRun()) {
      result.add(this.getRun());
    }
    return result;
  }

  Run findMax(ArrayList<Run> runs) {
    Run max = new Run();
    for (Run run : runs)
      max = max.compareTo(run) < 0 ? max : run;
    return max;
  }

  boolean hasRun() {
    return (this.row.size() == 0 && this.numbers.size() != 0);
  }

  Run getRun() {
    Run row = new Run();
    row.add(this.numbers.remove(0));
    while (this.numbers.size() > 0 && row.get(0) == this.numbers.get(0)) {
      row.add( this.numbers.remove(0) );
    }
    return row;
  }
}

bash-4.1$

Finally here's the code that does everything: 


bash-4.1$ javac One.java
bash-4.1$ java One 6 20 
[6, 5, 1, 1, 6, 6, 4, 6, 4, 1, 2, 2, 3, 4, 1, 3, 3, 2, 1, 1]
[6 , 5 , [1, 1], [6, 6], 4 , 6 , 4 , 1 , [2, 2], 3 , 4 , 1 , [3, 3], 2 , [1, 1]]
The max run is: [6, 6]                                                          
 6 5 1 1 ( 6 6 ) 4 6 4 1 2 2 3 4 1 3 3 2 1 1                                    
bash-4.1$ java One 6 20                                                         
[1, 2, 1, 1, 1, 1, 2, 3, 2, 4, 5, 4, 6, 3, 4, 6, 4, 6, 6, 2]                    
[1 , 2 , [1, 1, 1, 1], 2 , 3 , 2 , 4 , 5 , 4 , 6 , 3 , 4 , 6 , 4 , [6, 6], 2 ]  
The max run is: [1, 1, 1, 1]                                                    
 1 2 ( 1 1 1 1 ) 2 3 2 4 5 4 6 3 4 6 4 6 6 2                                    
bash-4.1$ java One 6 20                                                         
[4, 6, 3, 3, 6, 6, 5, 3, 4, 5, 5, 1, 4, 4, 6, 6, 6, 3, 3, 6]                    
[4 , 6 , [3, 3], [6, 6], 5 , 3 , 4 , [5, 5], 1 , [4, 4], [6, 6, 6], [3, 3], 6 ] 
The max run is: [6, 6, 6]                                                       
 4 6 3 3 6 6 5 3 4 5 5 1 4 4 ( 6 6 6 ) 3 3 6                                    
bash-4.1$ java One 6 20                                                         
[2, 6, 1, 2, 6, 3, 5, 1, 6, 3, 3, 2, 5, 2, 4, 1, 6, 2, 4, 4]                    
[2 , 6 , 1 , 2 , 6 , 3 , 5 , 1 , 6 , [3, 3], 2 , 5 , 2 , 4 , 1 , 6 , 2 , [4, 4]]
The max run is: [4, 4]                                                          
 2 6 1 2 6 3 5 1 6 3 3 2 5 2 4 1 6 2 ( 4 4 )                                    
bash-4.1$ java One 6 20                                                         
[2, 2, 6, 5, 4, 2, 5, 4, 4, 5, 1, 2, 6, 2, 1, 1, 3, 1, 1, 3]                    
[[2, 2], 6 , 5 , 4 , 2 , 5 , [4, 4], 5 , 1 , 2 , 6 , 2 , [1, 1], 3 , [1, 1], 3 ]
The max run is: [4, 4]                                                          
 2 2 6 5 4 2 5 ( 4 4 ) 5 1 2 6 2 1 1 3 1 1 3                                    
bash-4.1$ java One 6 20                                                         
[6, 6, 1, 6, 5, 3, 5, 6, 4, 3, 4, 2, 5, 1, 1, 4, 5, 1, 2, 2]                    
[[6, 6], 1 , 6 , 5 , 3 , 5 , 6 , 4 , 3 , 4 , 2 , 5 , [1, 1], 4 , 5 , 1 , [2, 2]]
The max run is: [6, 6]                                                          
 ( 6 6 ) 1 6 5 3 5 6 4 3 4 2 5 1 1 4 5 1 2 2                                    
bash-4.1$ java One 6 20                                                         
[3, 5, 5, 6, 1, 1, 5, 5, 4, 4, 4, 2, 4, 3, 5, 1, 4, 6, 2, 3]                    
[3 , [5, 5], 6 , [1, 1], [5, 5], [4, 4, 4], 2 , 4 , 3 , 5 , 1 , 4 , 6 , 2 , 3 ] 
The max run is: [4, 4, 4]                                                       
 3 5 5 6 1 1 5 5 ( 4 4 4 ) 2 4 3 5 1 4 6 2 3                                    
bash-4.1$ java One 6 20                                                         
[2, 6, 2, 6, 3, 1, 5, 4, 5, 2, 3, 4, 1, 1, 2, 2, 4, 3, 2, 3]                    
[2 , 6 , 2 , 6 , 3 , 1 , 5 , 4 , 5 , 2 , 3 , 4 , [1, 1], [2, 2], 4 , 3 , 2 , 3 ]
The max run is: [2, 2]                                                          
 2 6 2 6 3 1 5 4 5 2 3 4 1 1 ( 2 2 ) 4 3 2 3                                    
bash-4.1$ java One 6 20                                                         
[1, 5, 1, 3, 4, 4, 6, 4, 6, 5, 5, 5, 2, 2, 5, 6, 4, 3, 4, 2]                    
[1 , 5 , 1 , 3 , [4, 4], 6 , 4 , 6 , [5, 5, 5], [2, 2], 5 , 6 , 4 , 3 , 4 , 2 ] 
The max run is: [5, 5, 5]                                                       
 1 5 1 3 4 4 6 4 6 ( 5 5 5 ) 2 2 5 6 4 3 4 2                                    
bash-4.1$ java One 6 20                                                         
[2, 6, 5, 2, 4, 6, 2, 1, 4, 3, 6, 1, 3, 4, 2, 5, 2, 1, 1, 2]                    
[2 , 6 , 5 , 2 , 4 , 6 , 2 , 1 , 4 , 3 , 6 , 1 , 3 , 4 , 2 , 5 , 2 , [1, 1], 2 ]
The max run is: [1, 1]                                                          
 2 6 5 2 4 6 2 1 4 3 6 1 3 4 2 5 2 ( 1 1 ) 2                                    
bash-4.1$ java One 6 20                                                         
[3, 6, 4, 2, 4, 2, 6, 4, 3, 1, 2, 4, 6, 1, 6, 2, 4, 3, 5, 6]                    
[3 , 6 , 4 , 2 , 4 , 2 , 6 , 4 , 3 , 1 , 2 , 4 , 6 , 1 , 6 , 2 , 4 , 3 , 5 , 6 ]
The max run is: 6                                                               
 3 6 4 2 4 2 6 4 3 1 2 4 6 1 6 2 4 3 5 ( 6 )                                    
bash-4.1$ java One 6 20                                                         
[6, 4, 2, 1, 1, 4, 4, 4, 3, 6, 3, 2, 6, 5, 4, 2, 5, 2, 1, 1]                    
[6 , 4 , 2 , [1, 1], [4, 4, 4], 3 , 6 , 3 , 2 , 6 , 5 , 4 , 2 , 5 , 2 , [1, 1]] 
The max run is: [4, 4, 4]                                                       
 6 4 2 1 1 ( 4 4 4 ) 3 6 3 2 6 5 4 2 5 2 1 1                                    
bash-4.1$ java One 6 20                                                         
[6, 6, 1, 5, 5, 2, 2, 3, 2, 2, 6, 1, 5, 1, 2, 5, 4, 6, 2, 2]                    
[[6, 6], 1 , [5, 5], [2, 2], 3 , [2, 2], 6 , 1 , 5 , 1 , 2 , 5 , 4 , 6 , [2, 2]]
The max run is: [6, 6]                                                          
 ( 6 6 ) 1 5 5 2 2 3 2 2 6 1 5 1 2 5 4 6 2 2                                    
bash-4.1$ java One 6 20                                                         
[6, 6, 4, 5, 4, 2, 1, 1, 6, 4, 1, 1, 6, 5, 1, 1, 2, 1, 1, 1]                    
[[6, 6], 4 , 5 , 4 , 2 , [1, 1], 6 , 4 , [1, 1], 6 , 5 , [1, 1], 2 , [1, 1, 1]] 
The max run is: [1, 1, 1]                                                       
 6 6 4 5 4 2 1 1 6 4 1 1 6 5 1 1 2 ( 1 1 1 )                                    
bash-4.1$ java One 6 20                                                         
[4, 4, 1, 6, 5, 1, 3, 2, 3, 4, 3, 6, 5, 3, 4, 6, 2, 4, 5, 1]                    
[[4, 4], 1 , 6 , 5 , 1 , 3 , 2 , 3 , 4 , 3 , 6 , 5 , 3 , 4 , 6 , 2 , 4 , 5 , 1 ]
The max run is: [4, 4]                                                          
 ( 4 4 ) 1 6 5 1 3 2 3 4 3 6 5 3 4 6 2 4 5 1                                    
bash-4.1$ java One 6 20                                                         
[1, 6, 5, 2, 6, 4, 3, 5, 3, 4, 1, 4, 2, 2, 6, 2, 3, 1, 4, 2]                    
[1 , 6 , 5 , 2 , 6 , 4 , 3 , 5 , 3 , 4 , 1 , 4 , [2, 2], 6 , 2 , 3 , 1 , 4 , 2 ]
The max run is: [2, 2]                                                          
 1 6 5 2 6 4 3 5 3 4 1 4 ( 2 2 ) 6 2 3 1 4 2                                    
bash-4.1$ java One 6 20                                                         
[2, 2, 3, 1, 1, 6, 6, 5, 6, 1, 1, 6, 1, 6, 6, 1, 6, 5, 1, 4]                    
[[2, 2], 3 , [1, 1], [6, 6], 5 , 6 , [1, 1], 6 , 1 , [6, 6], 1 , 6 , 5 , 1 , 4 ]
The max run is: [6, 6]                                                          
 2 2 3 1 1 6 6 5 6 1 1 6 1 ( 6 6 ) 1 6 5 1 4                                    
bash-4.1$ cat One.java                                                          
import java.util.*;                                                             

class One {

  public static void main(String[] args) {
    int range = Integer.parseInt(args[0]),
        number = Integer.parseInt(args[1]); 
    ArrayList<Integer> numbers = One.generate(range, number); 
    System.out.println( numbers );                            
    Inspector gadget = new Inspector( numbers );              
    ArrayList<Run> answer = gadget.go();                      
    System.out.println( answer );                             

    Run max = gadget.findMax(answer); 
    System.out.println( "The max run is: " + max ); 

    One.print(answer, max); 

  }

  public static void print(ArrayList<Run> a, Run b) {
    for (Run run : a)                                
      if (run == b)                                  
        System.out.print( " (" + run.show() + " )" ); 
      else                                            
        System.out.print( run.show() );               
      System.out.println();                           
  }                                                   

  public static ArrayList<Integer> generate(int range, int number) {
    ArrayList<Integer> result = new ArrayList<Integer>();           
    for (int i = 0; i < number; i++) {                              
      int value = (int) (Math.random() * range) + 1;                
      result.add(value);                                            
    }                                                               
    return result;                                                  
  }                                                                 
}                                                                   

class Run extends ArrayList<Integer> implements Comparable<Run> {

  public String show() {
    String result = " "; 
    for (Integer i : this) 
      result = result + i + " ";       
    return result.substring(0, result.length() - 1);
  }

  public int compareTo(Run other) {
    if (this.size() > other.size()) {
      return -1;
    } else {
      if (other.size() > this.size()) {
        return 1;
      } else {
        return - this.get(0).compareTo(other.get(0));
      }
    }
  }

  public String toString() {
    if (this.size() > 1) {
      return super.toString();
    } else {
      String result = "";
      for (Integer num : this)
        result += num + " ";
      return result;
    }
  }
}

class Inspector {
  ArrayList<Integer> numbers;
  Inspector(ArrayList<Integer> numbers) {
    this.numbers = numbers;
  }
  Run row = new Run();

  ArrayList<Run> go() {
    ArrayList<Run> result = new ArrayList<Run>();
    while (this.hasRun()) {
      result.add(this.getRun());
    }
    return result;
  }

  Run findMax(ArrayList<Run> runs) {
    Run max = new Run();
    for (Run run : runs)
      max = max.compareTo(run) < 0 ? max : run;
    return max;
  }

  boolean hasRun() {
    return (this.row.size() == 0 && this.numbers.size() != 0);
  }

  Run getRun() {
    Run row = new Run();
    row.add(this.numbers.remove(0));
    while (this.numbers.size() > 0 && row.get(0) == this.numbers.get(0)) {
      row.add( this.numbers.remove(0) );
    }
    return row;
  }
}

bash-4.1$

--