Skip to content

Instantly share code, notes, and snippets.

@jrutley
Created August 7, 2019 01:04
Show Gist options
  • Save jrutley/4c57d4254980b3ccf357c933fa67fd41 to your computer and use it in GitHub Desktop.
Save jrutley/4c57d4254980b3ccf357c933fa67fd41 to your computer and use it in GitHub Desktop.
ArrayFlattener
See the full project at
https://github.com/jrutley/int-array-flattener
for csproj file and compilation instructions
using System;
using System.Collections.Generic;
using System.Linq;
namespace ArrayFlattener
{
public class Flattener
{
/// <summary>
/// Flatten takes a nested array of integers and flattens them out into a single flat array of integers
/// Any non-integer values present in the array will cause an InvalidOperationException to be thrown
/// </summary>
/// <param name="arrayOfInts">Dynamic array containing integers or arrays of integers</param>
/// <returns>Array of flattened ints</returns>
public int[] Flatten(dynamic arrayOfInts)
{
var result = new List<int>();
foreach(var el in arrayOfInts)
{
if (el is int)
{
result.Add((int)el);
}
else if(el is Array)
{
result.AddRange(Flatten(el));
}
else
{
throw new InvalidOperationException($"\"{el}\" should be either an integer or array of integer");
}
}
return result.ToArray();
}
}
}
using System;
using Xunit;
namespace ArrayFlattener.Tests
{
public class FlattenerTest
{
[Fact]
public void EmptyArray()
{
var array = new int[0];
var expected = new int[0];
var sut = new Flattener();
var result = sut.Flatten(array);
Assert.Equal(expected, result);
}
[Fact]
public void SingleElementsOnly()
{
var array = new int[]{ 1, 2, 3, 4, 5 };
var expected = new int[] { 1, 2, 3, 4, 5 };
var sut = new Flattener();
var result = sut.Flatten(array);
Assert.Equal(expected, result);
}
[Fact]
public void SingleAndMultiElements()
{
var array = new dynamic[]{ 1, 2, new[]{3, 6}, 4, 5 };
var expected = new[] {1,2,3,6,4,5};
var sut = new Flattener();
var result = sut.Flatten(array);
Assert.Equal(expected, result);
}
[Fact]
public void EmptyArrayElementsInMiddle()
{
var array = new dynamic[]{ 1, 2, new string[]{}, 4, 5 };
var expected = new[] {1,2,4,5};
var sut = new Flattener();
var result = sut.Flatten(array);
Assert.Equal(expected, result);
}
[Fact]
public void NestedElements()
{
var array = new dynamic[] { 1, 2, new dynamic[] { 3, 6, new[] { 7,7,7} }, 4, 5 };
var expected = new[] { 1, 2, 3, 6, 7, 7, 7, 4, 5 };
var sut = new Flattener();
var result = sut.Flatten(array);
Assert.Equal(expected, result);
}
[Fact]
public void NonIntElementsThrow()
{
var array = new dynamic[]{ 1, "2" };
var sut = new Flattener();
Assert.Throws<InvalidOperationException>(() => sut.Flatten(array));
}
[Fact]
// We don't want to run this one often since it adds an extra 7-8 seconds to the test run
public void LargeNumberOfElementsGetsProcessed()
{
var expected = new int[10000000];
var sut = new Flattener();
var result = sut.Flatten(expected);
Assert.Equal(expected, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment