Created
January 31, 2018 23:40
-
-
Save PaulWild/a37829bedb124bcbeaf29bdc324f4985 to your computer and use it in GitHub Desktop.
Prime Number anagram sorter
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
static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var groupedWords = new Dictionary<double, List<string>>(); | |
string line; | |
using (var reader = new StreamReader("words_alpha.txt")) | |
{ | |
while ((line = reader.ReadLine()) != null) | |
{ | |
var product = line.ToPrimeMultiple(); | |
if (groupedWords.ContainsKey(product)) | |
{ | |
groupedWords[product].Add(line); | |
} | |
else | |
{ | |
groupedWords[product] = new List<string>() {line}; | |
} | |
} | |
} | |
Console.WriteLine(String.Join(" ", groupedWords.OrderByDescending(x => x.Value.Count).First().Value)); | |
Console.ReadLine(); | |
} | |
public static double ToPrimeMultiple(this string word) | |
{ | |
return word.Aggregate(1d, (agg, letter) => agg * Map.Single(x => x.letter == letter).prime); | |
} | |
private static readonly HashSet<(char letter, double prime)> Map = new HashSet<(char letter, double prime)> | |
{ | |
('a', 2), | |
('b', 3), | |
('c', 5), | |
('d', 7), | |
('e', 11), | |
('f', 13), | |
('g', 17), | |
('h', 19), | |
('i', 23), | |
('j', 29), | |
('k', 31), | |
('l', 37), | |
('m', 41), | |
('n', 43), | |
('o', 47), | |
('p', 53), | |
('q', 59), | |
('r', 61), | |
('s', 67), | |
('t', 71), | |
('u', 73), | |
('v', 79), | |
('w', 83), | |
('x', 89), | |
('y', 97), | |
('z', 101) | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment