Example #1
0
    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());
                }
            }
        }

    }
Example #2
0
    /**
     * Reads in a command-line integer and sequence of words from
     * standard input and prints out a word (whose length exceeds
     * the threshold) that occurs most frequently to standard output.
     * It also prints out the number of words whose length exceeds
     * the threshold and the number of distinct such words.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        int distinct = 0, words = 0;
        int minlen = Integer.parseInt(args[0]);
        ST<String, Integer> st = new ST<String, Integer>();

        // compute frequency counts
        while (!StdIn.isEmpty()) {
            String key = StdIn.readString();
            if (key.length() < minlen) continue;
            words++;
            if (st.contains(key)) {
                st.put(key, st.get(key) + 1);
            }
            else {
                st.put(key, 1);
                distinct++;
            }
        }

        // find a key with the highest frequency count
        String max = "";
        st.put(max, 0);
        for (String word : st.keys()) {
            if (st.get(word) > st.get(max))
                max = word;
        }

        StdOut.println(max + " " + st.get(max));
        StdOut.println("distinct = " + distinct);
        StdOut.println("words    = " + words);
    }
Example #3
0
    /**
     * Unit tests the {@code TrieST} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // build symbol table from standard input
        TrieST<Integer> st = new TrieST<Integer>();
        for (int i = 0; !StdIn.isEmpty(); i++) {
            String key = StdIn.readString();
            st.put(key, i);
        }

        // print results
        if (st.size() < 100) {
            StdOut.println("keys(\"\"):");
            for (String key : st.keys()) {
                StdOut.println(key + " " + st.get(key));
            }
            StdOut.println();
        }

        StdOut.println("longestPrefixOf(\"shellsort\"):");
        StdOut.println(st.longestPrefixOf("shellsort"));
        StdOut.println();

        StdOut.println("longestPrefixOf(\"quicksort\"):");
        StdOut.println(st.longestPrefixOf("quicksort"));
        StdOut.println();

        StdOut.println("keysWithPrefix(\"shor\"):");
        for (String s : st.keysWithPrefix("shor"))
            StdOut.println(s);
        StdOut.println();

        StdOut.println("keysThatMatch(\".he.l.\"):");
        for (String s : st.keysThatMatch(".he.l."))
            StdOut.println(s);
    }
    /**
     *  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.
     *
     * @param args the command-line arguments
     */
    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.graph();
        if (!sg.contains(source)) {
            StdOut.println(source + " not in database.");
            return;
        }

        int s = sg.indexOf(source);
        BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

        while (!StdIn.isEmpty()) {
            String sink = StdIn.readLine();
            if (sg.contains(sink)) {
                int t = sg.indexOf(sink);
                if (bfs.hasPathTo(t)) {
                    for (int v : bfs.pathTo(t)) {
                        StdOut.println("   " + sg.nameOf(v));
                    }
                }
                else {
                    StdOut.println("Not connected");
                }
            }
            else {
                StdOut.println("   Not in database.");
            }
        }
    }
Example #5
0
    public static void main(String[] args) {
        String filename  = args[0];
        String separator = args[1];
        In in = new In(filename);

        ST<String, Queue<String>> st = new ST<String, Queue<String>>();
        ST<String, Queue<String>> ts = new ST<String, Queue<String>>();

        while (in.hasNextLine()) {
            String line = in.readLine();
            String[] fields = line.split(separator);
            String key = fields[0];
            for (int i = 1; i < fields.length; i++) {
                String val = fields[i];
                if (!st.contains(key)) st.put(key, new Queue<String>());
                if (!ts.contains(val)) ts.put(val, new Queue<String>());
                st.get(key).enqueue(val);
                ts.get(val).enqueue(key);
            }
        }

        StdOut.println("Done indexing");

        // read queries from standard input, one per line
        while (!StdIn.isEmpty()) {
            String query = StdIn.readLine();
            if (st.contains(query)) 
                for (String vals : st.get(query))
                    StdOut.println("  " + vals);
            if (ts.contains(query)) 
                for (String keys : ts.get(query))
                    StdOut.println("  " + keys);
        }

    }
Example #6
0
 /**
  * Unit tests the {@code BinarySearchST} data type.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) { 
     BinarySearchST<String, Integer> st = new BinarySearchST<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));
 }
    /**
     * Unit tests the {@code LinearProbingHashST} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) { 
        LinearProbingHashST<String, Integer> st = new LinearProbingHashST<String, Integer>();
        for (int i = 0; !StdIn.isEmpty(); i++) {
            String key = StdIn.readString();
            st.put(key, i);
        }

        // print keys
        for (String s : st.keys()) 
            StdOut.println(s + " " + st.get(s)); 
    }
Example #8
0
    /**
     * Reads in a sequence of integers from the whitelist file, specified as
     * a command-line argument. Reads in integers from standard input and
     * prints to standard output those integers that are not in the file.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        In in = new In(args[0]);
        int[] white = in.readAllInts();
        StaticSETofInts set = new StaticSETofInts(white);

        // Read key, print if not in whitelist.
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (!set.contains(key))
                StdOut.println(key);
        }
    }
Example #9
0
    public static void main(String[] args) {
        SET<String> set = new SET<String>();

        // read in strings and add to set
        while (!StdIn.isEmpty()) {
            String key = StdIn.readString();
            if (!set.contains(key)) {
                set.add(key);
                StdOut.println(key);
            }
        }
    }
 /**
  * Reads in a sequence of pairs of integers (between 0 and n-1) from standard input, 
  * where each integer represents some object;
  * if the sites are in different components, merge the two components
  * and print the pair to standard output.
  *
  * @param args the command-line arguments
  */
 public static void main(String[] args) {
     int n = StdIn.readInt();
     WeightedQuickUnionUF uf = new WeightedQuickUnionUF(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");
 }
Example #11
0
    /**/ public static void main(string[] strarr)
    {
        int num  = Integer.parseInt(strarr[1]);
        int num2 = Integer.parseInt(strarr[2]);
        ST  sT   = new ST();

        In @in = new In(strarr[0]);

        while (@in.hasNextLine())
        {
            string   text  = @in.readLine();
            string[] array = java.lang.String.instancehelper_split(text, ",");
            string   c     = array[num];
            string   obj   = array[num2];
            sT.put(c, obj);
        }
        while (!StdIn.isEmpty())
        {
            string text = StdIn.readString();
            if (sT.contains(text))
            {
                StdOut.println(sT.get(text));
            }
            else
            {
                StdOut.println("Not found");
            }
        }
    }
}



public class LookupIndex
{
    public LookupIndex()
    {
    }

    /**/ public static void main(string[] strarr)
    {
        string str   = strarr[0];
        string regex = strarr[1];
        In     @in   = new In(str);
        ST     sT    = new ST();
        ST     sT2   = new ST();

        while (@in.hasNextLine())
        {
            string   text  = @in.readLine();
            string[] array = java.lang.String.instancehelper_split(text, regex);
            string   text2 = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                string text3 = array[i];
                if (!sT.contains(text2))
                {
                    sT.put(text2, new Queue());
                }
                if (!sT2.contains(text3))
                {
                    sT2.put(text3, new Queue());
                }
                ((Queue)sT.get(text2)).enqueue(text3);
                ((Queue)sT2.get(text3)).enqueue(text2);
            }
        }
        StdOut.println("Done indexing");
        while (!StdIn.isEmpty())
        {
            string text = StdIn.readLine();
            if (sT.contains(text))
            {
                Iterator iterator = ((Queue)sT.get(text)).iterator();
                while (iterator.hasNext())
                {
                    string text2 = (string)iterator.next();
                    StdOut.println(new StringBuilder().append("  ").append(text2).toString());
                }
            }
            if (sT2.contains(text))
            {
                Iterator iterator = ((Queue)sT2.get(text)).iterator();
                while (iterator.hasNext())
                {
                    string text2 = (string)iterator.next();
                    StdOut.println(new StringBuilder().append("  ").append(text2).toString());
                }
            }
        }
    }
    /**/ public static void main(string[] strarr)
    {
        SET sET = new SET();

        while (!StdIn.isEmpty())
        {
            string text = StdIn.readString();
            if (!sET.contains(text))
            {
                sET.add(text);
                StdOut.println(text);
            }
        }
    }
    /**/ public static void main(string[] strarr)
    {
        int    num  = 0;
        double num2 = (double)0f;
        double num3;

        while (!StdIn.isEmpty())
        {
            num3  = StdIn.readDouble();
            num2 += num3;
            num++;
        }
        num3 = num2 / (double)num;
        StdOut.println(new StringBuilder().append("Average is ").append(num3).toString());
    }
Example #14
0
    /**
     * Reads in a sequence of integers from the whitelist file, specified as
     * a command-line argument; reads in integers from standard input;
     * prints to standard output those integers that do <em>not</em> appear in the file.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {

        // read the integers from a file
        In in = new In(args[0]);
        int[] whitelist = in.readAllInts();

        // sort the array
        Arrays.sort(whitelist);

        // read integer key from standard input; print if not in whitelist
        while (!StdIn.isEmpty()) {
            int key = StdIn.readInt();
            if (BinarySearch.indexOf(whitelist, key) == -1)
                StdOut.println(key);
        }
    }
Example #15
0
    public static void main(String[] args) {
        SET<String> set = new SET<String>();

        // read in strings and add to set
        In in = new In(args[0]);
        while (!in.isEmpty()) {
            String word = in.readString();
            set.add(word);
        }

        // read in string from standard input, printing out all exceptions
        while (!StdIn.isEmpty()) {
            String word = StdIn.readString();
            if (!set.contains(word))
                StdOut.println(word);
        }
    }
Example #16
0
    /**
     * Reads in a sequence of real numbers from standard input and prints
     * out their average to standard output.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) { 
        int count = 0;       // number input values
        double sum = 0.0;    // sum of input values

        // read data and compute statistics
        while (!StdIn.isEmpty()) {
            double value = StdIn.readDouble();
            sum += value;
            count++;
        }

        // compute the average
        double average = sum / count;

        // print results
        StdOut.println("Average is " + average);
    }
Example #17
0
    /**
     * Unit tests the {@code BST} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) { 
        BST<String, Integer> st = new BST<String, Integer>();
        for (int i = 0; !StdIn.isEmpty(); i++) {
            String key = StdIn.readString();
            if ((st.size() > 1) && (st.floor(key) != st.floor2(key)))
                throw new RuntimeException("floor() function inconsistent");
            st.put(key, i);
        }

        for (String s : st.levelOrder())
            StdOut.println(s + " " + st.get(s));

        StdOut.println();

        for (String s : st.keys())
            StdOut.println(s + " " + st.get(s));
    }
Example #18
0
    /**/ public static void main(string[] strarr)
    {
        string      str         = strarr[0];
        string      str2        = strarr[1];
        string      str3        = strarr[2];
        SymbolGraph symbolGraph = new SymbolGraph(str, str2);
        Graph       g           = symbolGraph.G();

        if (!symbolGraph.contains(str3))
        {
            StdOut.println(new StringBuilder().append(str3).append(" not in database.").toString());
            return;
        }
        int i = symbolGraph.index(str3);
        BreadthFirstPaths breadthFirstPaths = new BreadthFirstPaths(g, i);

        while (!StdIn.isEmpty())
        {
            string str4 = StdIn.readLine();
            if (symbolGraph.contains(str4))
            {
                int i2 = symbolGraph.index(str4);
                if (breadthFirstPaths.hasPathTo(i2))
                {
                    Iterator iterator = breadthFirstPaths.pathTo(i2).iterator();
                    while (iterator.hasNext())
                    {
                        int i3 = ((Integer)iterator.next()).intValue();
                        StdOut.println(new StringBuilder().append("   ").append(symbolGraph.name(i3)).toString());
                    }
                }
                else
                {
                    StdOut.println("Not connected");
                }
            }
            else
            {
                StdOut.println("   Not in database.");
            }
        }
    }
Example #19
0
    /**
     * Unit tests the {@code TrieSET} data type.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        TrieSET set = new TrieSET();
        while (!StdIn.isEmpty()) {
            String key = StdIn.readString();
            set.add(key);
        }

        // print results
        if (set.size() < 100) {
            StdOut.println("keys(\"\"):");
            for (String key : set) {
                StdOut.println(key);
            }
            StdOut.println();
        }

        StdOut.println("longestPrefixOf(\"shellsort\"):");
        StdOut.println(set.longestPrefixOf("shellsort"));
        StdOut.println();

        StdOut.println("longestPrefixOf(\"xshellsort\"):");
        StdOut.println(set.longestPrefixOf("xshellsort"));
        StdOut.println();

        StdOut.println("keysWithPrefix(\"shor\"):");
        for (String s : set.keysWithPrefix("shor"))
            StdOut.println(s);
        StdOut.println();

        StdOut.println("keysWithPrefix(\"shortening\"):");
        for (String s : set.keysWithPrefix("shortening"))
            StdOut.println(s);
        StdOut.println();

        StdOut.println("keysThatMatch(\".he.l.\"):");
        for (String s : set.keysThatMatch(".he.l."))
            StdOut.println(s);
    }
Example #20
0
    public static void main(String[] args) {
        int keyField = Integer.parseInt(args[1]);
        int valField = Integer.parseInt(args[2]);

        // symbol table
        ST<String, String> st = new ST<String, String>();

        // read in the data from csv file
        In in = new In(args[0]);
        while (in.hasNextLine()) {
            String line = in.readLine();
            String[] tokens = line.split(",");
            String key = tokens[keyField];
            String val = tokens[valField];
            st.put(key, val);
        }

        while (!StdIn.isEmpty()) {
            String s = StdIn.readString();
            if (st.contains(s)) StdOut.println(st.get(s));
            else                StdOut.println("Not found");
        }
    }