Skip to content

Tech review ch02 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CCSPiJ/src/chapter2/Gene.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public enum Nucleotide {

public static class Codon implements Comparable<Codon> {
public final Nucleotide first, second, third;
private final Comparator<Codon> comparator = Comparator.comparing((Codon c) -> c.first)
.thenComparing((Codon c) -> c.second)
.thenComparing((Codon c) -> c.third);

public Codon(String codonStr) {
first = Enum.valueOf(Nucleotide.class, codonStr.substring(0, 1));
second = Enum.valueOf(Nucleotide.class, codonStr.substring(1, 2));
third = Enum.valueOf(Nucleotide.class, codonStr.substring(2, 3));
first = Nucleotide.valueOf(codonStr.substring(0, 1));
second = Nucleotide.valueOf(codonStr.substring(1, 2));
third = Nucleotide.valueOf(codonStr.substring(2, 3));
}

@Override
public int compareTo(Codon other) {
// first is compared first, then second, etc.
// IOW first takes precedence over second and second over third
return Comparator.comparing((Codon c) -> c.first)
.thenComparing((Codon c) -> c.second)
.thenComparing((Codon c) -> c.third)
.compare(this, other);
return comparator.compare(this, other);
}
}

Expand Down
13 changes: 7 additions & 6 deletions CCSPiJ/src/chapter2/GenericSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
Expand All @@ -31,7 +32,7 @@

public class GenericSearch {

public static <T extends Comparable<T>> boolean linearContains(List<T> list, T key) {
public static <T extends Comparable<? super T>> boolean linearContains(List<T> list, T key) {
for (T item : list) {
if (item.compareTo(key) == 0) {
return true; // found a match
Expand All @@ -41,7 +42,7 @@ public static <T extends Comparable<T>> boolean linearContains(List<T> list, T k
}

// assumes *list* is already sorted
public static <T extends Comparable<T>> boolean binaryContains(List<T> list, T key) {
public static <T extends Comparable<? super T>> boolean binaryContains(List<T> list, T key) {
int low = 0;
int high = list.size() - 1;
while (low <= high) { // while there is still a search space
Expand Down Expand Up @@ -161,7 +162,7 @@ public static <T> Node<T> astar(T initial, Predicate<T> goalTest,
PriorityQueue<Node<T>> frontier = new PriorityQueue<>();
frontier.offer(new Node<>(initial, null, 0.0, heuristic.applyAsDouble(initial)));
// explored is where we've been
HashMap<T, Double> explored = new HashMap<>();
Map<T, Double> explored = new HashMap<>();
explored.put(initial, 0.0);
// keep going while there is more to explore
while (!frontier.isEmpty()) {
Expand All @@ -186,9 +187,9 @@ public static <T> Node<T> astar(T initial, Predicate<T> goalTest,
}

public static void main(String[] args) {
System.out.println(GenericSearch.linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true
System.out.println(GenericSearch.binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true
System.out.println(GenericSearch.binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false
System.out.println(linearContains(List.of(1, 5, 15, 15, 15, 15, 20), 5)); // true
System.out.println(binaryContains(List.of("a", "d", "e", "f", "z"), "f")); // true
System.out.println(binaryContains(List.of("john", "mark", "ronald", "sarah"), "sheila")); // false
}

}
16 changes: 7 additions & 9 deletions CCSPiJ/src/chapter2/MCState.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

public class MCState {
private static final int MAX_NUM = 3;
final private int wm; // west bank missionaries
final private int wc; // west bank cannibals
final private int em; // east bank missionaries
final private int ec; // east bank cannibals
final private boolean boat; // is boat on west bank?
private final int wm; // west bank missionaries
private final int wc; // west bank cannibals
private final int em; // east bank missionaries
private final int ec; // east bank cannibals
private final boolean boat; // is boat on west bank?

public MCState(int missionaries, int cannibals, boolean boat) {
wm = missionaries;
Expand Down Expand Up @@ -109,13 +109,11 @@ public static void displaySolution(List<MCState> path) {
System.out.println(oldState);
for (MCState currentState : path.subList(1, path.size())) {
if (currentState.boat) {
System.out.printf("%d missionaries and %d cannibals moved from the east bank to the west bank." +
System.lineSeparator(),
System.out.printf("%d missionaries and %d cannibals moved from the east bank to the west bank.%n",
oldState.em - currentState.em,
oldState.ec - currentState.ec);
} else {
System.out.printf("%d missionaries and %d cannibals moved from the west bank to the east bank." +
System.lineSeparator(),
System.out.printf("%d missionaries and %d cannibals moved from the west bank to the east bank.%n",
oldState.wm - currentState.wm,
oldState.wc - currentState.wc);
}
Expand Down
28 changes: 13 additions & 15 deletions CCSPiJ/src/chapter2/Maze.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,21 @@
public class Maze {

public enum Cell {
EMPTY, BLOCKED, START, GOAL, PATH;
EMPTY(" "),
BLOCKED("X"),
START("S"),
GOAL("G"),
PATH("*");

private final String code;

private Cell(String c) {
code = c;
}

@Override
public String toString() {
switch (this) {
case EMPTY:
return (" ");
case BLOCKED:
return ("X");
case START:
return ("S");
case GOAL:
return ("G");
case PATH:
return ("*");
}
return null; // should never get here
return code;
}
}

Expand Down Expand Up @@ -129,7 +127,7 @@ public String toString() {
StringBuilder sb = new StringBuilder();
for (Cell[] row : grid) {
for (Cell cell : row) {
sb.append(cell.toString());
sb.append(cell);
}
sb.append(System.lineSeparator());
}
Expand Down
2 changes: 0 additions & 2 deletions CCSPiJ/src/module-info.java

This file was deleted.