init
This commit is contained in:
78
SecureBank.Extensions/IEnumerableExtensions.cs
Normal file
78
SecureBank.Extensions/IEnumerableExtensions.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace SecureBank.Extensions
|
||||
{
|
||||
public static class IEnumerableExtensions
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public static IEnumerable<IEnumerable<T>> GetCombinations<T>(this IEnumerable<T> elements, int n)
|
||||
{
|
||||
if (elements.Count() < n)
|
||||
{
|
||||
throw new ArgumentException("Array length can't be less than number of selected elements");
|
||||
}
|
||||
|
||||
if (n < 1)
|
||||
{
|
||||
throw new ArgumentException("Number of selected elements can't be less than 1");
|
||||
}
|
||||
|
||||
if (elements.Count() == n)
|
||||
{
|
||||
return new List<IEnumerable<T>> { elements.ToList() };
|
||||
}
|
||||
|
||||
List<List<T>> result = new List<List<T>>();
|
||||
|
||||
int[] indices = Enumerable.Range(0, n).ToArray();
|
||||
|
||||
while (indices[0] <= elements.Count() - n)
|
||||
{
|
||||
List<T> combination = new List<T>();
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
combination.Add(elements.ElementAt(indices[i]));
|
||||
}
|
||||
|
||||
result.Add(combination);
|
||||
|
||||
int j = n - 1;
|
||||
while (j >= 0 && indices[j] == elements.Count() - n + j)
|
||||
{
|
||||
j--;
|
||||
}
|
||||
|
||||
if (j < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
indices[j]++;
|
||||
for (int k = j + 1; k < n; k++)
|
||||
{
|
||||
indices[k] = indices[k - 1] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
25
SecureBank.Extensions/IListExtensions.cs
Normal file
25
SecureBank.Extensions/IListExtensions.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Extensions
|
||||
{
|
||||
public static class IListExtensions
|
||||
{
|
||||
public static void Shuffle<T>(this IList<T> list)
|
||||
{
|
||||
Random rng = Random.Shared;
|
||||
int n = list.Count;
|
||||
while (n > 1)
|
||||
{
|
||||
n--;
|
||||
int k = rng.Next(n + 1);
|
||||
T value = list[k];
|
||||
list[k] = list[n];
|
||||
list[n] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
SecureBank.Extensions/SecureBank.Extensions.csproj
Normal file
9
SecureBank.Extensions/SecureBank.Extensions.csproj
Normal file
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
31
SecureBank.Extensions/StringExtensions.cs
Normal file
31
SecureBank.Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecureBank.Extensions
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string CreateRandom(int length) => CreateRandom(length, "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890`~!@#$%^&*()-_=+[{]};:'\"\\|,<.>/?");
|
||||
public static string CreateRandom(int length, IEnumerable<char> characters) => new string(Enumerable.Repeat(characters, length).Select(s => s.ElementAt(Random.Shared.Next(s.Count()))).ToArray());
|
||||
|
||||
public static string Shuffle(this string str)
|
||||
{
|
||||
char[] array = str.ToCharArray();
|
||||
Random rng = Random.Shared;
|
||||
int n = array.Length;
|
||||
while (n > 1)
|
||||
{
|
||||
n--;
|
||||
int k = rng.Next(n + 1);
|
||||
char value = array[k];
|
||||
array[k] = array[n];
|
||||
array[n] = value;
|
||||
}
|
||||
return new string(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user