diff --git a/Algorithms.Tests/Shufflers/LINQShufflerTests.cs b/Algorithms.Tests/Shufflers/LINQShufflerTests.cs new file mode 100644 index 00000000..c5e1d730 --- /dev/null +++ b/Algorithms.Tests/Shufflers/LINQShufflerTests.cs @@ -0,0 +1,60 @@ +using Algorithms.Shufflers; +using Algorithms.Tests.Helpers; +using FluentAssertions; +using NUnit.Framework; +using System; + +namespace Algorithms.Tests.Shufflers +{ + public static class LINQShufflerTests + { + [Test] + public static void ArrayShuffled_NewArraySameSize( + [Random(10, 1000, 100, Distinct = true)] + int n) + { + // Arrange + var shuffler = new LINQShuffler(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + shuffler.Shuffle(testArray); + + // Assert + testArray.Length.Should().Be(correctArray.Length); + } + + [Test] + public static void ArrayShuffled_NewArraySameValues( + [Random(10, 1000, 100, Distinct = true)] + int n) + { + // Arrange + var shuffler = new LINQShuffler(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + shuffler.Shuffle(testArray); + + // Assert + testArray.Should().BeEquivalentTo(correctArray); + } + + [Test] + public static void ArrayShuffled_NewArraySameShuffle( + [Random(0, 1000, 2, Distinct = true)] int n, + [Random(1000, 10000, 5, Distinct = true)] int seed) + { + // Arrange + var shuffle = new LINQShuffler(); + var (correctArray, testArray) = RandomHelper.GetArrays(n); + + // Act + shuffle.Shuffle(testArray, seed); + shuffle.Shuffle(correctArray, seed); + + // Assert + correctArray.Should().BeEquivalentTo(testArray, options => options.WithStrictOrdering()); + } + } +} diff --git a/Algorithms/Shufflers/LINQShuffler.cs b/Algorithms/Shufflers/LINQShuffler.cs new file mode 100644 index 00000000..6445519c --- /dev/null +++ b/Algorithms/Shufflers/LINQShuffler.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Algorithms.Shufflers +{ + /// + /// LINQ Shuffle is a simple shuffling algorithm, + /// where the elements within a collection are shuffled using + /// LINQ queries and lambda expressions in C#. + /// + /// Type array input. + public class LINQShuffler : IShuffler + { + /// + /// First, it will generate a random value for each element. + /// Next, it will sort the elements based on these generated + /// random numbers using OrderBy. + /// + /// Array to shuffle. + /// Random generator seed. Used to repeat the shuffle. + public void Shuffle(T[] array, int? seed = null) + { + var random = seed is null ? new Random() : new Random(seed.Value); + array = array.OrderBy(x => random.Next()).ToArray(); + } + } +} diff --git a/README.md b/README.md index b9933f9f..c5734067 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ find more than one implementation for the same objective but using different alg * [MSD Radix Sort](./Algorithms/Sorters/String/MsdRadixStringSorter.cs) * [Shufflers](./Algorithms/Shufflers) * [Fisher-Yates Shuffler](./Algorithms/Shufflers/FisherYatesShuffler.cs) + * [LINQ Shuffler](./Algorithms/Shufflers/LINQShuffler.cs) * [Sequences](./Algorithms/Sequences) * [A000002 Kolakoski](./Algorithms/Sequences/KolakoskiSequence.cs) * [A000004 Zero](./Algorithms/Sequences/ZeroSequence.cs)