Skip to content

Instantly share code, notes, and snippets.

@jonnii
Created September 16, 2013 13:24
Show Gist options
  • Save jonnii/6580639 to your computer and use it in GitHub Desktop.
Save jonnii/6580639 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace AyendeTest
{
[TestFixture]
public class Class1
{
[Test]
public void Should()
{
var inputs = new[] { 1, 59, 12, 43, 4, 58, 5, 13, 46, 3, 6, 8, 7 };
// sorted list of linked lists which contains all of runs
var runs = new SortedList<int, LinkedList<int>>();
// dictionary of previous and next expected inputs
var prevs = new Dictionary<int, LinkedList<int>>();
var nexts = new Dictionary<int, LinkedList<int>>();
foreach (var input in inputs)
{
if (input == 7)
{
Console.WriteLine();
}
LinkedList<int> run;
// see if this is an expected next
if (nexts.TryGetValue(input, out run))
{
run.AddLast(input);
nexts.Remove(input);
// special case, we have a next chain which wants us
LinkedList<int> nextInChain;
if (prevs.TryGetValue(input, out nextInChain))
{
//continue;
}
nexts.Add(input + 1, run);
continue;
}
// see if this is an expected prev
if (prevs.TryGetValue(input, out run))
{
run.AddFirst(input);
prevs.Remove(input);
prevs.Add(input - 1, run);
continue;
}
// if not start a new run
run = new LinkedList<int>(new[] { input });
runs.Add(input, run);
prevs.Add(input - 1, run);
nexts.Add(input + 1, run);
}
foreach (var run in runs)
{
var formatted = string.Join(",", run.Value);
Console.WriteLine(formatted);
Console.WriteLine("===========");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment