Created
August 18, 2014 15:04
-
-
Save beliy26/7c68d03416c271497d11 to your computer and use it in GitHub Desktop.
Test task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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