예제 #1
0
  public static void main(String[] args) {
    int N = 30;
    int M = 10;

    int[] a = new int[N];
    for (int i = 0; i < N; i++) a[i] = StdRandom.uniform(M);

    for (int i = 0; i < N; i++) StdOut.printf("%2d", a[i]);

    int[] h = histogram(a, M);

    StdOut.println("\n");
    for (int i = 0; i < M; i++) StdOut.printf("%4d", h[i]);
  }
예제 #2
0
 public static void main(String[] args) {
   ResizingArray<Integer> a = new ResizingArray<>(0);
   for (int i = 0; i < 10; i++) {
     a.add(StdRandom.uniform(100));
     StdOut.println(a);
   }
   a.set(1, 5);
   StdOut.println(a);
   a.addAll(new Integer[] {1, 2, 3, 4, 5});
   StdOut.println(a);
   Queue<Integer> q = new Queue<>();
   for (int i = 1; i < 6; i++) q.enqueue(i);
   a.addAll(q);
   StdOut.println(a);
 }
예제 #3
0
 public static void main(String[] args) {
   StdIn.fromFile("data/100ints.txt");
   final int[] a = StdIn.readAllInts();
   final int cnt = count(a);
   StdOut.println(cnt);
   if (cnt < 10) {
     printAll(a);
   }
 }
예제 #4
0
 public static void main(String[] args) {
   String regexp = "(.*" + args[0] + ".*)";
   NFA nfa = new NFA(regexp);
   while (StdIn.hasNextLine()) {
     String txt = StdIn.readLine();
     if (nfa.recognizes(txt)) {
       StdOut.println(txt);
     }
   }
 }
예제 #5
0
 /** @param args */
 public static void main(String[] args) {
   int[] a = new int[2];
   int[] b = new int[2];
   a[0] = 5;
   b[1] = 5;
   if (compareProfiles(a, b) == 0) {
     StdOut.println("Test passed!");
   }
   int k = 20;
   int d = 10000;
   ArrayList<String> races = new ArrayList<String>();
   ArrayList<int[]> profiles = new ArrayList<int[]>();
   while (!StdIn.isEmpty()) {
     races.add(StdIn.readLine());
     profiles.add(calculateProfile(StdIn.readLine() + StdIn.readLine() + StdIn.readLine(), k, d));
   }
   StdOut.println(races.size() + ", " + profiles.size());
   for (int i = 0; i < profiles.size(); i++) {
     StdOut.println(races.get(i));
     for (int j = 0; j < profiles.size(); j++) {
       StdOut.print(
           "\t" + races.get(j) + " " + compareProfiles(profiles.get(i), profiles.get(j)) + ", ");
       StdOut.println();
     }
     StdOut.println();
   }
 }
예제 #6
0
 public static void main(String[] args) {
   int N = 2000;
   countops(N);
   // System.exit (0);
   double prevOps = ops;
   double prevTime = time;
   for (int i = 0; i < 12; i++) {
     N *= 2;
     countops(N);
     StdOut.printf("%8d %10d %5.1f [%5.3f %5.3f]\n", N, ops, ops / prevOps, time, time / prevTime);
     prevOps = ops;
     prevTime = time;
   }
 }
예제 #7
0
 // print distinct 4-tuples (i, j, k, l) such that a[i] + a[j] + a[k] + a[l] = 0
 public static int printAll(int[] a) {
   final int N = a.length;
   int cnt = 0;
   for (int i = 0; i < N; i++) {
     for (int j = i + 1; j < N; j++) {
       for (int k = j + 1; k < N; k++) {
         for (int l = k + 1; l < N; l++) {
           if (a[i] + a[j] + a[k] + a[l] == 0) {
             StdOut.println(a[i] + " " + a[j] + " " + a[k] + " " + a[l]);
             cnt++;
           }
         }
       }
     }
   }
   return cnt;
 }
예제 #8
0
  // character array
  public static void charArray() {
    final int[] sizes = {
      64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
    };
    final int M = sizes.length;

    final double[] x = new double[M];
    final double[] memory = new double[M];

    for (int i = 0; i < M; i++) {
      final char[] s = random(sizes[i]).toCharArray();
      x[i] = s.length;
      memory[i] = MemoryUtil.deepMemoryUsageOf(s);
    }

    final XLinearRegression regression = new XLinearRegression(x, memory);
    StdOut.println("char[] array of length N  = " + regression);
  }
예제 #9
0
파일: Words.java 프로젝트: esfdk/HoNmasters
  /**
   * Runs the test.
   *
   * @param args Location of file to be used in Words.
   */
  public static void main(String[] args) {
    // In objects from Stdlib.
    In dat = new In(args[0] + ".dat"); // In file used to construct the Word graph.
    In in = new In(args[0] + "-test.in"); // In file containing the words to be checked.

    ArrayList<String> words =
        new ArrayList<
            String>(); // List to hold the words, so that we can later refer to it using an index.

    while (!dat.isEmpty()) { // Fills out the List with the Words from the .dat file.
      words.add(dat.readLine());
    }

    Words w =
        new Words(); // Only used to make the Inner objects Digraph and BreadthFirstDirectedPaths

    Digraph dg =
        w
        .new Digraph(
            words.size()); // Constructs the Directed Graph from the size of the list of Words.

    // Fills out the edges in the Directed Graph. This is done in Quadratic time. :/
    for (String s : words) {
      for (String a : words) {
        if (!a.equals(
            s)) { // Checks if it's the same word - if they are, we should not addEdges from the
                  // word to itself.
          if (compareWords(s, a)) { // Checks if there should be an arc from s to a.
            dg.addEdge(words.indexOf(s), words.indexOf(a));
          }
        }
      }
    }

    while (!in
        .isEmpty()) { // Runs BFS on the word combinations from the -test.in file and prints the
                      // result.
      BreadthFirstDirectedPaths BFS =
          w.new BreadthFirstDirectedPaths(dg, words.indexOf(in.readString()));
      StdOut.println(BFS.distTo(words.indexOf(in.readString())));
    }
  }
예제 #10
0
  // array of suffixes
  public static void suffixes() {
    final int[] sizes = {
      64, 128, 192, 256, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
    };
    final int M = sizes.length;

    final double[] x = new double[M];
    final double[] memory = new double[M];

    for (int i = 0; i < M; i++) {
      final String s = random(sizes[i]);
      final String[] suffixes = new String[s.length()];
      for (int j = 0; j < s.length(); j++) suffixes[j] = s.substring(j);
      x[i] = s.length();
      memory[i] = MemoryUtil.deepMemoryUsageOf(suffixes);
    }

    final XLinearRegression regression = new XLinearRegression(x, memory);
    StdOut.println("String[] of N suffixes    = " + regression);
  }
예제 #11
0
  // substring
  public static void substring() {
    final String genome = "CGCCTGGCGTCTGTAC";
    final String codon = genome.substring(6, 9);
    final LinkedList<String> list = new LinkedList<>();
    list.add(genome);
    list.add(codon);

    StdOut.println(
        "shallow memory of genome = \"CGCCTGGCGTCTGTAC\"    = " + MemoryUtil.memoryUsageOf(genome));
    StdOut.println(
        "shallow memory of codon = genome.substring(6, 9) = " + MemoryUtil.memoryUsageOf(codon));
    StdOut.println(
        "shallow memory of list                           = " + MemoryUtil.memoryUsageOf(list));

    StdOut.println(
        "deep memory of genome = \"CGCCTGGCGTCTGTAC\"    = "
            + MemoryUtil.deepMemoryUsageOf(genome));
    StdOut.println(
        "deep memory of codon = genome.substring(6, 9) = " + MemoryUtil.deepMemoryUsageOf(codon));
    StdOut.println(
        "deep memory of both                           = " + MemoryUtil.deepMemoryUsageOfAll(list));
  }
예제 #12
0
 public static void main(String[] args) {
   for (int N = 250; true; N += N) {
     final double time = timeTrial(N);
     StdOut.printf("%7d %5.1f\n", N, time);
   }
 }
예제 #13
0
 // print array to standard output
 private static <T> void show(T[] a) {
   for (T element : a) {
     StdOut.println(element);
   }
 }