Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework;
  5.  
  6. namespace Autocomplete
  7. {
  8.     internal class AutocompleteTask
  9.     {
  10.         public static string FindFirstByPrefix(IReadOnlyList<string> phrases, string prefix)
  11.         {
  12.             var index = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1;
  13.             if (index < phrases.Count && phrases[index].StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
  14.                 return phrases[index];
  15.            
  16.             return null;
  17.         }
  18.  
  19.         public static string[] GetTopByPrefix(IReadOnlyList<string> phrases, string prefix, int count)
  20.         {
  21.             if (FindFirstByPrefix(phrases, prefix) == null)
  22.             {
  23.                 return new string[0];
  24.             }
  25.             var wordCount = Math.Min(count, GetCountByPrefix(phrases, prefix));
  26.             var topByPrefix = new string[wordCount];
  27.             var leftBorder = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count);
  28.             for (var i = 0; i < wordCount; i++)
  29.             {
  30.                 topByPrefix[i] = phrases[leftBorder + 1 + i];
  31.             }
  32.             return topByPrefix;
  33.         }
  34.  
  35.         public static int GetCountByPrefix(IReadOnlyList<string> phrases, string prefix)
  36.         {
  37.             return RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count)
  38.                    - LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) - 1;
  39.         }
  40.     }
  41.  
  42.     [TestFixture]
  43.     public class AutocompleteTests
  44.     {
  45.         [Test]
  46.         public void TopByPrefix_IsEmpty_WhenNoPhrases()
  47.         {
  48.             // ...
  49.             //CollectionAssert.IsEmpty(actualTopWords);
  50.         }
  51.  
  52.         // ...
  53.  
  54.         [Test]
  55.         public void CountByPrefix_IsTotalCount_WhenEmptyPrefix()
  56.         {
  57.             // ...
  58.             //Assert.AreEqual(expectedCount, actualCount);
  59.         }
  60.  
  61.         // ...
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement