Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using NUnit.Framework;
- namespace Autocomplete
- {
- internal class AutocompleteTask
- {
- public static string FindFirstByPrefix(IReadOnlyList<string> phrases, string prefix)
- {
- var index = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1;
- if (index < phrases.Count && phrases[index].StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
- return phrases[index];
- return null;
- }
- public static string[] GetTopByPrefix(IReadOnlyList<string> phrases, string prefix, int count)
- {
- if (FindFirstByPrefix(phrases, prefix) == null)
- {
- return new string[0];
- }
- var wordCount = Math.Min(count, GetCountByPrefix(phrases, prefix));
- var topByPrefix = new string[wordCount];
- var leftBorder = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count);
- for (var i = 0; i < wordCount; i++)
- {
- topByPrefix[i] = phrases[leftBorder + 1 + i];
- }
- return topByPrefix;
- }
- public static int GetCountByPrefix(IReadOnlyList<string> phrases, string prefix)
- {
- return RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count)
- - LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) - 1;
- }
- }
- [TestFixture]
- public class AutocompleteTests
- {
- [Test]
- public void TopByPrefix_IsEmpty_WhenNoPhrases()
- {
- // ...
- //CollectionAssert.IsEmpty(actualTopWords);
- }
- // ...
- [Test]
- public void CountByPrefix_IsTotalCount_WhenEmptyPrefix()
- {
- // ...
- //Assert.AreEqual(expectedCount, actualCount);
- }
- // ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement