using System.Numerics;
namespace Solutions;
public static class Extensions
{
///
/// string.Join Wrapper
///
///
///
///
///
public static string Join(this IEnumerable enumerable, string delimiter = "") =>
string.Join(delimiter, enumerable);
///
/// Loop over a sequence with an optional number of times.
///
///
///
///
///
public static IEnumerable Repeat(this IEnumerable sequence, int? count = null)
{
while (count == null || count-- > 0)
// ReSharper disable once PossibleMultipleEnumeration
foreach (var item in sequence)
yield return item;
}
///
/// Increased accuracy for stopwatch based on frequency.
///
///
///
public static double ScaleMilliseconds(this Stopwatch stopwatch) =>
1_000 * stopwatch.ElapsedTicks / (double)Stopwatch.Frequency;
///
/// Given an array, it returns a rotated copy.
///
/// The two-dimensional jagged array to rotate.
public static T[][] Rotate(this T[][] array)
{
var result = new T[array[0].Length][];
for (var i = 0; i < result.Length; i++)
result[i] = new T[array.Length];
for (var i = 0; i < array.Length; i++)
for (var j = 0; j < array[i].Length; j++)
result[i][j] = array[array.Length - j - 1][i];
return result;
}
///
/// Given a jagged array, it returns a diagonally flipped copy.
///
/// The two-dimensional jagged array to flip.
public static T[][] FlipHorizontally(this IEnumerable array) =>
array.Select(x => x.Reverse().ToArray()).ToArray();
///
/// Does the include a given int?
///
///
///
///
public static bool Contains(this Range range, int i) =>
i >= range.Start.Value && i <= range.End.Value;
///
/// Does contain the entire range of ?
///
///
///
///
public static bool Contains(this Range r1, Range r2) =>
r1.Start.Value <= r2.Start.Value && r1.End.Value >= r2.End.Value;
///
/// Do and overlap?
///
///
///
///
public static bool Overlaps(this Range r1, Range r2) =>
r1.Start.Value <= r2.End.Value && r1.End.Value >= r2.Start.Value &&
r2.Start.Value <= r1.End.Value && r2.End.Value >= r1.Start.Value;
///
/// Creates a new BigInteger from a binary (Base2) string
/// Based on mjs3339's gist
///
public static BigInteger BigIntegerFromBinaryString(this string binaryValue)
{
BigInteger res = 0;
if (binaryValue.Count(b => b == '1') + binaryValue.Count(b => b == '0') != binaryValue.Length) return res;
foreach (var c in binaryValue)
{
res <<= 1;
res += c == '1' ? 1 : 0;
}
return res;
}
///
/// Generate all permutations of an Enumerable.
///
///
///
///
public static IEnumerable> Permute(this IEnumerable list)
{
var array = list as T[] ?? list.ToArray();
return array.Length == 1
? [array]
: array.SelectMany(t => Permute(array.Where(x => !x!.Equals(t))), (v, p) => p.Prepend(v));
}
///
/// Raise an integer to a given .
///
///
///
///
public static int Pow(this int i, int power)
{
var pow = (uint)power;
var ret = 1;
while (pow != 0)
{
if ((pow & 1) == 1) ret *= i;
i *= i;
pow >>= 1;
}
return ret;
}
///
/// Attach the index of each element.
///
///
///
///
public static IEnumerable> Indexed(this IEnumerable source) =>
source.Select((t, i) => new KeyValuePair(i, t));
///
/// Wrapper for KeyValuePair to enable passing delegates.
///
///
///
///
///
///
public static IEnumerable> WhereValue(
this IEnumerable> source, Func func) =>
source.Where(pair => func(pair.Value));
///
/// Compute the Hamming distance between two strings.
///
///
///
///
///
public static int HammingDistance(this string s1, string other)
{
if (s1.Length != other.Length) throw new("Strings must be equal length.");
return s1.Zip(other).Count(s => s.First != s.Second);
}
}