Take a nested list and return a single flattened list with all values except nil/null.
The challenge is to write a function that accepts an arbitrarily-deep nested list-like structure and returns a flattened structure without any nil/null values.
For Example
input: [1,[2,3,null,4],[null],5]
output: [1,2,3,4,5]
To run the tests, run the command dotnet test
from within the exercise directory.
Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the Skip
property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using exercism submit FlattenArray.cs
For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.
Interview Question https://reference.wolfram.com/language/ref/Flatten.html
// This file was auto-generated based on version 1.2.0 of the canonical data.
using Xunit;
public class FlattenArrayTest
{
[Fact]
public void No_nesting()
{
var array = new object[]
{
0,
1,
2
};
var expected = new[] { 0, 1, 2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact(Skip = "Remove to run test")]
public void Flattens_array_with_just_integers_present()
{
var array = new object[]
{
1,
new object[] { 2, 3, 4, 5, 6, 7 },
8
};
var expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact(Skip = "Remove to run test")]
public void Number_5_level_nesting()
{
var array = new object[]
{
0,
2,
new object[] { new object[] { 2, 3 }, 8, 100, 4, new object[] { new object[] { new object[] { 50 } } } },
-2
};
var expected = new[] { 0, 2, 2, 3, 8, 100, 4, 50, -2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact(Skip = "Remove to run test")]
public void Number_6_level_nesting()
{
var array = new object[]
{
1,
new object[] { 2, new object[] { new object[] { 3 } }, new object[] { 4, new object[] { new object[] { 5 } } }, 6, 7 },
8
};
var expected = new[] { 1, 2, 3, 4, 5, 6, 7, 8 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact(Skip = "Remove to run test")]
public void Number_6_level_nest_list_with_null_values()
{
var array = new object[]
{
0,
2,
new object[] { new object[] { 2, 3 }, 8, new object[] { new object[] { 100 } }, null, new object[] { new object[] { null } } },
-2
};
var expected = new[] { 0, 2, 2, 3, 8, 100, -2 };
Assert.Equal(expected, FlattenArray.Flatten(array));
}
[Fact(Skip = "Remove to run test")]
public void All_values_in_nested_list_are_null()
{
var array = new object[]
{
null,
new object[] { new object[] { new object[] { null } } },
null,
null,
new object[] { new object[] { null, null }, null },
null
};
Assert.Empty(FlattenArray.Flatten(array));
}
}
using System.Collections;
public static class FlattenArray
{
public static IEnumerable Flatten(IEnumerable input)
{
foreach (var element in input)
{
if (element is IEnumerable enumerable)
{
foreach (var childElement in Flatten(enumerable))
{
yield return childElement;
}
}
else if (element != null)
{
yield return element;
}
}
}
}
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,104 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments