45 lines
2.9 KiB
Bash
Executable File
45 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Builds one self-contained DynamicProgrammingTenProblems.java from the ten problem classes, so it runs with
|
|
# java DynamicProgrammingTenProblems.java
|
|
# No Maven needed. Output goes to ./DynamicProgrammingTenProblems.java (not committed).
|
|
set -euo pipefail
|
|
cd "$(dirname "$0")/.."
|
|
SRC=src/main/java/com/ankurm/interviewlab/dp
|
|
OUT=${1:-DynamicProgrammingTenProblems.java}
|
|
python3 - "$SRC" "$OUT" <<'PY'
|
|
import re, sys, pathlib
|
|
src, out = pathlib.Path(sys.argv[1]), sys.argv[2]
|
|
order = ["Fibonacci","ClimbingStairs","HouseRobber","CoinChange","LongestIncreasingSubsequence",
|
|
"Knapsack01","Lcs","EditDistance","UniquePaths","PartitionEqualSubsetSum"]
|
|
imports, bodies = set(), []
|
|
for name in order:
|
|
text = (src / f"{name}.java").read_text()
|
|
for m in re.finditer(r"^import [^;]+;", text, re.M): imports.add(m.group(0))
|
|
text = re.sub(r"^package [^;]+;\s*", "", text, flags=re.M)
|
|
text = re.sub(r"^import [^;]+;\s*", "", text, flags=re.M)
|
|
text = text.replace("public final class", "final class", 1)
|
|
bodies.append(text.strip())
|
|
main = '''
|
|
/** Prints a one-line answer for each of the ten problems. Run: java DynamicProgrammingTenProblems.java */
|
|
public class DynamicProgrammingTenProblems {
|
|
public static void main(String[] args) {
|
|
long[] calls = {0};
|
|
System.out.println("1 Fibonacci(40) naive = " + Fibonacci.naive(40, calls) + " (" + calls[0] + " calls)");
|
|
calls[0] = 0;
|
|
System.out.println(" memo = " + Fibonacci.memo(40, calls) + " (" + calls[0] + " calls)");
|
|
System.out.println("2 Climbing stairs(10) " + ClimbingStairs.tabulation(10));
|
|
System.out.println("3 House robber [2,7,9,3,1] " + HouseRobber.tabulation(new int[] {2, 7, 9, 3, 1}));
|
|
System.out.println("4 Coin change {1,3,4}, 6 " + CoinChange.tabulation(new int[] {1, 3, 4}, 6) + " coins (greedy says " + CoinChange.greedy(new int[] {1, 3, 4}, 6) + ")");
|
|
System.out.println("5 LIS [10,9,2,5,3,7,101,18] " + LongestIncreasingSubsequence.tabulation(new int[] {10, 9, 2, 5, 3, 7, 101, 18}));
|
|
System.out.println("6 Knapsack 0/1 " + Knapsack01.oneRowBackward(new int[] {5, 4, 6, 3}, new int[] {10, 40, 30, 50}, 10));
|
|
System.out.println("7 LCS ABCBDAB / BDCABA " + Lcs.tabulation("ABCBDAB", "BDCABA"));
|
|
System.out.println("8 Edit distance kitten/sitting " + EditDistance.tabulation("kitten", "sitting"));
|
|
System.out.println("9 Unique paths 3 x 7 " + UniquePaths.tabulation(3, 7));
|
|
System.out.println("10 Partition [1,5,11,5] " + PartitionEqualSubsetSum.tabulation(new int[] {1, 5, 11, 5}));
|
|
}
|
|
}
|
|
'''
|
|
header = "// Generated by scripts/make-single-file.sh from the java-interview-lab/dynamic-programming module.\n// Run with: java DynamicProgrammingTenProblems.java (JDK 25)\n"
|
|
pathlib.Path(out).write_text(header + "\n".join(sorted(imports)) + "\n" + main + "\n" + "\n\n".join(bodies) + "\n")
|
|
PY
|