using System.Collections.Generic;
public class WastedSandCounter
public int onTheEdge(int i, int j, int n, int m)
if (i == 0 && j == 0 || i == n - 1 && j == 0 || i == 0 && j == m - 1 || i == n - 1 && j == m - 1)
else if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
public int Count(int[, ] sandPile)
int n = sandPile.GetLength(0);
int m = sandPile.GetLength(1);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (onTheEdge(i, j, n, m) == 0)
else if (onTheEdge(i, j, n, m) == 1)
else if (onTheEdge(i, j, n, m) == 2)
if (i == n - 1 && j == 0)
if (i == n - 1 && j == m - 1)
if (i == 0 && j == m - 1)
Console.WriteLine(e.Message);
totalCount += currentCount;
while (changesMade == true);
private readonly string[] _people;
private readonly string[] _productTypes;
private readonly int _initialInventory;
public Marketplace(string[] people, string[] productTypes, int initialInventory)
_productTypes = productTypes;
_initialInventory = initialInventory;
public int CheckInventory(IEnumerable<string> transactions, string person, string productType)
int[, ] currentInventory = new int[_people.Length, _productTypes.Length];
for (int i = 0; i < currentInventory.GetLength(0); i++)
for (int j = 0; j < currentInventory.GetLength(1); j++)
currentInventory[i, j] = _initialInventory;
foreach (string s in transactions)
string[] words = s.Split(' ');
string persOne = words[0];
string persTwo = words[words.Length - 1];
string choiceOne = "bought";
string choiceTwo = "sold";
int indexOfPersOne = 0, indexOfPersTwo = 0, indexOfProduct = 0;
bool[] valid = new bool[]{false, false, false, false, true};
for (int i = 0; i < _people.Length; i++)
if (String.Equals(_people[i], persOne))
if (String.Equals(_people[i], persTwo))
if (String.Equals(words[1], choiceOne) || String.Equals(words[1], choiceTwo))
for (int i = 0; i < _productTypes.Length; i++)
if (String.Equals(_productTypes[i], words[3]))
for (int i = 0; i < valid.Length - 1; i++)
valid[valid.Length - 1] = false;
if (valid[valid.Length - 1] == true)
int amountExchanged = Convert.ToInt32(words[2]);
if (String.Equals(words[1], choiceOne))
if (currentInventory[indexOfPersTwo, indexOfProduct] >= amountExchanged)
currentInventory[indexOfPersTwo, indexOfProduct] -= amountExchanged;
currentInventory[indexOfPersOne, indexOfProduct] += amountExchanged;
if (String.Equals(words[1], choiceTwo))
if (currentInventory[indexOfPersOne, indexOfProduct] >= amountExchanged)
currentInventory[indexOfPersOne, indexOfProduct] -= amountExchanged;
currentInventory[indexOfPersTwo, indexOfProduct] += amountExchanged;
catch (NullReferenceException e)
Console.WriteLine(e.Message);
int indexOfPers = 0, indexOfProd = 0;
for (int i = 0; i < _people.Length; i++)
if (String.Equals(person, _people[i]))
for (int i = 0; i < _productTypes.Length; i++)
if (String.Equals(productType, _productTypes[i]))
return currentInventory[indexOfPers, indexOfProd];
public static void Main()
private static void Run()
new WastedSandCounterTests().Start();
new MarketplaceTests().Start();
public class WastedSandCounterTests
Tests.Run("WastedSandCounter should", (it) =>
it.Should(NotCountSandFallingOnTheTable, "not count sand that falls on the table");
it.Should(CountAllSandFallingOffTheTable, "count sand that falls off the table");
it.Should(FullyCollapseTallSandPiles, "fully collapse tall piles of the sand");
public void NotCountSandFallingOnTheTable()
var counter = new WastedSandCounter();
var table = new[, ]{{0, 0, 0}, {0, 4, 0}, {0, 0, 0}};
counter.Count(table).ShouldBe(0);
public void CountAllSandFallingOffTheTable()
var counter = new WastedSandCounter();
var table = new[, ]{{2, 4, 2}, {4, 0, 4}, {2, 4, 2}};
counter.Count(table).ShouldBe(12);
public void FullyCollapseTallSandPiles()
var counter = new WastedSandCounter();
var table = new[, ]{{12, 0, 0}, {0, 0, 0}, {0, 0, 0}};
counter.Count(table).ShouldBe(6);
public class MarketplaceTests
Tests.Run("Marketplace should", (it) =>
it.Should(() => NotDoInvalidTransactions(), "not do transactions that are invalid");
it.Should(() => DoValidTransactions(), "do transactions that are invalid");
public void NotDoInvalidTransactions()
var marketplace = new Marketplace(new[]{"Ana", "Ion"}, new[]{"apples"}, 3);
marketplace.CheckInventory(new[]{"Ana sold 4 apples to Ion"}, "Ana", "apples").ShouldBe(3);
marketplace.CheckInventory(new[]{"Ion bought 4 apples from Ana"}, "Ana", "apples").ShouldBe(3);
public void DoValidTransactions()
var marketplace = new Marketplace(new[]{"Ana", "Ion"}, new[]{"apples"}, 4);
marketplace.CheckInventory(new[]{"Ana sold 4 apples to Ion", "Ion sold 2 apples to Ana"}, "Ana", "apples").ShouldBe(2);
public static void Run(string testGroup, Action<Tests> tests)
public static void StartTests(string testGroup)
Console.WriteLine("====================================================");
Console.WriteLine(testGroup);
Console.WriteLine("====================================================");
public void Should(Action test, string testName)
Console.WriteLine(testName);
Console.WriteLine(" Passed");
Console.WriteLine(" Failed");
Console.WriteLine(" " + e.Message);
public static void EndTests()