using System; using System.Linq; public class Kata { private const int monmeValue = 60; public static int[] PurchaseTofu(int cost, string box) { string[] boxSplited = box.Split(' '); var monCount = boxSplited.Count(x => x == "mon");//one foreach will be faster than this 2 lines but this is easier to read var monmeCount = boxSplited.Count(x => x == "monme"); var totalValue = monCount + (monmeCount * monmeValue); int monmeCounter = Math.Min(cost / monmeValue, monmeCount); cost -= monmeCounter * monmeValue; return (monCount < cost) ? new[] { 0 } : //leaving the market new[] { monCount, monmeCount, totalValue, monmeCounter + cost }; } }
- using System;
- using System.Linq;
- public class Kata
- {
- private const int monmeValue = 60;
- public static int[] PurchaseTofu(int cost, string box)
- {
string[] boxSplited = box.Split(' ');- string[] boxSplited = box.Split(' ');
- var monCount = boxSplited.Count(x => x == "mon");//one foreach will be faster than this 2 lines but this is easier to read
- var monmeCount = boxSplited.Count(x => x == "monme");
var totalValue = monCount + (monmeCount * 60);- var totalValue = monCount + (monmeCount * monmeValue);
int monmeCounter = cost / 60;if (monmeCounter > monmeCount){monmeCounter = monmeCount;}cost -= monmeCounter*60;- int monmeCounter = Math.Min(cost / monmeValue, monmeCount);
- cost -= monmeCounter * monmeValue;
if (monCount < cost){return new[] { 0 };//leaving the market}return new[] { monCount, monmeCount, totalValue, monmeCounter + cost };- return (monCount < cost) ?
- new[] { 0 } : //leaving the market
- new[] { monCount, monmeCount, totalValue, monmeCounter + cost };
- }
- }