Beispiel #1
0
        public void SequentialSearchSTTest1()
        {
            // testing Get/Put semantics
            SequentialSearchST <string, int> st = new SequentialSearchST <string, int>();

            Assert.IsTrue(st.IsEmpty);

            // making sure we can delete all from ST
            st.Put("asd", 355);
            st.Put("dsd", 25);
            st.Put("esd", 15);
            st.Delete("esd");
            st.Delete("dsd");
            st.Delete("asd");
            Assert.IsTrue(st.IsEmpty);

            string[] keys = { "to", "be", "or", "not", "to", "be", "is", "quest" };
            for (int i = 0; i < keys.Length; i++)
            {
                st.Put(keys[i], i);
            }
            Assert.IsTrue(!(st.IsEmpty) && (st.Count == 6));

            string key = "not";

            Assert.IsTrue(st.Contains(key));
            st.Delete(key);
            Assert.IsFalse(st.Contains(key));
            Assert.IsNull(st.Get(key));

            object value = st.Get("is");

            Assert.AreEqual(6, value);

            value = st.Get("world");
            Assert.IsNull(value);

            int dummy = (int)st.Get("hello"); // generate null exception
        }
Beispiel #2
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyST.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in       = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();


            var st = new SequentialSearchST <string, string>();


            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var key   = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key, value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.ReadLine();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            // 官方实现见:https://algs4.cs.princeton.edu/31elementary/SequentialSearchST.java.html
            var input = "S E A R C H E X A M P L E".Split(' ');
            var st    = new SequentialSearchST <string, int>();

            for (var i = 0; i < input.Length; i++)
            {
                st.Put(input[i], i);
            }
            foreach (var s in st.Keys())
            {
                Console.WriteLine(s + " " + st.Get(s));
            }
        }
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyST.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            var fieName = string.Empty;
            switch (fileNumber)
            {
                case "1":
                    fieName = "tinyST.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In($"Files\\Searching\\{fieName}");
            var keyValues = @in.ReadAllLines();

            //var list = words.Select(word => new StringComparable(word)).ToList();

            //var listComparable = list.Cast<IComparable>().ToList();
            //var arrayComparable = list.Cast<IComparable>().ToArray();
            //var listStrings = words.ToList();

            var st = new SequentialSearchST<string,string>();

            foreach (var keyValue in keyValues)
            {
                var splittedKeyValue = keyValue.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                var key = splittedKeyValue[0];
                var value = splittedKeyValue[1];
                st.Put(key,value);
            }
            // print results
            foreach (var item in st.Keys())
            {
                Console.WriteLine(st.Get(item));
            }
            Console.ReadLine();
        }
Beispiel #5
0
        public void TestSequentialSearchST()
        {
            SequentialSearchST<string, int> st = new SequentialSearchST<string, int>();

            int amounts = 20;
            string[] strs = new string[amounts];
            Random rand = new Random();
            for (int i = 0; i < amounts; i++)
            {
                strs[i] = Convert.ToChar(rand.Next(97, 122)).ToString();
                st.Put(strs[i], i);
            }
            StringBuilder sb0 = new StringBuilder();
            foreach (var str in strs)
                sb0.Append(str + " ");
            Debug.WriteLine(sb0.ToString());

            StringBuilder sb = new StringBuilder();
            foreach (var item in st.Keys())
                sb.Append(st.Get(item) + " ");
            Debug.WriteLine(sb.ToString());

            Assert.AreEqual(strs.Distinct().Count(), st.Keys().Count());
        }