Created
July 3, 2015 15:26
-
-
Save wimplash/4ae2f14c89f743513cb2 to your computer and use it in GitHub Desktop.
A solution for challenge #221 from /r/dailyprogrammer
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.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
namespace PoemFinder | |
{ | |
internal static class PoemFinder | |
{ | |
private static void Main(string[] args) | |
{ | |
const string wordListUrl = "http://www-01.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt"; | |
// a hash-based list containing only distinct, lower-cased words with three or more letters | |
var wordList = ReadLines(() => new WebClient().OpenRead(wordListUrl)); | |
var uniqueWordList = | |
new HashSet<string>(wordList.Where(w => w.Length > 2).Select(w => w.ToLower())); | |
const string challengeInputUrl = "https://gist.githubusercontent.com/anonymous/c8fb349e9ae4fcb40cb5/raw/05a1ef03626057e1b57b5bbdddc4c2373ce4b465/challenge.txt"; | |
var challengeInputLines = ReadLines(() => new WebClient().OpenRead(challengeInputUrl)); | |
var challengeOutputLines = challengeInputLines.Where(line => | |
{ | |
// A list of the lower-cased version of all words with three or more letters in the line | |
var candidateWordList = line.Split(' ').Where(w => w.Length > 2).Select(w => w.ToLower()).ToList(); | |
var candidateWordCount = candidateWordList.Count(); | |
// count all of the words in the line which are also in uniqueWordList | |
var hitCount = candidateWordList.Where(word => uniqueWordList.Contains(word)).ToList().Count; | |
// if the ratio of hits to words is high enough... | |
return (float) hitCount/candidateWordCount > .7; | |
}); | |
challengeOutputLines.ToList().ForEach(line => Console.WriteLine(line)); | |
} | |
// Thanks JonSkeet: http://stackoverflow.com/a/13312954/12604 | |
private static IEnumerable<string> ReadLines(Func<Stream> streamProvider) | |
{ | |
using (var stream = streamProvider()) | |
using (var reader = new StreamReader(stream, Encoding.UTF8)) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
yield return line; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment