-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
61 lines (53 loc) · 1.89 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
namespace AnagramsDemo
{
/// <summary>Interaction logic for MainWindow.xaml</summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
input.Text = string.Join(Environment.NewLine,
"enlist",
"skins",
"inlets",
"fresher",
"boaters",
"listen",
"boaster",
"silent",
"borates",
"tac",
"refresh",
"sinks",
"knits",
"stink",
"sort",
"cat",
"rots"
);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
output.Clear();
//Collect anagrams
IEnumerable<IGrouping<int, IGrouping<string, string>>> anagramsOccurences =
CollectAndGroupAnagrams(Regex.Split(input.Text, @"\r?\n").Where(str => !string.IsNullOrWhiteSpace(str)));
//Then decide what to do with the collection
output.Text =
string.Join(Environment.NewLine, anagramsOccurences.OrderByDescending(i => i.Key).Select(anagrams => //order by number of occurences descending
string.Join(Environment.NewLine, anagrams.OrderByDescending(j => j.Key.Length).Select(words => //order by length of word descending
string.Join(" ", words.OrderBy(word => word)))))); //order words ascending
}
private IEnumerable<IGrouping<string, string>> CollectAnagrams(IEnumerable<string> words) => words
.GroupBy(w => string.Concat(w.OrderBy(x => x))); //This will group all anagrams by sorted characters in words
private IEnumerable<IGrouping<int, IGrouping<string, string>>> GroupAnagrams(IEnumerable<IGrouping<string, string>> anagrams) => anagrams
.GroupBy(i => i.Count()); //This will group by number of occurencces of words
private IEnumerable<IGrouping<int, IGrouping<string, string>>> CollectAndGroupAnagrams(IEnumerable<string> words)
=> GroupAnagrams(CollectAnagrams(words));
}
}