Re: Distance traveled (not traveling sales man)
Jeff Higgins wrote:
Gee whiz, no code review? I'm devastated. :)
Oh, c'mon, you know your code is good! But since you ask:
import java.util.ArrayList;
import java.util.List;
public class Scratch {
Personally I prefer to put the opening brace on its own line indented to the
method signature, but your way is The Standard.
public static void main(String[] args) {
Personally I prefer to put more white space in, such as inside method
parentheses, but that's just a personal style matter and no reflection on your
code.
int[] data = new int[100];
for (int i = 0; i < 100; i++) {
data[i] = 1 + (int)(Math.random() * 50); }
Now putting the closing brace there does violate The Standard, and mars
readability.
List<List<Integer>> bins =
new ArrayList<List<Integer>>();
int count = 0;
List<Integer> currentBin;
Excellent variable name choices.
while ( count < 100 ) {
double sub = 0;
I prefer to use double constants to initialize double variables.
currentBin = new ArrayList<Integer>();
bins.add(currentBin);
A blank line here could aid readability.
while (sub <= 100 && count < 100) {
if (sub + data[count] <= 100) {
sub += data[count];
currentBin.add(data[count]);
count++;
} else break;
Oooh, I don't like the lack of braces around the 'else' statement, nor the way
you conflated all those lines into one. The code would be more maintainable
if you followed The Standard on these.
}
}
for (List<Integer> l : bins) {
'l' is a very unfortunate choice for a variable name.
System.out.println(l.toString());
}
}
}
Nice clean algorithm.
--
Lew