This is now a solution as requested in the comments of solution 3.
Speed is maybe not optimal, though...
static void Main(string[] args)
{
int[] arr = {2, 1, 1, 2, 2, 1, 3, 3, 3, 3, 3, 4, 4};
int step = 2;
var r = GetPermutations(arr, step);
Console.WriteLine("arr = {0}", string.Join(",", arr));
Console.WriteLine("step = {0}", step);
foreach (var e in r)
{
Console.WriteLine("{0}-{1} = {2}", e.Key.Item1, e.Key.Item2, e.Value);
}
}
public static Dictionary<Tuple<int, int>, int> GetPermutations(int[] arr, int step)
{
Dictionary<Tuple<int, int>, int> r = new Dictionary<Tuple<int, int>, int>();
var e1 = arr.Take(arr.Length-step).GetEnumerator();
var e2 = arr.Skip(step).GetEnumerator();
while(e1.MoveNext() & e2.MoveNext())
{
Tuple<int, int> k = new Tuple<int,int>(e1.Current, e2.Current);
if (r.ContainsKey(k)) r[k]++;
else r.Add(k, 1);
}
return r;
}
Output:
arr = 2,1,1,2,2,1,3,3,3,3,3,4,4
step = 2
2-1 = 2
1-2 = 2
2-3 = 1
1-3 = 1
3-3 = 3
3-4 = 2
Cheers
Andi
[EDIT]
...and here is the .Net2.0 Version:
Not optimal, but it works...
static void Main(string[] args)
{
int[] arr = {2, 1, 1, 2, 2, 1, 3, 3, 3, 3, 3, 4, 4};
int step = 2;
var r = GetPermutations(arr, step);
Console.WriteLine("arr = {0}",
string.Join(",",
Array.ConvertAll(arr,
delegate(int i) { return i.ToString(); })));
Console.WriteLine("step = {0}", step);
foreach (var e in r)
{
Console.WriteLine("{0}-{1} = {2}", e.Key.Key, e.Key.Value, e.Value);
}
}
public static Dictionary<KeyValuePair<int,int>,int> GetPermutations(int[] arr, int step)
{
Dictionary<KeyValuePair<int,int>,int> r =
new Dictionary<KeyValuePair<int,int>,int>();
var e1 = arr.GetEnumerator();
var e2 = arr.GetEnumerator();
while ((step > 0) && e2.MoveNext()) step--;
while(e1.MoveNext() & e2.MoveNext())
{
KeyValuePair<int, int> k =
new KeyValuePair<int, int>((int)e1.Current, (int)e2.Current);
if (r.ContainsKey(k)) r[k]++;
else r.Add(k, 1);
}
return r;
}