public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { var leftBoarder = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); var rightBoarder = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); return(rightBoarder - leftBoarder - 1); }
public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { int left = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); int right = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); return(right - left - 1); }
/// <returns> /// Возвращает количество фраз, начинающихся с заданного префикса /// </returns> public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { var count = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count) - LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) - 1; return(Math.Max(0, count)); }
public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { var end = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); var start = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); return(end - start - 1); }
/// <returns> /// Возвращает количество фраз, начинающихся с заданного префикса /// </returns> public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { // тут стоит использовать написанные ранее классы LeftBorderTask и RightBorderTask var first = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1; var last = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count) - 1; return(Math.Max(0, last - first + 1)); }
/// <returns> /// Возвращает количество фраз, начинающихся с заданного префикса /// </returns> public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { int leftIndex = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); int rightIndex = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); // тут стоит использовать написанные ранее классы LeftBorderTask и RightBorderTask return(rightIndex - leftIndex - 1); }
/// <returns> /// Возвращает количество фраз, начинающихся с заданного префикса /// </returns> public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { // тут стоит использовать написанные ранее классы LeftBorderTask и RightBorderTask var startIndex = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1; var endIndex = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); return(endIndex - startIndex); }
public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { var left = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); var right = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); var result = right - left - 1; return((result <= 0) ? 0 : result); }
public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { if (prefix == "") { return(phrases.Count); } return(RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count) - LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) - 1); }
public void TopByPrefix_IsEmpty_WhenNoPhrases() { var phrases = new List <string>() { "ab", "ab", "ab", "ab" }; IReadOnlyList <string> read = phrases; Assert.AreEqual(RightBorderTask.GetRightBorderIndex(read, "aa", -1, 4), 2); //CollectionAssert.IsEmpty(actualTopWords); }
/// <returns> /// Возвращает первые в лексикографическом порядке count (или меньше, если их меньше count) /// элементов словаря, начинающихся с prefix. /// </returns> /// <remarks>Эта функция должна работать за O(log(n) + count)</remarks> public static string[] GetTopByPrefix(IReadOnlyList <string> phrases, string prefix, int count) { var begin = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1; var end = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count) - 1; List <string> goodPhrases = new List <string>(); for (int i = begin; i <= end && i < begin + count; i++) { goodPhrases.Add(phrases[i]); } return(goodPhrases.ToArray()); }
/// <returns> /// Возвращает количество фраз, начинающихся с заданного префикса /// </returns> public static int GetCountByPrefix(IReadOnlyList <string> phrases, string prefix) { var right = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); var left = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); if (right - left - 1 < 0) { return(0); } else { return(right - left - 1); } }
/// <returns> /// Возвращает первые в лексикографическом порядке count (или меньше, если их меньше count) /// элементов словаря, начинающихся с prefix. /// </returns> /// <remarks>Эта функция должна работать за O(log(n) + count)</remarks> public static string[] GetTopByPrefix(IReadOnlyList <string> phrases, string prefix, int count) { // паста из GetCountByPrefix(...) в целях уложиться в требования к O(...) var first = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count) + 1; var last = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count) - 1; count = Math.Min(count, Math.Max(0, last - first + 1)); var ret = new string[count]; for (var i = 0; i < count; ++i) { ret[i] = phrases[first + i]; } return(ret); }
public static string[] GetTopByPrefix(IReadOnlyList <string> phrases, string prefix, int count) { var leftIndex = LeftBorderTask.GetLeftBorderIndex(phrases, prefix, -1, phrases.Count); var rightIndex = RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Count); count = Math.Min(count, rightIndex - leftIndex - 1); var topByPrefix = new string[count]; for (var i = 0; i < count; i++) { topByPrefix[i] = phrases[leftIndex + i + 1]; } return(topByPrefix); }
public void CheckRightBorder(string[] phrases, string prefix, int expectedIndex) { Assert.AreEqual(expectedIndex, RightBorderTask.GetRightBorderIndex(phrases, prefix, -1, phrases.Length)); }