Last active
March 12, 2025 14:15
-
-
Save nsylvestre/c8127b15424553b0fcc8d09dda1b7df1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Die { | |
| const int NumberOfSides = 6; | |
| public int Id { get; set; } | |
| public int Value { get; set; } | |
| public Die(int id) { | |
| Id = id; | |
| Value = 0; | |
| } | |
| public void Roll(Random rnd) { | |
| Value = rnd.Next(1, NumberOfSides + 1); | |
| } | |
| public static Die operator +(Die d1, Die d2) { | |
| Die d = new Die(0); | |
| d.Value = d1.Value + d2.Value; | |
| return d; | |
| } | |
| public override string ToString() { | |
| return $"{Value} "; | |
| } | |
| } |
This file contains hidden or 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
| var setOfDie = from die in Dice | |
| group die by die.Value into g | |
| select new { | |
| Value = g.Key, | |
| Count = g.Count(), | |
| Score = g.Key * g.Count() | |
| } into g2 | |
| orderby g2.Value | |
| select g2; | |
| //Alternative | |
| var setOfDie2 = Dice.GroupBy(die => die.Value).Select(die => { | |
| return new { | |
| Value = die.Key, | |
| Count = die.Count(), | |
| Score = die.Key * die.Count() | |
| }; | |
| }).OrderBy(Die => Die.Value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment