public string BinarySearch(SortedWords words) { var methodExecutionTime = Stopwatch.StartNew(); int leftSide = 0; int rightSide = words.WordsSeperatedIntoArray.Length; while (leftSide <= rightSide) { int middle = (leftSide + rightSide) / 2; if (string.Compare(words.RandomWord, words.WordsSeperatedIntoArray[middle]) == 0) { methodExecutionTime.Stop(); TotalTimeForBinarySearch.Add(methodExecutionTime.Elapsed); Console.WriteLine($"The word {words.RandomWord} has been found at index value {middle} using Binary Search, it took {methodExecutionTime.Elapsed} to find"); return(words.RandomWord); } if (string.Compare(words.RandomWord, words.WordsSeperatedIntoArray[middle]) > 0) { leftSide = middle + 1; } else { rightSide = middle - 1; } } return(null); }
public void ExpectedLengthOfDownloadWordsArray_ExpectedLengthOfDownloadWordsArray_AreEqual() { //Arrange var words = new DownloadWords(); var expected = new SortedWords(words); //Act var actual = new SortedWords(words); //Assert Assert.AreEqual(expected.WordsSeperatedIntoArray.Length, actual.WordsSeperatedIntoArray.Length); }
public string LinearSearch(SortedWords word) { var methodExecutionTime = Stopwatch.StartNew(); for (int i = 0; i < word.WordsSeperatedIntoArray.Length; i++) { if (word.WordsSeperatedIntoArray[i] == word.RandomWord) { methodExecutionTime.Stop(); TotalTimeForLinearSearch.Add(methodExecutionTime.Elapsed); Console.WriteLine($"The Word {word.RandomWord} has been found using Linear Search, it is entry {i} in the array, it took { methodExecutionTime.Elapsed} Seconds to find"); return(word.RandomWord); } } Console.WriteLine("Sorry there is no match"); return(null); }
public static void ListWords() { TextInfo tx = CultureInfo.CurrentCulture.TextInfo; int amount = 0; bool exitLoop = false; List <string> chosenWords = new List <string>(); Func <KeyValuePair <string, int>, bool> filter = pair => String.IsNullOrEmpty(pair.Key); var filterdWords = SortedWords.Where(filter).ToList(); int wordsCount = filterdWords.Count(); do { Clear(); filterdWords = SortedWords.Where(filter).ToList(); WriteLine("Word Count % of total words"); WriteLine("-----------------------------------------"); for (int i = 0; i < wordsCount; i++) { WriteLine(tx.ToTitleCase(filterdWords[i].Key.ToLower()).PadRight(15, ' ') + filterdWords[i].Value.ToString().PadRight(15, ' ') + ((filterdWords[i].Value / SortedWords.Count) * 100)); chosenWords.Add(tx.ToTitleCase(filterdWords[i].Key.ToLower())); } WriteLine("Press 1 to sort out specified number of top words"); WriteLine("Press 2 to sort out words with count more than specified number"); WriteLine("Press 3 to display all words"); WriteLine("Press enter to save current list to txt"); WriteLine("Press escape to exit to main menu"); ConsoleKeyInfo input; bool isValidKey; do { input = ReadKey(true); isValidKey = input.Key == ConsoleKey.D1 || input.Key == ConsoleKey.D2 || input.Key == ConsoleKey.D3 || input.Key == ConsoleKey.Escape || input.Key == ConsoleKey.Enter; } while (!isValidKey); if (input.Key == ConsoleKey.D1) { Write("\nDisplay top: "); wordsCount = Convert.ToInt32(ReadLine()); filter = pair => !String.IsNullOrEmpty(pair.Key); } if (input.Key == ConsoleKey.D2) { WriteLine("\nCount more than: "); amount = Convert.ToInt32(ReadLine()); filter = pair => pair.Value > amount; } if (input.Key == ConsoleKey.D3) { filter = pair => !String.IsNullOrEmpty(pair.Key); } if (input.Key == ConsoleKey.Escape) { exitLoop = true; } if (input.Key == ConsoleKey.Enter) { TextFileProsessing.WritCurrentListToTxt(chosenWords); } } while (!exitLoop); }