#1
14.04.2021 um 19:11 UhrOh, mir ist ein Fehler unterlaufen. Es muss natürlich heißen:
C#-Code

var lst2 = new List<int> { 123, 321, 456, 231, 654, 789 };

C# :: Aufgabe #372 :: Lösung #1
using System.Collections.Generic; using System.Linq; using static System.Console; var lst1 = new List<string> { "eat", "tea", "tan", "ate", "nat", "bat" }; var lst2 = new List<string> { "123", "321", "456", "231", "654", "789" }; lst1.GroupAnagrams().Print(); WriteLine(); lst2.GroupAnagrams().Print(); public static class Extensions { public static List<List<T>> GroupAnagrams<T>(this List<T> l) => new( l.Select(x => (index: l.IndexOf(x), values: string.Join("", x.ToString().OrderBy(y => y)))) .GroupBy(g => g.values).Select(z => new { lists = z.Select(x => l[x.index]).ToList() }).Select(x => x.lists)); public static void Print<T>(this List<List<T>> l) => l.ForEach(x => WriteLine($"[{string.Join(", ", x)}]")); }
Kommentare:
JKooP
Punkte: 18090
680 Aufgaben
227 Lösungen
19 Kommentare
var lst2 = new List<int> { 123, 321, 456, 231, 654, 789 };