Skip to content

Instantly share code, notes, and snippets.

@RajeevNakka
Last active July 4, 2019 18:15
Show Gist options
  • Save RajeevNakka/fc8508e70ac0a3f58498dc15aa257525 to your computer and use it in GitHub Desktop.
Save RajeevNakka/fc8508e70ac0a3f58498dc15aa257525 to your computer and use it in GitHub Desktop.
Permutations
using System;
using System.Collections.Generic;
using System.Linq;
namespace CSharpRough
{
class Program
{
static void Main(string[] args)
{
List<ArrayDimensions> dimensions = new List<ArrayDimensions>()
{
new ArrayDimensions(){StartIndex = 1, Count = 9},
new ArrayDimensions(){StartIndex = -5, Count = 9},
new ArrayDimensions(){StartIndex = 5, Count = 3}
};
foreach (var indices in GetIndices(dimensions))
{
Console.WriteLine(string.Join(",", indices));
}
//Console.ReadKey();
}
public static IEnumerable<int> Prepend(int first, IEnumerable<int> rest)
{
yield return first;
foreach (var item in rest)
yield return item;
}
static IEnumerable<T> Singleton<T>(T first)
{
yield return first;
}
public static IEnumerable<IEnumerable<int>> GetIndices(List<ArrayDimensions> dimensions)
{
if (dimensions.Count == 0)
return Singleton(Enumerable.Empty<int>());
return from first in Enumerable.Range(dimensions.First().StartIndex, dimensions.First().Count)
from rest in GetIndices(dimensions.Skip(1).ToList())
select Prepend(first, rest);
}
}
public class ArrayDimensions
{
public int StartIndex { get; set; }
public int Count { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment