DEV Community

Discussion on: Daily Challenge #260 - Subtract the Sum

Collapse
 
pbouillon profile image
Pierre Bouillon • Edited

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]);
    }