using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArrayFunctions2
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[16];
A.Comment("Test1").LineFeed().Initialize(0, 1).RandomShuffle().ShowConsole();
return;
}
}
static class ArrayFunction
{
public static T[] Initialize<T>(this T[] Array, T Value)
{
for (int i = 0; i < Array.Count(); i++)
{
Array[i] = Value;
}
return Array;
}
public static T[] Initialize<T>(this T[] Array, T Value, dynamic Step)
{
for (int i = 0; i < Array.Count(); i++)
{
Array[i] = Value + (Step * i);
}
return Array;
}
public static T[] ShowConsole<T>(this T[] Array, int LineFeed = 8, int Pad = 2, char Sepalator = ',')
{
string ShowString = string.Format(@"{0}0, {2}{1}{3}", '{', '}', Pad, Sepalator);
for (int i = 0; i < Array.Count(); i++)
{
Console.Write(ShowString, Array[i], Sepalator);
if (i % LineFeed == (LineFeed - 1)) Console.WriteLine();
}
return Array;
}
public static T[] Filter<T>(this T[] Array, Func<T, bool> Action)
{
Queue<T> Q = new Queue<T>();
for (int i = 0; i < Array.Count(); i++)
{
if (Action(Array[i]) == true) Q.Enqueue(Array[i]);
}
return Q.ToArray();
}
public static T[] Swap<T>(this T[] Array, int PosA, int PosB)
{
T Temp = Array[PosA];
Array[PosA] = Array[PosB];
Array[PosB] = Temp;
return Array;
}
public static T[] Sort<T>(this T[] Array, Func<T, T, bool> Action)
{
for (int i = 0; i < Array.Count(); i++)
{
for (int j = i + 1; j < Array.Count(); j++)
{
if (Action(Array[i], Array[j]) == true) Array.Swap(i, j);
}
}
return Array;
}
public static T[] RandomShuffle<T>(this T[] Array)
{
Random Rnd = new Random();
for (int i = 0; i < Array.Count(); i++)
{
Array.Swap(i, Rnd.Next(0, Array.Length - 1));
}
return Array;
}
public static T[] LineFeed<T>(this T[] Array)
{
Console.WriteLine();
return Array;
}
public static T[] Comment<T>(this T[] Array, string Comment)
{
Console.Write(Comment);
return Array;
}
public static T[] MinMax<T>(this T[] Array, Func<T, T, bool> MaxAction)
{
T Max = Array[0];
T Min = Array[0];
for (int i = 0; i < Array.Count(); i++)
{
if (MaxAction(Max, Array[i]) == true) Max = Array[i];
if (MaxAction(Min, Array[i]) == false) Min = Array[i];
}
return new T[2] { Min, Max };
}
public static int Counter<T>(this T[] Array, Func<T, T, bool> Action, T Value)
{
int Count = 0;
for (int i = 0; i < Array.Count(); i++)
{
if (Action(Value, Array[i]) == true) Count++;
}
return Count;
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CnVzaW5nIFN5c3RlbS5UZXh0OwoKbmFtZXNwYWNlIEFycmF5RnVuY3Rpb25zMgp7CiAgICBjbGFzcyBQcm9ncmFtCiAgICB7CiAgICAgICAgc3RhdGljIHZvaWQgTWFpbihzdHJpbmdbXSBhcmdzKQogICAgICAgIHsKICAgICAgICAgICAgaW50W10gQSA9IG5ldyBpbnRbMTZdOwoKICAgICAgICAgICAgQS5Db21tZW50KCJUZXN0MSIpLkxpbmVGZWVkKCkuSW5pdGlhbGl6ZSgwLCAxKS5SYW5kb21TaHVmZmxlKCkuU2hvd0NvbnNvbGUoKTsKCiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICB9CgogICAgc3RhdGljIGNsYXNzIEFycmF5RnVuY3Rpb24KICAgIHsKICAgICAgICBwdWJsaWMgc3RhdGljIFRbXSBJbml0aWFsaXplPFQ+KHRoaXMgVFtdIEFycmF5LCBUIFZhbHVlKQogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBBcnJheS5Db3VudCgpOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIEFycmF5W2ldID0gVmFsdWU7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIHJldHVybiBBcnJheTsKICAgICAgICB9CgogICAgICAgIHB1YmxpYyBzdGF0aWMgVFtdIEluaXRpYWxpemU8VD4odGhpcyBUW10gQXJyYXksIFQgVmFsdWUsIGR5bmFtaWMgU3RlcCkKICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgQXJyYXkuQ291bnQoKTsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBBcnJheVtpXSA9IFZhbHVlICsgKFN0ZXAgKiBpKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgcmV0dXJuIEFycmF5OwogICAgICAgIH0KICAgICAgICBwdWJsaWMgc3RhdGljIFRbXSBTaG93Q29uc29sZTxUPih0aGlzIFRbXSBBcnJheSwgaW50IExpbmVGZWVkID0gOCwgaW50IFBhZCA9IDIsIGNoYXIgU2VwYWxhdG9yID0gJywnKQogICAgICAgIHsKICAgICAgICAgICAgc3RyaW5nIFNob3dTdHJpbmcgPSBzdHJpbmcuRm9ybWF0KEAiezB9MCwgezJ9ezF9ezN9IiwgJ3snLCAnfScsIFBhZCwgU2VwYWxhdG9yKTsKCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgQXJyYXkuQ291bnQoKTsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlKFNob3dTdHJpbmcsIEFycmF5W2ldLCBTZXBhbGF0b3IpOwogICAgICAgICAgICAgICAgaWYgKGkgJSBMaW5lRmVlZCA9PSAoTGluZUZlZWQgLSAxKSkgQ29uc29sZS5Xcml0ZUxpbmUoKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgcmV0dXJuIEFycmF5OwogICAgICAgIH0KCiAgICAgICAgcHVibGljIHN0YXRpYyBUW10gRmlsdGVyPFQ+KHRoaXMgVFtdIEFycmF5LCBGdW5jPFQsIGJvb2w+IEFjdGlvbikKICAgICAgICB7CiAgICAgICAgICAgIFF1ZXVlPFQ+IFEgPSBuZXcgUXVldWU8VD4oKTsKCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgQXJyYXkuQ291bnQoKTsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAoQWN0aW9uKEFycmF5W2ldKSA9PSB0cnVlKSBRLkVucXVldWUoQXJyYXlbaV0pOwogICAgICAgICAgICB9CgogICAgICAgICAgICByZXR1cm4gUS5Ub0FycmF5KCk7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyBzdGF0aWMgVFtdIFN3YXA8VD4odGhpcyBUW10gQXJyYXksIGludCBQb3NBLCBpbnQgUG9zQikKICAgICAgICB7CiAgICAgICAgICAgIFQgVGVtcCA9IEFycmF5W1Bvc0FdOwogICAgICAgICAgICBBcnJheVtQb3NBXSA9IEFycmF5W1Bvc0JdOwogICAgICAgICAgICBBcnJheVtQb3NCXSA9IFRlbXA7CgogICAgICAgICAgICByZXR1cm4gQXJyYXk7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyBzdGF0aWMgVFtdIFNvcnQ8VD4odGhpcyBUW10gQXJyYXksIEZ1bmM8VCwgVCwgYm9vbD4gQWN0aW9uKQogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBBcnJheS5Db3VudCgpOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSBpICsgMTsgaiA8IEFycmF5LkNvdW50KCk7IGorKykKICAgICAgICAgICAgICAgIHsKCiAgICAgICAgICAgICAgICAgICAgaWYgKEFjdGlvbihBcnJheVtpXSwgQXJyYXlbal0pID09IHRydWUpIEFycmF5LlN3YXAoaSwgaik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIHJldHVybiBBcnJheTsKICAgICAgICB9CiAgICAgICAgcHVibGljIHN0YXRpYyBUW10gUmFuZG9tU2h1ZmZsZTxUPih0aGlzIFRbXSBBcnJheSkKICAgICAgICB7CiAgICAgICAgICAgIFJhbmRvbSBSbmQgPSBuZXcgUmFuZG9tKCk7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgQXJyYXkuQ291bnQoKTsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBBcnJheS5Td2FwKGksIFJuZC5OZXh0KDAsIEFycmF5Lkxlbmd0aCAtIDEpKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgcmV0dXJuIEFycmF5OwogICAgICAgIH0KICAgICAgICBwdWJsaWMgc3RhdGljIFRbXSBMaW5lRmVlZDxUPih0aGlzIFRbXSBBcnJheSkKICAgICAgICB7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCk7CiAgICAgICAgICAgIHJldHVybiBBcnJheTsKICAgICAgICB9CiAgICAgICAgcHVibGljIHN0YXRpYyBUW10gQ29tbWVudDxUPih0aGlzIFRbXSBBcnJheSwgc3RyaW5nIENvbW1lbnQpCiAgICAgICAgewogICAgICAgICAgICBDb25zb2xlLldyaXRlKENvbW1lbnQpOwogICAgICAgICAgICByZXR1cm4gQXJyYXk7CiAgICAgICAgfQogICAgICAgIHB1YmxpYyBzdGF0aWMgVFtdIE1pbk1heDxUPih0aGlzIFRbXSBBcnJheSwgRnVuYzxULCBULCBib29sPiBNYXhBY3Rpb24pCiAgICAgICAgewogICAgICAgICAgICBUIE1heCA9IEFycmF5WzBdOwogICAgICAgICAgICBUIE1pbiA9IEFycmF5WzBdOwogICAgICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IEFycmF5LkNvdW50KCk7IGkrKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYgKE1heEFjdGlvbihNYXgsIEFycmF5W2ldKSA9PSB0cnVlKSBNYXggPSBBcnJheVtpXTsKICAgICAgICAgICAgICAgIGlmIChNYXhBY3Rpb24oTWluLCBBcnJheVtpXSkgPT0gZmFsc2UpIE1pbiA9IEFycmF5W2ldOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBuZXcgVFsyXSB7IE1pbiwgTWF4IH07CiAgICAgICAgfQogICAgICAgIHB1YmxpYyBzdGF0aWMgaW50IENvdW50ZXI8VD4odGhpcyBUW10gQXJyYXksIEZ1bmM8VCwgVCwgYm9vbD4gQWN0aW9uLCBUIFZhbHVlKQogICAgICAgIHsKICAgICAgICAgICAgaW50IENvdW50ID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBBcnJheS5Db3VudCgpOyBpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmIChBY3Rpb24oVmFsdWUsIEFycmF5W2ldKSA9PSB0cnVlKSBDb3VudCsrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHJldHVybiBDb3VudDsKICAgICAgICB9CiAgICB9Cn0=