Ad
Code
Diff
  • using System;
    using System.Linq;  
    
    public class Kata
        {
            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 * 60);
    
                int monmeCounter = cost / 60;
                if (monmeCounter > monmeCount)
                {
                    monmeCounter = monmeCount;
                }
                cost -= monmeCounter*60;
    
                if (monCount < cost)
                {
                    return new[] { 0 };//leaving the market
                }
    
                return new[] { monCount, monmeCount, totalValue, monmeCounter + cost };
            }
        }
    • using System;
    • using System.Linq;
    • public class Kata
    • {
    • public static int[] PurchaseTofu(int cost, string box)
    • {
    • var monCount = box.Split(' ').Count(x => x == "mon");
    • Console.WriteLine(monCount);
    • var monmeCount = box.Split(' ').Count(x => x == "monme");
    • var TotalValue = monCount + (monmeCount * 60);
    • int NumberOfMonmeUsed = 0;
    • int NumberOfMonUsed = 0;
    • int NeverChangeCost = cost;
    • 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 monCounter = monCount;
    • var monmeCounter = monmeCount;
    • var totalValue = monCount + (monmeCount * 60);
    • while(cost >= 60 && monmeCounter > 0)
    • int monmeCounter = cost / 60;
    • if (monmeCounter > monmeCount)
    • {
    • NumberOfMonmeUsed = NumberOfMonmeUsed + 1;
    • cost = cost - 60;
    • monmeCounter--;
    • monmeCounter = monmeCount;
    • }
    • cost -= monmeCounter*60;
    • while (cost >= 1 && monCounter > 0)
    • if (monCount < cost)
    • {
    • NumberOfMonUsed = NumberOfMonUsed + 1;
    • cost = cost - 1;
    • monCounter--;
    • return new[] { 0 };//leaving the market
    • }
    • var TotalCoinsUsed = NumberOfMonmeUsed + NumberOfMonUsed;
    • var test = (NumberOfMonmeUsed * 60) + NumberOfMonUsed;
    • if (test != NeverChangeCost)
    • {
    • Console.WriteLine("Leaving the market");
    • return new int[] {0};
    • }
    • Console.WriteLine($"[{monCount}, {monmeCount}, {TotalValue}, {TotalCoinsUsed}]");
    • return new int[] { monCount, monmeCount, TotalValue, TotalCoinsUsed };
    • return new[] { monCount, monmeCount, totalValue, monmeCounter + cost };
    • }
    • }