Skip to content

Add LINQ shuffling algorithm #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Algorithms.Tests/Shufflers/LINQShufflerTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>();
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<int>();
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<int>();
var (correctArray, testArray) = RandomHelper.GetArrays(n);

// Act
shuffle.Shuffle(testArray, seed);
shuffle.Shuffle(correctArray, seed);

// Assert
correctArray.Should().BeEquivalentTo(testArray, options => options.WithStrictOrdering());
}
}
}
30 changes: 30 additions & 0 deletions Algorithms/Shufflers/LINQShuffler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Shufflers
{
/// <summary>
/// LINQ Shuffle is a simple shuffling algorithm,
/// where the elements within a collection are shuffled using
/// LINQ queries and lambda expressions in C#.
/// </summary>
/// <typeparam name="T">Type array input.</typeparam>
public class LINQShuffler<T> : IShuffler<T>

Check notice on line 15 in Algorithms/Shufflers/LINQShuffler.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Shufflers/LINQShuffler.cs#L15

Rename class 'LINQShuffler' to match pascal case naming rules, consider using 'LinqShuffler'.
{
/// <summary>
/// First, it will generate a random value for each element.
/// Next, it will sort the elements based on these generated
/// random numbers using OrderBy.
/// </summary>
/// <param name="array">Array to shuffle.</param>
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
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();

Check warning on line 27 in Algorithms/Shufflers/LINQShuffler.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Algorithms/Shufflers/LINQShuffler.cs#L27

Remove this useless assignment to local variable 'array'.
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down