loading...

re: Daily Challenge #260 - Subtract the Sum VIEW POST

FULL DISCUSSION
 

My version, in C# 😄

    public static string GetMatchingWordFor(int number)
    {
        var nValues = GetDictionaryFromRawString(RawValues);

        while (!nValues.ContainsKey(number))
        {
            number -= number.ToString()
                .Select(digit => int.Parse(digit.ToString()))
                .Aggregate(0, (acc, x) => acc + x);
        }

        return nValues[number];
    }

The helper function to convert the raw list into a usable dictionary:

    public static Dictionary<int, string> GetDictionaryFromRawString(string raw)
    {
        return raw.Split("\n")
            .Select(str =>
                str.Trim()
                    .Split("-"))
            .ToDictionary(
                row => int.Parse(row[0]),
                row => row[1]);
    }
code of conduct - report abuse