Skip to content

Instantly share code, notes, and snippets.

@sholev
Created May 9, 2016 20:15
Show Gist options
  • Save sholev/8d36b073e2adabceb3599a31802710a8 to your computer and use it in GitHub Desktop.
Save sholev/8d36b073e2adabceb3599a31802710a8 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
// http://bgcoder.com/Contests/Practice/Index/143#3
class RelevanceIndex
{
public static void Main(string[] args)
{
string keyWord = Console.ReadLine();
int numberOfParagraphs = int.Parse(Console.ReadLine());
string[] paragraphs = new string[numberOfParagraphs];
var punctuation = new string[] { ",", ".", "(", ")", ";", "-", "!", "?", " " };
for (int i = 0; i < numberOfParagraphs; i++)
{
var inputWords =
Console.ReadLine()
.Split(punctuation, StringSplitOptions.RemoveEmptyEntries)
.Select(w => w.ToUpper().Equals(keyWord.ToUpper()) ? keyWord.ToUpper() : w);
paragraphs[i] = string.Join(" ", inputWords);
}
var orderedParagraphs = paragraphs.OrderByDescending(p => CountStringOccurrences(p, keyWord.ToUpper()));
foreach (string orderedParagraph in orderedParagraphs)
{
Console.WriteLine(orderedParagraph);
}
}
private static int CountStringOccurrences(string text, string pattern)
{
int count = 0;
int i = 0;
while ((i = text.IndexOf(pattern, i)) != -1)
{
i += pattern.Length;
count++;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment