void GPA(string st) { ISymbolTable <String, Double> grades = GetGrades(st); var values = new[] { grades.Get("A-"), grades.Get("A+"), grades.Get("B-"), grades.Get("B+") }; Assert.Equal(3.5, values.Average()); }
/// <summary> /// https://algs4.cs.princeton.edu/34hash/tinyST.txt /// </summary> /// <param name="symbolTableType"></param> void FrequencyCounter(string symbolTableType) { ISymbolTable <string, int> st = Factory <string, int>(symbolTableType); var strings = new string[] { "S", "E", "A", "R", "C", "H", "E", "X", "A", "M", "P", "L", "E" }; FrequencyCounter(st, strings); Assert.Equal(2, st.Get("A")); Assert.Equal(3, st.Get("E")); }
//--- return a required symbol; looks through nested scopes if necessary. public ISymbol Get(String name) { ISymbol s = symbols.Get(name); if (s != null || outer == null) { return(s); // found or there is no enclosing scope. } else { return(outer.Get(name)); // search in outer scope. } } // end Get method.
// a symbol-table client that finds the number of occurrences of each string // (having at least as many characters as a given threshold length) // in a sequence of strings from standard input, // then iterates through the keys to find the one that occurs the most frequently static void FrequencyCounter(ISymbolTable <string, int> st, IEnumerable <string> strings) { foreach (string s in strings) { if (st.Contains(s)) { st.Put(s, 1 + st.Get(s)); } else { st.Put(s, 1); } } }
public void PutTest() { var expected = 8; var actual = _symbolTable.Get("a"); Assert.AreEqual(expected, actual); expected = 4; actual = _symbolTable.Get("by"); Assert.AreEqual(expected, actual); expected = 6; actual = _symbolTable.Get("sea"); Assert.AreEqual(expected, actual); expected = 1; actual = _symbolTable.Get("sells"); Assert.AreEqual(expected, actual); expected = 0; actual = _symbolTable.Get("she"); Assert.AreEqual(expected, actual); expected = 3; actual = _symbolTable.Get("shells"); Assert.AreEqual(expected, actual); expected = 7; actual = _symbolTable.Get("shore"); Assert.AreEqual(expected, actual); expected = 5; actual = _symbolTable.Get("the"); Assert.AreEqual(expected, actual); }