Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4.  
  5. namespace Autocomplete
  6. {
  7.     internal class AutocompleteTask
  8.     {
  9.         public static string FindFirstByPrefix(IReadOnlyList<string> phrases, string prefix)
  10.         {
  11.             var index = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1;
  12.             if (index < phrases.Count)
  13.                 return phrases[index];
  14.  
  15.             return null;
  16.         }
  17.  
  18.         public static string[] GetTopByPrefix(IReadOnlyList<string> phrases, string prefix, int count)
  19.         {
  20.             if (FindFirstByPrefix(phrases, prefix) == null)
  21.             {
  22.                 return new string[0];
  23.             }
  24.             var topByPrefix = new List<string>();
  25.             var leftBorder = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count);
  26.             var wordCount = 0;
  27.             while (wordCount < count
  28.                    && leftBorder + wordCount + 1 < phrases.Count
  29.                    && phrases[leftBorder + wordCount + 1].StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  30.             {
  31.                 topByPrefix.Add(phrases[leftBorder + wordCount + 1]);
  32.                 wordCount++;
  33.             }
  34.             return topByPrefix.ToArray();
  35.         }
  36.  
  37.         public static int GetCountByPrefix(IReadOnlyList<string> phrases, string prefix)
  38.         {
  39.             var rightBorder = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count);
  40.             var leftBorder = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count);
  41.             return rightBorder - leftBorder - 1;
  42.         }
  43.     }
  44.  
  45.     [TestFixture]
  46.     public class AutocompleteTests
  47.     {
  48.         [Test]
  49.         public void TopByPrefix_IsEmpty_WhenNoPhrases()
  50.         {
  51.             var receivedTopWords = AutocompleteTask.GetTopByPrefix(new string[] { }, "a", 10);
  52.             CollectionAssert.IsEmpty(receivedTopWords);
  53.         }
  54.  
  55.         [Test]
  56.         public void TopByPrefix_IsNoEmpty_WhenExistPhrases()
  57.         {
  58.             var receivedTopWords = AutocompleteTask.GetTopByPrefix(new string[] { "a", "ab", "abc", "abcd" }, "c", 10);
  59.             CollectionAssert.IsEmpty(receivedTopWords);
  60.         }
  61.        
  62.         [Test]
  63.         public void CountByPrefix_IsTotalCount_WhenEmptyPrefix()
  64.         {
  65.             var receivedlCount = AutocompleteTask.GetCountByPrefix(new string[] { "a", "ab", "abc", "abcd" }, "");
  66.             Assert.AreEqual(4, receivedlCount);
  67.         }
  68.  
  69.         [Test]
  70.         public void CountByPrefix_IsTotalCount_WhenNoEmptyPrefix()
  71.         {
  72.             var receivedlCount = AutocompleteTask.GetCountByPrefix(new string[] { "a", "bb", "bbc", "abcd" }, "b");
  73.             Assert.AreEqual(2, receivedlCount);
  74.         }
  75.  
  76.         [Test]
  77.         public void TopByPrefix_OneRecommendedWord()
  78.         {
  79.             var receivedlCount = AutocompleteTask.GetTopByPrefix(new string[] { "abc", "abcd", "adb" }, "adb", 10);
  80.             CollectionAssert.AreEqual(new string[] { "adb" }, receivedlCount);
  81.         }
  82.        
  83.         [Test]
  84.         public void TopByPrefix_IsEmpty()
  85.         {
  86.             var receivedTopWords = AutocompleteTask.GetTopByPrefix(new string[] { "abc", "abcd", "adb" }, "d", 10);
  87.             CollectionAssert.IsEmpty(receivedTopWords);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement