前回作成した連結リストを使った簡単な問題集です。
Pair は組を表す immutable なクラスです。パッケージ immutable に追加して使ってください。
//
// Pair.java : immutable な Pair
//
// Copyright (C) 2016-2026 Makoto Hiroi
//
package immutable;
public final class Pair<T, U> {
final private T fst;
final private U snd;
private Pair(T a, U b) {
fst = a;
snd = b;
}
public static <T, U> Pair<T, U> pair(T a, U b) {
return new Pair<T, U>(a, b);
}
public T getFst() { return fst; }
public U getSnd() { return snd; }
public String toString() {
return "(" + fst.toString() + ", " + snd.toString() + ")";
}
}
リスト : 「リストで遊ぼう」の解答 (listproblem.java)
import java.util.Optional;
import java.util.function.Predicate;
import immutable.ImList;
import immutable.Pair;
import static immutable.ImList.*;
// Q01
<E> boolean longer(ImList<E> xs, ImList<E> ys) {
while (!xs.isEmpty() && !ys.isEmpty()) {
xs = xs.rest();
ys = ys.rest();
}
return !xs.isEmpty();
}
// Q02
<E> ImList<E> butlast(ImList<E> xs, int n) {
return xs.reverse().drop(n).reverse();
}
// Q03
<E> ImList<ImList<E>> group(ImList<E> xs, int n) {
if (xs.isEmpty())
return nil();
else
return cons(xs.take(n), group(xs.drop(n), n));
}
// Q04
<E> ImList<E> subList(ImList<E> xs, int n, int m) {
return xs.drop(n).take(m - n);
}
// Q05
<K, V> ImList<Pair<K, V>> zip(ImList<K> xs, ImList<V> ys) {
return zipWith(Pair::pair, xs, ys);
}
// Q06
<K, V> Pair<ImList<K>, ImList<V>> unzip(ImList<Pair<K, V>> xs) {
if (xs.isEmpty()) {
return Pair.pair(nil(), nil());
} else {
var ys = unzip(xs.rest());
return Pair.pair(cons(xs.first().getFst(), ys.getFst()),
cons(xs.first().getSnd(), ys.getSnd()));
}
}
// Q07
<K, V> Optional<V> assoc(ImList<Pair<K, V>> xs, K key) {
return xs.findIf(p -> p.getFst() == key).map(p -> p.getSnd());
}
// Q08
<E> Pair<ImList<E>, ImList<E>> partition(Predicate<? super E> pred, ImList<E> xs) {
ImList<E> a = nil();
ImList<E> b = nil();
for (E x: xs) {
if (pred.test(x))
a = cons(x, a);
else
b = cons(x, b);
}
return Pair.pair(a.reverse(), b.reverse());
}
// Q09
<E extends Comparable<E>> ImList<E> quickSort(ImList<E> xs) {
if (xs.isEmpty()) return xs;
E pivot = xs.first();
var a = partition(x -> pivot.compareTo(x) > 0, xs.rest());
var ys = quickSort(a.getFst());
var zs = quickSort(a.getSnd());
return ys.append(cons(pivot, zs));
}
// Q10
<E extends Comparable<E>> ImList<E> mergeList(ImList<E> xs, ImList<E> ys) {
if (xs.isEmpty()) {
return ys;
} else if (ys.isEmpty()) {
return xs;
} else {
if (xs.first().compareTo(ys.first()) <= 0)
return cons(xs.first(), mergeList(xs.rest(), ys));
else
return cons(ys.first(), mergeList(xs, ys.rest()));
}
}
// Q11
<E extends Comparable<E>> ImList<E> mergeSortSub(ImList<E> xs, int n) {
if (n == 0) {
return nil();
} else if (n == 1) {
return of(xs.first());
} else {
int m = n / 2;
ImList<E> ys = mergeSortSub(xs, m);
ImList<E> zs = mergeSortSub(xs.drop(m), n - m);
return mergeList(ys, zs);
}
}
<E extends Comparable<E>> ImList<E> mergeSort(ImList<E> xs) {
return mergeSortSub(xs, xs.length());
}
// Q12
<E> ImList<ImList<E>> permutations(int n, ImList<E> xs) {
if (n == 0)
return of(nil());
else
return xs.flatMap(x -> permutations(n - 1, xs.filter(y -> !x.equals(y))).map(z -> cons(x, z)));
}
// Q13
<E> ImList<ImList<E>> combinations(int n, ImList<E> xs) {
if (n == 0) {
return of(nil());
} else if (n == xs.length()) {
return of(xs);
} else {
return combinations(n - 1, xs.rest())
.map(ys -> cons(xs.first(), ys))
.append(combinations(n, xs.rest()));
}
}
// Q14
<E> ImList<ImList<E>> powerSet(ImList<E> xs) {
if (xs.isEmpty()) {
return of(nil());
} else {
var ys = powerSet(xs.rest());
return ys.append(ys.map(zs -> cons(xs.first(), zs)));
}
}
// Q15
<E> ImList<E> revAppend(ImList<E> xs, ImList<E> ys) {
for (E x: xs) ys = cons(x, ys);
return ys;
}
ImList<Integer> sieve(int n) {
var xs = iota(2, n);
ImList<Integer> ys = nil();
while (true) {
Integer p = xs.first();
if (p * p > n) break;
ys = cons(p, ys);
xs = xs.rest().filter(x -> x % p != 0);
}
return revAppend(ys, xs);
}
// Q16
class Query {
ImList<Integer> code;
int bulls;
int cows;
Query(ImList<Integer> xs, int b, int c) {
code = xs;
bulls = b;
cows = c;
}
}
int countBulls(ImList<Integer> xs, ImList<Integer> ys) {
return zip(xs, ys).foldLeft((a, p) -> p.getFst() == p.getSnd() ? a + 1 : a, 0);
}
int countSameNumber(ImList<Integer> xs, ImList<Integer> ys) {
return xs.foldLeft((a, x) -> ys.contains(x) ? a + 1 : a, 0);
}
boolean checkQuery(ImList<Query> qs, ImList<Integer> xs) {
return qs.allMatch(q -> {
int b = countBulls(q.code, xs);
int c = countSameNumber(q.code, xs) - b;
return b == q.bulls && c == q.cows;
});
}
void masterMind(ImList<Integer> answer) {
var codeTable = permutations(4, iota(0, 9));
ImList<Query> qs = nil();
int n = 1;
for (ImList<Integer> code: codeTable) {
if (checkQuery(qs, code)) {
int bulls = countBulls(answer, code);
int cows = countSameNumber(answer, code) - bulls;
qs = cons(new Query(code, bulls, cows), qs);
System.out.printf("%d: %s, bulls = %d, cows = %d\n", n++, code, bulls, cows);
if (bulls == 4) {
IO.println("Good Job!!");
break;
}
}
}
}
void main() {
var xs = iota(1, 8);
var ys = iota(1, 9);
IO.println("----- Q01 -----");
IO.println(longer(ys, xs));
IO.println(longer(xs, ys));
IO.println(longer(xs, xs));
IO.println("----- Q02 -----");
IO.println(butlast(xs, 0));
IO.println(butlast(xs, 1));
IO.println(butlast(xs, 7));
IO.println(butlast(xs, 8));
IO.println("----- Q03 -----");
IO.println(group(xs, 2));
IO.println(group(ys, 3));
IO.println(group(xs, 4));
IO.println(group(ys, 5));
IO.println("----- Q04 -----");
IO.println(subList(xs, 2, 5));
IO.println(subList(xs, 0, 8));
IO.println("----- Q05 -----");
var nList1 = zip(iota(1, 5), iota(11, 15));
var nList2 = zip(iota(1, 6), iota(11, 15));
var nList3 = zip(iota(1, 5), iota(11, 16));
IO.println(nList1);
IO.println(nList2);
IO.println(nList3);
IO.println("----- Q06 -----");
IO.println(unzip(nList1));
IO.println("----- Q07 -----");
var aList = of(
Pair.pair("foo", 10),
Pair.pair("bar", 20),
Pair.pair("baz", 30),
Pair.pair("oops", 40));
IO.println(assoc(aList, "foo"));
IO.println(assoc(aList, "oops"));
IO.println(assoc(aList, "FOO"));
IO.println("----- Q08 -----");
IO.println(partition(x -> x % 2 == 0, xs));
IO.println(partition(x -> x % 2 != 0, ys));
IO.println("----- Q09 -----");
var zs = of(5,6,4,7,3,8,2,9,1,0);
IO.println(quickSort(zs));
IO.println(quickSort(ys));
IO.println(quickSort(ys.reverse()));
IO.println("----- Q10 -----");
var a = of(1,3,5,7,9);
var b = of(2,4,6,8,10);
IO.println(mergeList(a, b));
IO.println(mergeList(b, a));
IO.println(mergeList(a, a));
IO.println("----- Q11 -----");
IO.println(mergeSort(zs));
IO.println(mergeSort(ys));
IO.println(mergeSort(ys.reverse()));
IO.println("----- Q12 -----");
IO.println(permutations(3, iota(1, 3)));
IO.println(permutations(4, iota(1, 4)));
IO.println("----- Q13 -----");
IO.println(combinations(3, iota(1, 5)));
IO.println(combinations(4, iota(1, 5)));
IO.println("----- Q14 -----");
IO.println(powerSet(iota(1, 3)));
IO.println(powerSet(iota(1, 4)));
IO.println("----- Q15 -----");
IO.println(sieve(100));
IO.println(sieve(500));
IO.println(sieve(1000));
IO.println("----- Q16 -----");
masterMind(of(9,8,7,6));
masterMind(of(9,4,3,1));
}
$ java listproblem.java ----- Q01 ----- true false false ----- Q02 ----- (1 2 3 4 5 6 7 8) (1 2 3 4 5 6 7) (1) () ----- Q03 ----- ((1 2) (3 4) (5 6) (7 8)) ((1 2 3) (4 5 6) (7 8 9)) ((1 2 3 4) (5 6 7 8)) ((1 2 3 4 5) (6 7 8 9)) ----- Q04 ----- (3 4 5) (1 2 3 4 5 6 7 8) ----- Q05 ----- ((1, 11) (2, 12) (3, 13) (4, 14) (5, 15)) ((1, 11) (2, 12) (3, 13) (4, 14) (5, 15)) ((1, 11) (2, 12) (3, 13) (4, 14) (5, 15)) ----- Q06 ----- ((1 2 3 4 5), (11 12 13 14 15)) ----- Q07 ----- Optional[10] Optional[40] Optional.empty ----- Q08 ----- ((2 4 6 8), (1 3 5 7)) ((1 3 5 7 9), (2 4 6 8)) ----- Q09 ----- (0 1 2 3 4 5 6 7 8 9) (1 2 3 4 5 6 7 8 9) (1 2 3 4 5 6 7 8 9) ----- Q10 ----- (1 2 3 4 5 6 7 8 9 10) (1 2 3 4 5 6 7 8 9 10) (1 1 3 3 5 5 7 7 9 9) ----- Q11 ----- (0 1 2 3 4 5 6 7 8 9) (1 2 3 4 5 6 7 8 9) (1 2 3 4 5 6 7 8 9) ----- Q12 ----- ((1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1)) ((1 2 3 4) (1 2 4 3) (1 3 2 4) (1 3 4 2) (1 4 2 3) (1 4 3 2) (2 1 3 4) (2 1 4 3) (2 3 1 4) (2 3 4 1) (2 4 1 3) (2 4 3 1) (3 1 2 4) (3 1 4 2) (3 2 1 4) (3 2 4 1) (3 4 1 2) (3 4 2 1) (4 1 2 3) (4 1 3 2) (4 2 1 3) (4 2 3 1) (4 3 1 2) (4 3 2 1)) ----- Q13 ----- ((1 2 3) (1 2 4) (1 2 5) (1 3 4) (1 3 5) (1 4 5) (2 3 4) (2 3 5) (2 4 5) (3 4 5)) ((1 2 3 4) (1 2 3 5) (1 2 4 5) (1 3 4 5) (2 3 4 5)) ----- Q14 ----- (() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) (() (4) (3) (3 4) (2) (2 4) (2 3) (2 3 4) (1) (1 4) (1 3) (1 3 4) (1 2) (1 2 4) (1 2 3) (1 2 3 4)) ----- Q15 ----- (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97) (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499) (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997) ----- Q16 ----- 1: (0 1 2 3), bulls = 0, cows = 0 2: (4 5 6 7), bulls = 0, cows = 2 3: (5 4 8 9), bulls = 0, cows = 2 4: (6 7 9 8), bulls = 0, cows = 4 5: (8 9 7 6), bulls = 2, cows = 2 6: (9 8 7 6), bulls = 4, cows = 0 Good Job!! 1: (0 1 2 3), bulls = 0, cows = 2 2: (1 0 4 5), bulls = 0, cows = 2 3: (2 3 5 4), bulls = 0, cows = 2 4: (3 4 0 6), bulls = 1, cows = 1 5: (3 5 6 1), bulls = 1, cows = 1 6: (6 5 0 2), bulls = 0, cows = 0 7: (7 4 3 1), bulls = 3, cows = 0 8: (8 4 3 1), bulls = 3, cows = 0 9: (9 4 3 1), bulls = 4, cows = 0 Good Job!!