Search in sources :

Example 11 with Bag

use of com.jimmysun.algorithms.chapter1_3.Bag in project useful-java-links by Vedenin.

the class ApacheTreeBagTest method main.

public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = new TreeBag(Arrays.asList(INPUT_TEXT.split(" ")));
    // Print count words
    // print [1:All!,2:Hello,1:Hi,2:World!]- in natural (alphabet) order
    System.out.println(bag);
    // Print all unique words
    // print [All!, Hello, Hi, World!]- in natural (alphabet) order
    System.out.println(bag.uniqueSet());
    // Print count occurrences of words
    // print 2
    System.out.println("Hello = " + bag.getCount("Hello"));
    // print 2
    System.out.println("World = " + bag.getCount("World!"));
    // print 1
    System.out.println("All = " + bag.getCount("All!"));
    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));
    // print 0
    System.out.println("Empty = " + bag.getCount("Empty"));
    // Print count all words
    //print 6
    System.out.println(bag.size());
    // Print count unique words
    //print 4
    System.out.println(bag.uniqueSet().size());
}
Also used : TreeBag(org.apache.commons.collections4.bag.TreeBag) TreeBag(org.apache.commons.collections4.bag.TreeBag) Bag(org.apache.commons.collections4.Bag)

Example 12 with Bag

use of com.jimmysun.algorithms.chapter1_3.Bag in project useful-java-links by Vedenin.

the class ApacheSynchronizedBag method main.

public static void main(String[] args) {
    // Parse text to separate words
    String INPUT_TEXT = "Hello World! Hello All! Hi World!";
    // Create Multiset
    Bag bag = SynchronizedBag.synchronizedBag(new HashBag(Arrays.asList(INPUT_TEXT.split(" "))));
    // Print count words
    // print [1:Hi,2:Hello,2:World!,1:All!] - in random orders
    System.out.println(bag);
    // Print all unique words
    // print [Hi, Hello, World!, All!] - in random orders
    System.out.println(bag.uniqueSet());
    // Print count occurrences of words
    // print 2
    System.out.println("Hello = " + bag.getCount("Hello"));
    // print 2
    System.out.println("World = " + bag.getCount("World!"));
    // print 1
    System.out.println("All = " + bag.getCount("All!"));
    // print 1
    System.out.println("Hi = " + bag.getCount("Hi"));
    // print 0
    System.out.println("Empty = " + bag.getCount("Empty"));
    // Print count all words
    //print 6
    System.out.println(bag.size());
    // Print count unique words
    //print 4
    System.out.println(bag.uniqueSet().size());
}
Also used : HashBag(org.apache.commons.collections4.bag.HashBag) SynchronizedBag(org.apache.commons.collections4.bag.SynchronizedBag) Bag(org.apache.commons.collections4.Bag) HashBag(org.apache.commons.collections4.bag.HashBag)

Example 13 with Bag

use of com.jimmysun.algorithms.chapter1_3.Bag in project AlgorithmsSolutions by Allenskoo856.

the class CC method main.

public static void main(String[] args) {
    Graph G = new Graph(new In(args[0]));
    CC cc = new CC(G);
    int M = cc.count();
    StdOut.println(M + " components");
    Bag<Integer>[] components = (Bag<Integer>[]) new Bag[M];
    for (int i = 0; i < M; i++) {
        components[i] = new Bag<Integer>();
    }
    for (int v = 0; v < G.V(); v++) {
        components[cc.id(v)].add(v);
    }
    for (int i = 0; i < M; i++) {
        for (int v : components[i]) {
            StdOut.print(v + " ");
        }
        StdOut.println();
    }
}
Also used : In(edu.princeton.cs.algs4.In) Bag(com.jimmysun.algorithms.chapter1_3.Bag)

Example 14 with Bag

use of com.jimmysun.algorithms.chapter1_3.Bag in project algorithms-sedgewick-wayne by reneargento.

the class RegularExpressionMatcher method recognizes.

public boolean recognizes(String text) {
    Bag<Integer> allPossibleStates = new Bag<>();
    DirectedDFS directedDFS = new DirectedDFS(digraph, 0);
    for (int vertex = 0; vertex < digraph.vertices(); vertex++) {
        if (directedDFS.marked(vertex)) {
            allPossibleStates.add(vertex);
        }
    }
    for (int i = 0; i < text.length(); i++) {
        // Compute possible NFA states for text[i + 1]
        Bag<Integer> states = new Bag<>();
        for (int vertex : allPossibleStates) {
            if (vertex < numberOfStates) {
                if (regularExpression[vertex] == text.charAt(i) || regularExpression[vertex] == '.') {
                    states.add(vertex + 1);
                }
            }
        }
        allPossibleStates = new Bag<>();
        directedDFS = new DirectedDFS(digraph, states);
        for (int vertex = 0; vertex < digraph.vertices(); vertex++) {
            if (directedDFS.marked(vertex)) {
                allPossibleStates.add(vertex);
            }
        }
    }
    for (int vertex : allPossibleStates) {
        if (vertex == numberOfStates) {
            return true;
        }
    }
    return false;
}
Also used : Bag(chapter1.section3.Bag) DirectedDFS(chapter4.section2.DirectedDFS)

Aggregations

Bag (org.apache.commons.collections4.Bag)8 Bag (com.jimmysun.algorithms.chapter1_3.Bag)4 In (edu.princeton.cs.algs4.In)4 HashBag (org.apache.commons.collections4.bag.HashBag)4 TreeBag (org.apache.commons.collections4.bag.TreeBag)4 Bag (chapter1.section3.Bag)2 SynchronizedBag (org.apache.commons.collections4.bag.SynchronizedBag)2 SynchronizedSortedBag (org.apache.commons.collections4.bag.SynchronizedSortedBag)2 DirectedDFS (chapter4.section2.DirectedDFS)1 DirectedDFS (com.jimmysun.algorithms.chapter4_2.DirectedDFS)1