using System; using System.Linq; using System.Collections.Generic; namespace Kumite { public class Problem { public static int SumDigitsOf(long integer) => Array.ConvertAll(integer.ToString().TrimStart('-').ToCharArray(), c => (int)Char.GetNumericValue(c)).Sum(); } }
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace Kumite
- {
- public class Problem
- {
public static int SumDigitsOf(long integer) => (int)(from ch in integer.ToString()where Char.IsDigit(ch)select Char.GetNumericValue(ch)).Sum();- public static int SumDigitsOf(long integer) =>
- Array.ConvertAll(integer.ToString().TrimStart('-').ToCharArray(),
- c => (int)Char.GetNumericValue(c)).Sum();
- }
- }
using System; using System.Linq; using System.Collections.Generic; namespace Kumite { public class Problem { public static int SumDigitsOf(long integer) { return (int)(from ch in integer.ToString() where Char.IsDigit(ch) select Char.GetNumericValue(ch)).Sum(); } } }
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace Kumite
- {
- public class Problem
- {
- public static int SumDigitsOf(long integer)
- {
return integer.GetDigits().Sum();- return (int)(from ch in integer.ToString()
- where Char.IsDigit(ch)
- select Char.GetNumericValue(ch)).Sum();
- }
- }
public static class LongExtensions{public static IEnumerable<int> GetDigits(this long number){number = Math.Abs(number);while (number > 9){yield return (int)(number % 10);number /= 10;}yield return (int)number;}}- }
Fundamentals