// decrease this players cash by amount // return if you owe some resting standing (you didn't pay fully) public Long giveMoney(Long amount) { if (this.cash > amount) { this.cash -= amount; StdOut.println( this.name + " paid " + Bank.moneyEuro(amount) + " (current cash " + Bank.moneyEuro(this.cash) + ")"); return 0L; } else { Long owed = amount - this.cash; StdOut.println( this.name + " paid " + Bank.moneyEuro(this.cash) + " (owed " + Bank.moneyEuro(owed) + ")"); this.cash = 0L; return owed; } }
public static void main(String[] args) { String filename = args[0]; String delimiter = args[1]; String source = args[2]; StdOut.println("Zrodlo: " + source); SymbolGraph sg = new SymbolGraph(filename, delimiter); Graph G = sg.G(); if (!sg.contains(source)) { StdOut.println(source + " nie wystepuje w bazie danych."); return; } int s = sg.index(source); BreadthFirstPaths bfs = new BreadthFirstPaths(G, s); while (!StdIn.isEmpty()) { String sink = StdIn.readLine(); if (sg.contains(sink)) { int t = sg.index(sink); if (bfs.hasPathTo(t)) { for (int v : bfs.pathTo(t)) { StdOut.println(" " + sg.name(v)); } } else { StdOut.println("Brak polaczenia"); } } else { StdOut.println(" Nie wystepuje w bazie danych."); } } }
/** Unit tests the <tt>com.akieus.algos.coursera.lib.TopologicalX</tt> data type. */ public static void main(String[] args) { // create random DAG with V vertices and E edges; then add F random edges int V = Integer.parseInt(args[0]); int E = Integer.parseInt(args[1]); int F = Integer.parseInt(args[2]); Digraph G = DigraphGenerator.dag(V, E); // add F extra edges for (int i = 0; i < F; i++) { int v = StdRandom.uniform(V); int w = StdRandom.uniform(V); G.addEdge(v, w); } StdOut.println(G); // find a directed cycle TopologicalX topological = new TopologicalX(G); if (!topological.hasOrder()) { StdOut.println("Not a DAG"); } // or give topologial sort else { StdOut.print("com.akieus.algos.coursera.lib.Topological order: "); for (int v : topological.order()) { StdOut.print(v + " "); } StdOut.println(); } }
public static void main(String[] args) { In in = new In("jobsPC1.txt"); int N = in.readInt(); in.readLine(); EdgeWeightedDigraph G; G = new EdgeWeightedDigraph(2 * N + 2); int s = 2 * N, t = 2 * N + 1; for (int i = 0; i < N; ++i) { String[] a = in.readLine().split("\\s+"); double duration = Double.parseDouble(a[0]); G.addEdge(new DirectedEdge(i, i + N, duration)); G.addEdge(new DirectedEdge(s, i, 0.0)); G.addEdge(new DirectedEdge(i + N, t, 0.0)); for (int j = 1; j < a.length; ++j) { G.addEdge(new DirectedEdge(i + N, Integer.parseInt(a[j]), 0)); } } StdOut.println(G); MyAcyclicLP lp = new MyAcyclicLP(G, s); StdOut.println("Start times: "); for (int i = 0; i < N; ++i) StdOut.printf("%4d: %5.1f\n", i, lp.distTo(i)); StdOut.printf("%5.1f\n", lp.distTo(t)); for (DirectedEdge e : lp.pathTo(t)) { StdOut.print(e + " "); } StdOut.println(); }
/** Unit tests the <tt>GabowSCC</tt> data type. */ public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); GabowSCC scc = new GabowSCC(G); // number of connected components int M = scc.count(); StdOut.println(M + " components"); // compute list of vertices in each strong component Queue<Integer>[] components = (Queue<Integer>[]) new Queue[M]; for (int i = 0; i < M; i++) { components[i] = new Queue<Integer>(); } for (int v = 0; v < G.V(); v++) { components[scc.id(v)].enqueue(v); } // print results for (int i = 0; i < M; i++) { for (int v : components[i]) { StdOut.print(v + " "); } StdOut.println(); } }
public void payToPlayer(Player payee, Long amount) { // if calling giveMoney will be anything but not 0 then all belongings go to payee Long owed = this.giveMoney(amount); payee.getMoney(amount - owed); if (owed > 0) { StdOut.println(this.name + " owes to " + payee.name + " " + Bank.moneyEuro(owed)); if (this == Game.getCurrentPlayer()) { // the moment player gave up it will stop recursing if (this.playing) { // allows player to sell something and then calls itself, // so it won't leave till it's paid ot player gave up this.sellProperty(); this.payToPlayer(payee, owed); } } else { for (BoardTile tile : BoardSearch.all().filterByOwner(this).getTiles()) { tile.changeOwner(payee); } // give payee money you got from selling of upgrades StdOut.println( payee + " got " + Bank.moneyEuro(this.cash) + " from " + this.name + "'s upgrades."); this.giveMoney(this.cash); this.cash = 0L; } } }
public static void main(String[] args) { // key = word, value = set of files containing that word ST<String, SET<File>> st = new ST<String, SET<File>>(); // create inverted index of all files StdOut.println("Indexing files"); for (String filename : args) { StdOut.println(" " + filename); File file = new File(filename); In in = new In(file); while (!in.isEmpty()) { String word = in.readString(); if (!st.contains(word)) st.put(word, new SET<File>()); SET<File> set = st.get(word); set.add(file); } } // read queries from standard input, one per line while (!StdIn.isEmpty()) { String query = StdIn.readString(); if (st.contains(query)) { SET<File> set = st.get(query); for (File file : set) { StdOut.println(" " + file.getName()); } } } }
// test client, optional public static void main(String[] args) { StdOut.println("start testing.."); int N = 20; Percolation p = new Percolation(N); int openCounter = 0; while (true) { int i = (int) (Math.random() * N + 1); int j = (int) (Math.random() * N + 1); if (!p.isOpen(i - 1, j - 1)) { openCounter++; StdOut.println("open: " + i + ", " + j); p.open(i, j); if (p.percolates()) { break; } } } StdOut.println("open counter: " + openCounter); double threshold = openCounter / (double) (N * N); StdOut.println(threshold); }
public static void main(String[] args) { // create initial board from file In in = new In(args[0]); int N = in.readInt(); int[][] tiles = new int[N][N]; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { tiles[i][j] = in.readInt(); } Board initial = new Board(tiles); /* System.out.println(initial.isSolvable()); System.out.println(initial.toString()); Iterable<Board> newbrdstack = new Stack<Board>(); newbrdstack = initial.neighbors(); System.out.println(newbrdstack.toString()); */ // check if puzzle is solvable; if so, solve it and output solution if (initial.isSolvable()) { Solver solver = new Solver(initial); StdOut.println("Minimum number of moves = " + solver.moves()); for (Board board : solver.solution()) StdOut.println(board); } // if not, report unsolvable else { StdOut.println("Unsolvable puzzle"); } }
private int displayDebugMenu() { ArrayList<String> items = new ArrayList<String>( Arrays.asList( "Exit debugging", ((Game.getDebugDice()) ? "Disable" : "Enable") + " dice debugging", "Jump to tile #", "Execute current tile action", "Set your cash", "Set jail time", "Pick community card", "Pick chance card", "Show deck sizes", "Switch to other player", "Display board", "Access available upgrades")); StdOut.println( "\nWARNING !!! Be careful, you could break the game.\n Some of these functions do not do any sanity checks!!!"); StdOut.println( "And really if some of these functions is used in some situations, it will make the game misbehave."); return UI.displayMenuNGetChoice( "Debug options (" + Bank.moneyEuro(Game.getCurrentPlayer().getCash()) + " cash)", items, true, true); }
/** * Unit tests the {@code DepthFirstOrder} data type. * * @param args the command-line arguments */ public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); DepthFirstOrder dfs = new DepthFirstOrder(G); StdOut.println(" v pre post"); StdOut.println("--------------"); for (int v = 0; v < G.V(); v++) { StdOut.printf("%4d %4d %4d\n", v, dfs.pre(v), dfs.post(v)); } StdOut.print("Preorder: "); for (int v : dfs.pre()) { StdOut.print(v + " "); } StdOut.println(); StdOut.print("Postorder: "); for (int v : dfs.post()) { StdOut.print(v + " "); } StdOut.println(); StdOut.print("Reverse postorder: "); for (int v : dfs.reversePost()) { StdOut.print(v + " "); } StdOut.println(); }
/** * Reads in a social network from a file, and then repeatedly reads in individuals from standard * input and prints out their degrees of separation. Takes three command-line arguments: the name * of a file, a delimiter, and the name of the distinguished individual. Each line in the file * contains the name of a vertex, followed by a list of the names of the vertices adjacent to that * vertex, separated by the delimiter. */ public static void main(String[] args) { String filename = args[0]; String delimiter = args[1]; String source = args[2]; // StdOut.println("Source: " + source); SymbolGraph sg = new SymbolGraph(filename, delimiter); Graph G = sg.G(); if (!sg.contains(source)) { StdOut.println(source + " not in database."); return; } int s = sg.index(source); BreadthFirstPaths bfs = new BreadthFirstPaths(G, s); while (!StdIn.isEmpty()) { String sink = StdIn.readLine(); if (sg.contains(sink)) { int t = sg.index(sink); if (bfs.hasPathTo(t)) { for (int v : bfs.pathTo(t)) { StdOut.println(" " + sg.name(v)); } } else { StdOut.println("Not connected"); } } else { StdOut.println(" Not in database."); } } }
/** Print an array of ints to standard output. */ public static void print(int[] a) { int N = a.length; StdOut.println(N); for (int i = 0; i < N; i++) { StdOut.printf("%9d ", a[i]); } StdOut.println(); }
public void printQueue() { for (int i = 1; i < N + 1; i++) { StdOut.println(queue[i].toString()); } StdOut.println(); }
/** * ************************************************************************* Check integrity of * red-black tree data structure. * ************************************************************************* */ private boolean check() { if (!isBST()) StdOut.println("Not in symmetric order"); if (!isSizeConsistent()) StdOut.println("Subtree counts not consistent"); if (!isRankConsistent()) StdOut.println("Ranks not consistent"); if (!is23()) StdOut.println("Not a 2-3 tree"); if (!isBalanced()) StdOut.println("Not balanced"); return isBST() && isSizeConsistent() && isRankConsistent() && is23() && isBalanced(); }
public static void main(String[] args) { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int c = Integer.parseInt(args[2]); if ((a <= b && b <= c) || (a >= b && b >= c)) StdOut.println("true"); else StdOut.println("false"); }
// display an array of Complex numbers to standard output public static void show(Complex[] x, String title) { StdOut.println(title); StdOut.println("-------------------"); for (int i = 0; i < x.length; i++) { StdOut.println(x[i]); } StdOut.println(); }
public static void main(String[] args) { // test client (described below) int N = Integer.parseInt(args[0]); int T = Integer.parseInt(args[1]); PercolationStats test = new PercolationStats(N, T); StdOut.println("mean = " + test.mean()); StdOut.println("stddev = " + test.stddev()); StdOut.println("95% confidence interval = " + test.confidenceLo() + ", " + test.confidenceHi()); }
// test client public static void main(String[] args) { double z = Double.parseDouble(args[0]); double mu = Double.parseDouble(args[1]); double sigma = Double.parseDouble(args[2]); StdOut.println(Phi(z, mu, sigma)); double y = Phi(z); StdOut.println(PhiInverse(y)); }
/** Simple unit test for <tt>StdOut</tt>. */ public static void main(String[] args) { // write to stdout StdOut.println("Test"); StdOut.println(17); StdOut.println(true); StdOut.printf("%.6f\n", 1.0 / 7.0); }
/** Test client. */ public static void main(String[] args) { In in = new In(args[0]); Digraph G = new Digraph(in); StdOut.println(G); StdOut.println(); for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) StdOut.println(v + "->" + w); }
public static void main(String[] args) { Deque<Integer> ints = new Deque<Integer>(); assert ints.size() == 0; assert ints.isEmpty(); ints.addFirst(1); assert ints.size() == 1; assert !ints.isEmpty(); int val = ints.removeFirst(); assert val == 1; assert ints.size() == 0; assert ints.isEmpty(); ints.addFirst(2); assert ints.size() == 1; assert !ints.isEmpty(); val = ints.removeLast(); assert val == 2; assert ints.size() == 0; assert ints.isEmpty(); ints.addFirst(3); ints.addFirst(2); ints.addFirst(1); ints.addLast(4); ints.addLast(5); ints.addLast(6); assert ints.size() == 6; assert !ints.isEmpty(); for (int value : ints) { StdOut.print(value + " "); } StdOut.println(); while (!ints.isEmpty()) { StdOut.println(ints.removeLast()); } assert ints.size() == 0; assert ints.isEmpty(); for (int i = 0; i < 10; ++i) { ints.addFirst(i); } for (int i = 0; i < 10; ++i) { assert ints.size() == 10 - i; ints.removeLast(); } assert ints.size() == 0; }
/** Unit tests the <tt>RedBlackBST</tt> data type. */ public static void main(String[] args) { RedBlackBST<String, Integer> st = new RedBlackBST<String, Integer>(); for (int i = 0; !StdIn.isEmpty(); i++) { String key = StdIn.readString(); st.put(key, i); } for (String s : st.keys()) StdOut.println(s + " " + st.get(s)); StdOut.println(); }
/** Print an array of booleans to standard output. */ public static void print(boolean[] a) { int N = a.length; StdOut.println(N); for (int i = 0; i < N; i++) { if (a[i]) StdOut.print("1 "); else StdOut.print("0 "); } StdOut.println(); }
private static void printArray(int[] array) { if (array != null) { for (int i = 0; i < array.length; i++) { StdOut.print(array[i] + " "); } StdOut.println(); } else { StdOut.println("Array not available"); } }
public static void main(String[] args) { File directory = new File(args[0]); File[] files = directory.listFiles(); // Insertion.sort(files); Selection.sort(files); for (int i = 0; i < files.length; i++) StdOut.println(files[i].getName()); Date d1 = new Date(2011, 12, 15); Date d2 = new Date(2011, 12, 14); StdOut.println(d1.compareTo(d2)); }
// main method simply tests the deleteEvenEven method: public static void main(String[] args) { Graph original = new Graph(10); // Randomly add edges: for (int i = 0; i < 10; i++) for (int j = i + 1; j < 10; j++) if (StdRandom.random() < .5) original.addEdge(i, j); StdOut.println("Original graph:" + original); Graph noeven = deleteEvenEven(original); StdOut.println("No even-to-even edges: " + noeven); }
/** Print the M-by-N array of doubles to standard output. */ public static void print(double[][] a) { int M = a.length; int N = a[0].length; StdOut.println(M + " " + N); for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { StdOut.printf("%9.5f ", a[i][j]); } StdOut.println(); } }
/** Unit tests the <tt>Bag</tt> data type. */ public static void main(String[] args) { Bag<String> bag = new Bag<String>(); while (!StdIn.isEmpty()) { String item = StdIn.readString(); bag.add(item); } StdOut.println("size of bag = " + bag.size()); for (String s : bag) { StdOut.println(s); } }
/** * Reads in a sequence of pairs of integers (between 0 and N-1) from standard input, where each * integer represents some object; if the objects are in different components, merge the two * components and print the pair to standard output. */ public static void main(String[] args) { int N = StdIn.readInt(); QuickUnionUF uf = new QuickUnionUF(N); while (!StdIn.isEmpty()) { int p = StdIn.readInt(); int q = StdIn.readInt(); if (uf.connected(p, q)) continue; uf.union(p, q); StdOut.println(p + " " + q); } StdOut.println(uf.count() + " components"); }