Skip to content

Instantly share code, notes, and snippets.

@beliy26
Created August 18, 2014 15:04
Show Gist options
  • Save beliy26/7c68d03416c271497d11 to your computer and use it in GitHub Desktop.
Save beliy26/7c68d03416c271497d11 to your computer and use it in GitHub Desktop.
Test task
using System;
using System.IO;
using System.Linq;
namespace TestApplication
{
class Program
{
static void Main(string[] args)
{
if (!args.Any())
{
Console.WriteLine("Error! Invalid command argument");
Console.ReadKey();
}
else
{
var filePath = args.First();
if (!File.Exists(filePath))
{
Console.WriteLine("Error! File does not exist!");
Console.ReadKey();
}
else
{
var allWords = File.ReadAllText(filePath)
.Split(new[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
if (!allWords.Any())
{
Console.WriteLine("Error! File does not contains words!");
Console.ReadKey();
}
else
{
foreach (var x in allWords.Distinct(StringComparer.CurrentCultureIgnoreCase).Select(word => new
{
Word = word.ToLower(),
Count = allWords.Count(w => w.Equals(word,StringComparison.CurrentCultureIgnoreCase))
}).OrderByDescending(w => w.Count).ThenBy(w=>w.Word).Take(20))
{
Console.WriteLine("{0} {1}", x.Word, x.Count);
}
Console.ReadKey();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment