Skip to content

Instantly share code, notes, and snippets.

@Spongman
Last active June 6, 2017 06:25
Show Gist options
  • Save Spongman/a8bd9c9d360a3a2ead0e554618ddc5a2 to your computer and use it in GitHub Desktop.
Save Spongman/a8bd9c9d360a3a2ead0e554618ddc5a2 to your computer and use it in GitHub Desktop.
HackerRank utils
using System.Linq;
public static class U {
public static string Join<T>(this IEnumerable<T> rg, string sep = " ") => string.Join(sep, rg.Select(o => o.ToString()));
public static int ReadInt() => int.Parse(Console.ReadLine());
public static IEnumerable<int> ParseInts() => Console.ReadLine().Split(' ').Select(int.Parse);
public static int[] ReadInts() => ParseInts().ToArray();
public static IEnumerable<long> ParseLongs() => Console.ReadLine().Split(' ').Select(long.Parse);
public static long[] ReadLongs() => ParseLongs().ToArray();
public static IEnumerable<T> Order<T>(this IEnumerable<T> @this) where T : IComparable<T> => @this.OrderBy((T t) => t);
public static IEnumerable<T> OrderDescending<T>(this IEnumerable<T> @this) where T : IComparable<T> => @this.OrderByDescending((T t) => t);
public static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs; rhs = temp; }
public static bool All(this IEnumerable<bool> @this) => @this.All(b=>b);
public static bool Any(this IEnumerable<bool> @this) => @this.Any(b=>b);
public static bool NextPermutation<T>(this T[] a) where T: IComparable
{
int k = a.Length - 2;
while (k >= 0 && a[k].CompareTo(a[k+1])>=0) k--;
if(k<0) return false;
int l = a.Length - 1;
while (l > k && a[l].CompareTo(a[k]) <= 0) l--;
U.Swap(ref a[k], ref a[l]);
for (int i = k + 1, j = a.Length - 1; i < j; i++,j--)
U.Swap(ref a[i], ref a[j]);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment