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(); }
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()); }
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(); }