Skip to content

Instantly share code, notes, and snippets.

@coderodde
Last active August 19, 2021 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderodde/648e30adabe1c3da5031bec2df3aa783 to your computer and use it in GitHub Desktop.
Save coderodde/648e30adabe1c3da5031bec2df3aa783 to your computer and use it in GitHub Desktop.
Comparing the CR plate number iterators
import java.util.Iterator;
import java.util.NoSuchElementException;
public final class PlateNumberIterable implements Iterable<String> {
@Override
public Iterator<String> iterator() {
return new PlateNumberIterator();
}
private static final class PlateNumberIterator implements Iterator<String> {
private static final int NUMBER_OF_ALL_CANDIDATE_PLATE_NUMBERS =
26 * 26 * 26 * 10_000;
private static final int NUMBER_OF_ALL_SKIPPED_PLATE_NUMBERS =
26 * 26 * 26;
private static final int NUMBER_OF_TOTAL_LEGAL_PLATE_NUMBERS =
NUMBER_OF_ALL_CANDIDATE_PLATE_NUMBERS -
NUMBER_OF_ALL_SKIPPED_PLATE_NUMBERS;
private int numberOfIteratedPlates = 0;
private final char[] chars =
new char[]{ 'A', 'A', 'A', '0', '0', '0', '1' };
@Override
public boolean hasNext() {
return numberOfIteratedPlates < NUMBER_OF_TOTAL_LEGAL_PLATE_NUMBERS;
}
@Override
public String next() {
if (!hasNext()) {
throw new NoSuchElementException("Iterator exhausted.");
}
numberOfIteratedPlates++;
String result = new String(chars);
incrementPlateNumber();
return result;
}
private void incrementPlateNumber() {
for (int i = 6; i >= 3; i--) {
if (chars[i] < '9') {
chars[i]++;
for (int j = i + 1; j < 7; j++) {
chars[j] = '0';
}
return;
}
}
chars[3] =
chars[4] =
chars[5] = '0';
chars[6] = '1';
for (int i = 2; i >= 0; i--) {
if (chars[i] < 'Z') {
chars[i]++;
for (int j = i + 1; j < 3; j++) {
chars[j] = 'A';
}
return;
}
}
}
}
public static void main(String[] args) {
int coderoddePlates = 0;
long start = System.currentTimeMillis();
for (String plateNumber : new PlateNumberIterable()) {
coderoddePlates++;
}
long end = System.currentTimeMillis();
System.out.println("coderodde iterable computation time: " +
(end - start) + " ms.");
int AJNeufeldPlates = 0;
start = System.currentTimeMillis();
for (String plateNumber : new Plates("AAA0001")) {
AJNeufeldPlates++;
}
end = System.currentTimeMillis();
System.out.println("AJNeufeld iterable computation time: " +
(end - start) + " ms.");
System.out.println("coderodde iterable produced " + coderoddePlates +
" plates.");
System.out.println("AJNeufeld iterable produced " + AJNeufeldPlates +
" plates.");
}
}
import java.util.Iterator;
public class Plates implements Iterable<String> {
private final String initial_plate;
public Plates(String first_plate) {
initial_plate = first_plate;
}
@Override
public Iterator<String> iterator() {
return new PlateIterator(initial_plate);
}
private static class PlateIterator implements Iterator<String> {
private final int plate_length;
private final StringBuilder plate;
private boolean has_next = true;
private boolean need_next = false;
PlateIterator(String first_plate) {
plate_length = first_plate.length();
plate = new StringBuilder(plate_length);
plate.append(first_plate);
has_next = true;
need_next = false;
}
private void advance() {
need_next = false;
has_next = false;
for (int i = plate_length - 1; i >= 0; i--) {
char ch = plate.charAt(i);
if (ch == '9')
plate.setCharAt(i, '0');
else if (ch == 'Z')
plate.setCharAt(i, 'A');
else {
plate.setCharAt(i, (char) (ch + 1));
has_next = true;
break;
}
}
if (plate.substring(plate_length - 4).equals("0000"))
plate.setCharAt(plate_length - 1, '1');
}
@Override
public boolean hasNext() {
if (has_next && need_next)
advance();
return has_next;
}
@Override
public String next() {
if (need_next)
advance();
need_next = true;
return plate.toString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment