Skip to content

Instantly share code, notes, and snippets.

@mpoullet
Forked from PaulWild/AnagramSorter.cs
Created February 2, 2018 13:25
Show Gist options
  • Save mpoullet/621958761d71443819fdd73fc7906494 to your computer and use it in GitHub Desktop.
Save mpoullet/621958761d71443819fdd73fc7906494 to your computer and use it in GitHub Desktop.
Prime Number anagram sorter
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