 # Alexpud's solution

## to Pascal's Triangle in the C# Track

Published at May 17 2020 · 0 comments
Instructions
Test suite
Solution

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

``````    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
``````

## Running the tests

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 PascalsTriangle.cs`

## Further information

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.

## Source

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

### PascalsTriangleTests.cs

``````// This file was auto-generated based on version 1.5.0 of the canonical data.

using Xunit;

public class PascalsTriangleTests
{
[Fact]
public void Zero_rows()
{
Assert.Empty(PascalsTriangle.Calculate(0));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Single_row()
{
var expected = new[]
{
new[] { 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Two_rows()
{
var expected = new[]
{
new[] { 1 },
new[] { 1, 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(2));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Three_rows()
{
var expected = new[]
{
new[] { 1 },
new[] { 1, 1 },
new[] { 1, 2, 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(3));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Four_rows()
{
var expected = new[]
{
new[] { 1 },
new[] { 1, 1 },
new[] { 1, 2, 1 },
new[] { 1, 3, 3, 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(4));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Five_rows()
{
var expected = new[]
{
new[] { 1 },
new[] { 1, 1 },
new[] { 1, 2, 1 },
new[] { 1, 3, 3, 1 },
new[] { 1, 4, 6, 4, 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(5));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Six_rows()
{
var expected = new[]
{
new[] { 1 },
new[] { 1, 1 },
new[] { 1, 2, 1 },
new[] { 1, 3, 3, 1 },
new[] { 1, 4, 6, 4, 1 },
new[] { 1, 5, 10, 10, 5, 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(6));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Ten_rows()
{
var expected = new[]
{
new[] { 1 },
new[] { 1, 1 },
new[] { 1, 2, 1 },
new[] { 1, 3, 3, 1 },
new[] { 1, 4, 6, 4, 1 },
new[] { 1, 5, 10, 10, 5, 1 },
new[] { 1, 6, 15, 20, 15, 6, 1 },
new[] { 1, 7, 21, 35, 35, 21, 7, 1 },
new[] { 1, 8, 28, 56, 70, 56, 28, 8, 1 },
new[] { 1, 9, 36, 84, 126, 126, 84, 36, 9, 1 }
};
Assert.Equal(expected, PascalsTriangle.Calculate(10));
}
}``````
``````﻿using System.Collections.Generic;
using System.Linq;

public static class PascalsTriangle
{
public static IEnumerable<IEnumerable<int>> Calculate(int rows)
{
if (rows == 0)
{
return Enumerable.Empty<IEnumerable<int>>();
}

var previousTriangleLvlElements = new List<int>(){1};
var pascalTriangle = new List<List<int>>();
for(var currentTriangleLvl = 1; currentTriangleLvl < rows; currentTriangleLvl++)
{
var newTriangleLvl = GetNewTriangleLvl(previousTriangleLvlElements, currentTriangleLvl);
previousTriangleLvlElements = newTriangleLvl;
}
return pascalTriangle;
}

private static List<int> GetNewTriangleLvl(List<int> previousTriangleLvlElements,  int currentTriangleLvl)
{
var newTriangleLvlElements = new List<int>();
for (int newTriangleColumn = 0; newTriangleColumn <= currentTriangleLvl; newTriangleColumn++)
{
}
return newTriangleLvlElements;
}

private static int GetCurrentTriangleColumnValue(int newLineColumn, List<int> previousTriangleLvlElements)
{
var valueInPreviousColumnInLvlAbove = newLineColumn == 0 ? 0 : previousTriangleLvlElements[newLineColumn - 1];
var valueInCurrentColumnLvlAbove = newLineColumn >= previousTriangleLvlElements.Count() ? 0  : previousTriangleLvlElements[newLineColumn];
return valueInPreviousColumnInLvlAbove + valueInCurrentColumnLvlAbove;
}
}``````