Beispiel #1
0
        public static void MainTest(string[] args)
        {
            TextInput   StdIn = new TextInput();
            Accumulator stats = new Accumulator();

            while (!StdIn.IsEmpty)
            {
                double x = StdIn.ReadDouble();
                stats.AddDataValue(x);
            }

            Console.Write("N      = {0}\n", stats.Count());
            Console.Write("mean   = {0:F5}\n", stats.Mean());
            Console.Write("stddev = {0:F5}\n", stats.Stddev());
            Console.Write("var    = {0:F5}\n", stats.Var());
        }
Beispiel #2
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            BinarySearchST <string, int> st = new BinarySearchST <string, int>();

            for (int i = 0; !StdIn.IsEmpty; i++)
            {
                string key = StdIn.ReadString();
                st[key] = i;
            }
            foreach (string s in st.Keys())
            {
                Console.WriteLine("{0} {1}", s, st[s]);
            }
        }
Beispiel #3
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            int       N     = StdIn.ReadInt();

            Point2D[] points = new Point2D[N];
            for (int i = 0; i < N; i++)
            {
                double x = StdIn.ReadDouble();
                double y = StdIn.ReadDouble();
                points[i] = new Point2D(x, y);
            }
            ClosestPair closest = new ClosestPair(points);

            Console.WriteLine("{0} from {1} to {2}", closest.Distance(), closest.Either, closest.Other);
        }
Beispiel #4
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            int       N     = StdIn.ReadInt();

            Point2D[] points = new Point2D[N];
            for (int i = 0; i < N; i++)
            {
                int x = StdIn.ReadInt();
                int y = StdIn.ReadInt();
                points[i] = new Point2D(x, y);
            }
            FarthestPair farthest = new FarthestPair(points);

            Console.WriteLine(farthest.Distance() + " from " + farthest.Either + " to " + farthest.Other);
        }
Beispiel #5
0
        public static void MainTest(string[] args)
        {
            SequentialSearchST <string, int> st = new SequentialSearchST <string, int>();
            TextInput StdIn = new TextInput();

            string[] keys = StdIn.ReadAllStrings();

            for (int i = 0; i < keys.Length; i++)
            {
                st.Put(keys[i], i);
            }
            foreach (string s in st.Keys())
            {
                Console.WriteLine("{0} {1}", s, st.Get(s));
            }
        }
        public static void MainTest(string[] args)
        {
            TextInput input             = new TextInput(args[0]);
            Digraph   G                 = new Digraph(input);
            int       s                 = int.Parse(args[1]);
            NonrecursiveDirectedDFS dfs = new NonrecursiveDirectedDFS(G, s);

            for (int v = 0; v < G.V; v++)
            {
                if (dfs.Marked(v))
                {
                    Console.Write(v + " ");
                }
            }
            Console.WriteLine();
        }
Beispiel #7
0
        /// <summary>
        /// Demo test for the <c>TextInput</c> data type. The test shows
        /// the methods' behavior and how to use them.
        /// </summary>
        /// <param name="args">Place holder for user arguments</param>
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            Console.Write("Type 3 char, 1 integer, a few strings: ");
            char[] c = { StdIn.ReadChar(), StdIn.ReadChar(), StdIn.ReadChar() };
            int    a = StdIn.ReadInt();
            string s = StdIn.ReadLine();

            Console.WriteLine("3 char: {0}, 1 int: {1}, new line: {2}", new string(c), a, s);

            Console.Write("Type a string: ");
            s = StdIn.ReadString();
            Console.WriteLine("Your string was: " + s);
            Console.WriteLine();

            Console.Write("Type an int: ");
            a = StdIn.ReadInt();
            Console.WriteLine("Your int was: " + a);
            Console.WriteLine();

            Console.Write("Type a bool: ");
            bool b = StdIn.ReadBool();

            Console.WriteLine("Your bool was: " + b);
            Console.WriteLine();

            Console.Write("Type a double: ");
            double d = StdIn.ReadDouble();

            Console.WriteLine("Your double was: " + d);
            Console.WriteLine();

            Console.WriteLine("Enter a line:");
            s = StdIn.ReadLine();
            Console.WriteLine("Your line was: " + s);
            Console.WriteLine();

            Console.Write("Type any thing you like, enter Ctrl-Z to finish: ");
            string[] all = StdIn.ReadAllStrings();
            Console.WriteLine("Your remaining input was:");
            foreach (var str in all)
            {
                Console.Write(str + " ");
            }
            Console.WriteLine();
        }
Beispiel #8
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput("tale.txt");

            int distinct = 0, words = 0;
            int minlen          = int.Parse(args[0]);
            ST <string, int> st = new ST <string, int>();

            int count = 0;

            // compute frequency counts
            while (!StdIn.IsEmpty)
            {
                string key = StdIn.ReadString();
                count++;
                if (key.Length < minlen)
                {
                    continue;
                }
                words++;
                if (st.Contains(key))
                {
                    st[key] = st[key] + 1;
                }
                else
                {
                    st[key] = 1;
                    distinct++;
                }
            }

            // find a key with the highest frequency count
            string max = "";

            st[max] = 0;;
            foreach (string word in st.Keys())
            {
                if (st[word] > st[max])
                {
                    max = word;
                }
            }

            Console.WriteLine(max + " " + st[max]);
            Console.WriteLine("distinct = " + distinct);
            Console.WriteLine("words    = " + words);
        }
Beispiel #9
0
        public static void MainTest(string[] args)
        {
            // read in the strings from standard input
            TextInput StdIn = new TextInput();

            string[] a = StdIn.ReadAllStrings();
            int      N = a.Length;

            // sort the strings
            Quick3string.Sort(a);

            // print the results
            for (int i = 0; i < N; i++)
            {
                Console.WriteLine(a[i]);
            }
        }
Beispiel #10
0
        /// <summary>
        /// Initializes an edge-weighted graph from an input stream.
        /// The format is the number of vertices <c>V</c>,
        /// followed by the number of edges <c>E</c>,
        /// followed by <c>E</c> pairs of vertices and edge weights,
        /// with each entry separated by whitespace.</summary>
        /// <param name="input"> in the input stream</param>
        /// <exception cref="IndexOutOfRangeException">if the endpoints of any edge are not in prescribed range</exception>
        /// <exception cref="ArgumentException">if the number of vertices or edges is negative</exception>
        ///
        public EdgeWeightedGraph(TextInput input) : this(input.ReadInt())
        {
            int E = input.ReadInt();

            if (E < 0)
            {
                throw new ArgumentException("Number of edges must be nonnegative");
            }
            for (int i = 0; i < E; i++)
            {
                int    v      = input.ReadInt();
                int    w      = input.ReadInt();
                double weight = input.ReadDouble();
                Edge   e      = new Edge(v, w, weight);
                AddEdge(e);
            }
        }
Beispiel #11
0
        public static void MainTest(string[] args)
        {
            // build symbol table from standard input
            TrieST <int> st = new TrieST <int>();

            TextInput StdIn = new TextInput();

            for (int i = 0; !StdIn.IsEmpty; i++)
            {
                string key = StdIn.ReadString();
                st.Put(key, i);
                //Console.WriteLine("Key, value = {0}, {1} is {2}", key, st[key], st[key] == i);
            }

            // print results
            if (st.Count < 100)
            {
                Console.WriteLine("keys(\"\"):");
                foreach (string key in st.Keys)
                {
                    Console.WriteLine(key + " " + st.Get(key));
                }
                Console.WriteLine();
            }

            Console.WriteLine("longestPrefixOf(\"shellsort\"):");
            Console.WriteLine(st.LongestPrefixOf("shellsort"));
            Console.WriteLine();

            Console.WriteLine("longestPrefixOf(\"quicksort\"):");
            Console.WriteLine(st.LongestPrefixOf("quicksort"));
            Console.WriteLine();

            Console.WriteLine("keysWithPrefix(\"shor\"):");
            foreach (string s in st.KeysWithPrefix("shor"))
            {
                Console.WriteLine(s);
            }
            Console.WriteLine();

            Console.WriteLine("keysThatMatch(\".he.l.\"):");
            foreach (string s in st.KeysThatMatch(".he.l."))
            {
                Console.WriteLine(s);
            }
        }
Beispiel #12
0
        public static void MainTest(string[] args)
        {
            LinearProbingHashST <string, int> st = new LinearProbingHashST <string, int>();
            TextInput StdIn = new TextInput();

            string[] keys = StdIn.ReadAllStrings();

            for (int i = 0; i < keys.Length; i++)
            {
                st[keys[i]] = i;
            }
            foreach (string s in st.Keys())
            {
                Console.WriteLine("{0} {1}", s, st[s]);
            }
            Console.WriteLine();
        }
Beispiel #13
0
        public static void MainTest(string[] args)
        {
            TextInput    StdIn = new TextInput();
            Bag <string> bag   = new Bag <string>();

            while (!StdIn.IsEmpty)
            {
                string item = StdIn.ReadString();
                bag.Add(item);
            }

            Console.WriteLine("Size of bag = " + bag.Count);
            foreach (string s in bag)
            {
                Console.WriteLine(s);
            }
        }
        /// <summary>
        /// Initializes an edge-weighted digraph from a text input stream.
        /// The format is the number of vertices <c>V</c>,
        /// followed by the number of edges <c>E</c>,
        /// followed by <c>E</c> pairs of vertices and edge weights,
        /// with each entry separated by whitespace.</summary>
        /// <param name="input">the input stream</param>
        /// <exception cref="IndexOutOfRangeException">if the endpoints of any edge are not in prescribed range</exception>
        /// <exception cref="ArgumentException">if the number of vertices or edges is negative</exception>
        ///
        public AdjMatrixEdgeWeightedDigraph(TextInput input) : this(input.ReadInt())
        {
            int E = input.ReadInt();

            if (E < 0)
            {
                throw new ArgumentException("Number of edges must be nonnegative");
            }
            for (int i = 0; i < E; i++)
            {
                int v = input.ReadInt();
                int w = input.ReadInt();
                // validation wil be done from within AddEdge
                double weight = input.ReadDouble();
                AddEdge(new DirectedEdge(v, w, weight));
            }
        }
Beispiel #15
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            string[] a = StdIn.ReadAllStrings();
            OrderHelper.Show(a);

            // shuffle
            StdRandom.Shuffle(a);

            // display results again using select
            Console.WriteLine();
            for (int i = 0; i < a.Length; i++)
            {
                string ith = (string)Quick.Select(a, i);
                Console.WriteLine(ith);
            }
        }
Beispiel #16
0
        public static void MainTest(string[] args)
        {
            TextInput       input = new TextInput(args[0]);
            Graph           G     = new Graph(input);
            int             s     = int.Parse(args[1]);
            NonrecursiveDFS dfs   = new NonrecursiveDFS(G, s);

            int markedCount = 0;

            for (int v = 0; v < G.V; v++)
            {
                if (dfs.Marked(v))
                {
                    Console.Write(v + " ");
                    markedCount++;
                }
            }
            Console.WriteLine("\nTotal {0} marked vertices out of {1} vertices\n", markedCount, G.V);

            for (int v = 0; v < G.V; v++)
            {
                if (dfs.HasPathTo(v))
                {
                    Console.Write("{0,2} to {1,2}:  ", s, v);
                    foreach (int x in dfs.PathTo(v))
                    {
                        if (x == s)
                        {
                            Console.Write(x);
                        }
                        else
                        {
                            Console.Write("-" + x);
                        }
                    }
                    Console.WriteLine();
                }

                else
                {
                    Console.Write("{0,2} to {1,2}:  not connected\n", s, v);
                }
            }
        }
Beispiel #17
0
        /// <summary>
        /// Initializes a graph from a file using the specified delimiter.
        /// 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.</summary>
        /// <param name="filename">the name of the file</param>
        /// <param name="delimiter">the delimiter between fields</param>
        ///
        public SymbolGraph(string filename, string delimiter)
        {
            st = new ST <string, int>();

            // First pass builds the index by reading strings to associate
            // distinct strings with an index
            TextInput input = new TextInput(filename);

            // while (in.hasNextLine()) {
            while (!input.IsEmpty)
            {
                string[] a = input.ReadLine().Split(delimiter.ToCharArray());
                for (int i = 0; i < a.Length; i++)
                {
                    if (!st.Contains(a[i]))
                    {
                        st[a[i]] = st.Count;
                    }
                }
            }
            Console.WriteLine("Done reading " + filename);

            // inverted index to get string keys in an aray
            keys = new string[st.Count];
            foreach (string name in st.Keys())
            {
                keys[st[name]] = name;
            }

            // second pass builds the graph by connecting first vertex on each
            // line to all others
            innerGraph = new Graph(st.Count);
            input      = new TextInput(filename);
            while (input.HasNextLine())
            {
                string[] a = input.ReadLine().Split(delimiter.ToCharArray());
                int      v = st[a[0]];
                for (int i = 1; i < a.Length; i++)
                {
                    int w = st[a[i]];
                    innerGraph.AddEdge(v, w);
                }
            }
        }
Beispiel #18
0
        public static void MainTest(string[] args)
        {
            string filename  = args[0];
            string delimiter = args[1];
            string source    = args[2];

            SymbolGraph sg = new SymbolGraph(filename, delimiter);
            Graph       G  = sg.G;

            if (!sg.Contains(source))
            {
                Console.WriteLine(source + " not in database.");
                return;
            }

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

            TextInput StdIn = new TextInput();

            while (!StdIn.IsEmpty)
            {
                string sink = StdIn.ReadLine();
                if (sg.Contains(sink))
                {
                    int t = sg.Index(sink);
                    if (bfs.HasPathTo(t))
                    {
                        foreach (int v in bfs.PathTo(t))
                        {
                            Console.WriteLine("   " + sg.Name(v));
                        }
                    }
                    else
                    {
                        Console.WriteLine("Not connected");
                    }
                }
                else
                {
                    Console.WriteLine("   Not in database.");
                }
            }
        }
Beispiel #19
0
        public static void MainTest(string[] args)
        {
            TextInput input = new TextInput(args[0]);

            int[] whitelist = input.ReadAllInts();
            input.Close();
            // sort the array
            Array.Sort(whitelist);
            TextInput StdIn = new TextInput();

            while (!StdIn.IsEmpty)
            {
                int key = StdIn.ReadInt();
                if (BinarySearch.IndexOf(whitelist, key) == -1)
                {
                    Console.WriteLine(key);
                }
            }
        }
Beispiel #20
0
        public static void MainTest(string[] args)
        {
            LinkedQueue <string> q     = new LinkedQueue <string>();
            TextInput            StdIn = new TextInput();

            while (!StdIn.IsEmpty)
            {
                string item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    q.Enqueue(item);
                }
                else if (!q.IsEmpty)
                {
                    Console.Write(q.Dequeue() + " ");
                }
            }
            Console.WriteLine("(" + q.Count + " left on queue)");
        }
Beispiel #21
0
        public static void MainTest(string[] args)
        {
            ResizingArrayStack <string> s = new ResizingArrayStack <string>();
            TextInput StdIn = new TextInput();

            while (!StdIn.IsEmpty)
            {
                string item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    s.Push(item);
                }
                else if (!s.IsEmpty)
                {
                    Console.Write(s.Pop() + " ");
                }
            }
            Console.WriteLine("(" + s.Count + " left on stack)");
        }
Beispiel #22
0
        public static void MainTest(string[] args)
        {
            TextInput      StdIn = new TextInput();
            MinPQ <string> pq    = new MinPQ <string>();

            while (!StdIn.IsEmpty)
            {
                string item = StdIn.ReadString();
                if (!item.Equals("-"))
                {
                    pq.Insert(item);
                }
                else if (!pq.IsEmpty)
                {
                    Console.Write(pq.DelMin() + " ");
                }
            }
            Console.WriteLine("(" + pq.Count + " left on pq)");
        }
Beispiel #23
0
        public static void MainTest(string[] args)
        {
            TextInput    StdIn = new TextInput();
            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);
                Console.WriteLine(p + " " + q);
            }
            Console.WriteLine(uf.Count + " components");
        }
Beispiel #24
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            int       N     = StdIn.ReadInt();

            Point2D[] points = new Point2D[N];
            for (int i = 0; i < N; i++)
            {
                int x = StdIn.ReadInt();
                int y = StdIn.ReadInt();
                points[i] = new Point2D(x, y);
            }

            GrahamScan graham = new GrahamScan(points);

            foreach (Point2D p in graham.Hull())
            {
                Console.WriteLine(p);
            }
        }
Beispiel #25
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn    = new TextInput();
            Alphabet  alphabet = new Alphabet(args[0]);
            int       R        = alphabet.Radix;

            int[] count = new int[R];
            while (StdIn.HasNextChar())
            {
                char c = StdIn.ReadChar();
                if (alphabet.Contains(c))
                {
                    count[alphabet.ToIndex(c)]++;
                }
            }
            for (int c = 0; c < R; c++)
            {
                Console.WriteLine(alphabet.ToChar(c) + " " + count[c]);
            }
        }
Beispiel #26
0
        public static void MainTest(string[] args)
        {
            TextInput input = new TextInput(args[0]);
            Graph     G     = new Graph(input);

            Cycle finder = new Cycle(G);

            if (finder.HasCycle)
            {
                foreach (int v in finder.GetCycle())
                {
                    Console.Write(v + " ");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("Graph is acyclic");
            }
        }
Beispiel #27
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            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
            Console.WriteLine("Average is " + average);
        }
Beispiel #28
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            // number of jobs
            int N = StdIn.ReadInt();

            // source and sink
            int source = 2 * N;
            int sink   = 2 * N + 1;

            // build network
            EdgeWeightedDigraph G = new EdgeWeightedDigraph(2 * N + 2);

            for (int i = 0; i < N; i++)
            {
                double duration = StdIn.ReadDouble();
                G.AddEdge(new DirectedEdge(source, i, 0.0));
                G.AddEdge(new DirectedEdge(i + N, sink, 0.0));
                G.AddEdge(new DirectedEdge(i, i + N, duration));

                // precedence constraints
                int M = StdIn.ReadInt();
                for (int j = 0; j < M; j++)
                {
                    int precedent = StdIn.ReadInt();
                    G.AddEdge(new DirectedEdge(N + i, precedent, 0.0));
                }
            }

            // compute longest path
            AcyclicLP lp = new AcyclicLP(G, source);

            // print results
            Console.WriteLine(" job   start  finish");
            Console.WriteLine("--------------------");
            for (int i = 0; i < N; i++)
            {
                Console.Write("{0,4} {1,7:F1} {2,7:F1}\n", i, lp.DistTo(i), lp.DistTo(i + N));
            }
            Console.Write("Finish time: {0,7:F1}\n", lp.DistTo(sink));
        }
Beispiel #29
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();

            SET <string> set = new SET <string>();

            // read in strings and add to set
            while (!StdIn.IsEmpty)
            {
                string key = StdIn.ReadString();
                if (key.Equals(""))
                {
                    continue;
                }
                if (!set.Contains(key))
                {
                    set.Add(key);
                    Console.WriteLine(key);
                }
            }
        }
Beispiel #30
0
        public static void MainTest(string[] args)
        {
            TextInput StdIn = new TextInput();
            // key = word, value = set of files containing that word
            ST <string, SET <string> > st = new ST <string, SET <string> >();

            // create inverted index of all files
            Console.WriteLine("Indexing files");
            string[] allFileNames = FileIndex.GetFileNames(args);

            foreach (string filename in allFileNames)
            {
                Console.WriteLine("  " + filename);
                TextInput input = new TextInput(filename);
                while (!input.IsEmpty)
                {
                    string word = input.ReadString();
                    if (!st.Contains(word))
                    {
                        st[word] = new SET <string>();
                    }
                    SET <string> set = st[word];
                    set.Add(filename);
                }
            }

            // read queries from standard input, one per line
            while (!StdIn.IsEmpty)
            {
                string query = StdIn.ReadString();
                if (st.Contains(query))
                {
                    SET <string> set = st[query];
                    foreach (string filename in set)
                    {
                        Console.WriteLine("  " + filename);
                    }
                }
            }
        }