Exemplo n.º 1
0
 public static void main(String[] args) {
   // mystery() is a product function
   assert mystery(2, 25) == 50;
   assert mystery(3, 11) == 33;
   // mysteryStar() is a power function
   assert mysteryStar(2, 5) == 32;
   assert mysteryStar(3, 4) == 81;
   StdOut.printf("mystery(2, 25) = %d\n", mystery(2, 25));
   StdOut.printf("mystery(3, 11) = %d\n", mystery(3, 11));
   StdOut.printf("mysteryStar(2, 5) == %d\n", mysteryStar(2, 5));
   StdOut.printf("mysteryStar(3, 4) == %d\n", mysteryStar(3, 4));
 }
Exemplo n.º 2
0
  /** Unit tests the <tt>DepthFirstOrder</tt> data type. */
  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();
  }
Exemplo n.º 3
0
  /** 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);
  }
 public static int rank(int key, int[] a, int lo, int hi, int deep) {
   for (int i = 0; i < deep; i++) {
     StdOut.print(" ");
   }
   StdOut.printf("%d %d %d\n", lo, hi, deep);
   if (lo > hi) return -1;
   int mid = lo + (hi - lo) / 2;
   if (key < a[mid]) return rank(key, a, lo, mid - 1, ++deep);
   else if (key > a[mid]) return rank(key, a, mid + 1, hi, ++deep);
   else return mid;
 }
Exemplo n.º 5
0
 // do unit testing of this class
 public static void main(String[] args) {
   In in = new In(args[0]);
   Digraph G = new Digraph(in);
   SAP sap = new SAP(G);
   while (!StdIn.isEmpty()) {
     int v = StdIn.readInt();
     int w = StdIn.readInt();
     int length = sap.length(v, w);
     int ancestor = sap.ancestor(v, w);
     StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);
   }
 }
Exemplo n.º 6
0
 /* do unit testing of this class */
 public static void main(String[] args) {
   System.out.println("Test started file name " + args[0]);
   In in = new In(args[0]);
   Digraph G = new Digraph(in);
   System.out.println("Graph vertices " + G.V() + " graph edges " + G.E());
   System.out.println(G.toString());
   SAP sap = new SAP(G);
   while (!StdIn.isEmpty()) {
     int v = StdIn.readInt();
     int w = StdIn.readInt();
     int length = sap.length(v, w);
     int ancestor = sap.ancestor(v, w);
     StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);
   }
 }
Exemplo n.º 7
0
  public static void main(String[] args) {

    In in1 = new In(Inputs.DIGRAPH_SMALL);
    In in2 = new In(Inputs.DIGRAPH_SMALL);
    edu.princeton.cs.algs4.Digraph sedgewicks = new edu.princeton.cs.algs4.Digraph(in1);
    Digraph digraph = new Digraph(in2);
    in1.close();
    in2.close();

    StdOut.printf("Sedgewicks - v: %d, e: %d\n", sedgewicks.V(), sedgewicks.E());
    for (int i = 0; i < sedgewicks.V(); i++) {
      for (int w : sedgewicks.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
    StdOut.println();

    StdOut.printf("My - v: %d, e: %d\n", digraph.V(), digraph.E());
    for (int i = 0; i < digraph.V(); i++) {
      for (int w : digraph.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
    StdOut.println();

    edu.princeton.cs.algs4.Digraph sedwickDi = sedgewicks.reverse();
    StdOut.printf("Sedgewicks reverse - v: %d, e: %d\n", sedwickDi.V(), sedwickDi.E());
    for (int i = 0; i < sedwickDi.V(); i++) {
      for (int w : sedwickDi.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
    StdOut.println();

    Digraph myReverse = digraph.reverse();
    StdOut.printf("My reverse - v: %d, e: %d\n", myReverse.V(), myReverse.E());
    for (int i = 0; i < myReverse.V(); i++) {
      for (int w : myReverse.adj(i)) {
        StdOut.printf("%d -> %d\n", i, w);
      }
    }
  }
Exemplo n.º 8
0
 private static void brute(Point[] points) {
   Arrays.sort(points);
   for (int i = 0; i < points.length - 3; i++) {
     for (int j = i + 1; j < points.length - 2; j++) {
       for (int k = j + 1; k < points.length - 1; k++) {
         for (int n = k + 1; n < points.length; n++) {
           // System.out.println(i + "," + j + "," + k + "," + n);
           Comparator<Point> comparator = points[i].SLOPE_ORDER;
           if (comparator.compare(points[j], points[k]) == 0
               && comparator.compare(points[k], points[n]) == 0) {
             StdOut.printf(
                 points[i] + " -> " + points[j] + " -> " + points[k] + " -> " + points[n] + "\n");
             points[i].drawTo(points[n]);
           }
         }
       }
     }
   }
 }
Exemplo n.º 9
0
  public static void execute(final Class<?> clazz) {
    try {
      final Method method = clazz.getMethod("main", String[].class);
      final Annotation[] annotations = clazz.getAnnotationsByType(TestDrive.class);

      for (final Annotation annotation : annotations) {
        final TestDrive td = (TestDrive) annotation;
        StdOut.printf("java %s %s%n", clazz.getSimpleName(), Arrays.deepToString(td.value()));

        final String[] value = td.value();
        if (td.valueFile()) {
          for (int i = 0; i < value.length; i++) {
            value[i] = RESOURCES_PATH + value[i];
          }
        }

        if (td.input().length > 0) {
          System.setIn(
              new ByteArrayInputStream(
                  Arrays.stream(getInputValue(td)).collect(Collectors.joining(" ")).getBytes()));

          final Method resync = StdIn.class.getDeclaredMethod("resync");
          resync.setAccessible(true);
          resync.invoke(null);
        }

        method.invoke(null, (Object) value);
      }
    } catch (IllegalArgumentException
        | NoSuchMethodException
        | SecurityException
        | IllegalAccessException exception) {
      StdOut.println(exception.getMessage());
      StdOut.println(exception);
    } catch (final InvocationTargetException exception) {
      StdOut.println(exception.getMessage());
      StdOut.println(exception);
      StdOut.println(exception.getTargetException());
    }
  }