Fundamentals
Strings
Code
Diff
  • public class FizzBuzz
    {
        public string GetOutput(int number) =>
            (number % 3, number % 5) switch
            {
                (0, 0) => "FizzBuzz",
                (0, _) => "Fizz",
                (_, 0) => "Buzz",
                (_, _) => $"{number}"
            };
    }
  • 11 public class FizzBuzz
    2 {
    3 public string GetOutput(int number)
    4 {
    5 var ret = string.Empty;
    6
    7 if (number % 3 == 0)
    8 ret += "Fizz";
    9 if (number % 5 == 0)
    10 ret += "Buzz";
    11
    12 if (ret.Length == 0)
    13 ret = number.ToString();
    14 return ret;
    15 }
    2+{
    3+ public string GetOutput(int number) =>
    4+ (number % 3, number % 5) switch
    5+ {
    6+ (0, 0) => "FizzBuzz",
    7+ (0, _) => "Fizz",
    8+ (_, 0) => "Buzz",
    9+ (_, _) => $"{number}"
    10+ };
    1616 }

We can use recursion because the size of long is limited
Also we could combine two methods like this:

f(x) => x == 0 ? 0 : x < 0 ? f(-x) : x % 10 + f(x / 10);

or even multiply by the signum of x on each iteration:

(x >> sizeof(long) * 8 - 1 << 1) + 1

but it is a bit slower.

Code
Diff
  • using System;
    using System.Linq;
    using System.Collections.Generic;
    
    namespace Kumite
    {
        public class Problem
        {
            public static long SignedSumDigitsOf(long x) =>
              x == 0 ? 0 : x % 10 + SignedSumDigitsOf(x / 10);
    
            public static long SumDigitsOf(long x) =>
              SignedSumDigitsOf(x) * Math.Sign(x);
        }
    }
  • 11 using System;
    22 using System.Linq;
    33 using System.Collections.Generic;
    44
    55 namespace Kumite
    66 {
    7 public class Problem
    8 {
    9 public static int SumDigitsOf(long integer) =>
    10 Array.ConvertAll(integer.ToString().TrimStart('-').ToCharArray(),
    11 c => (int)Char.GetNumericValue(c)).Sum();
    12 }
    7+ public class Problem
    8+ {
    9+ public static long SignedSumDigitsOf(long x) =>
    10+ x == 0 ? 0 : x % 10 + SignedSumDigitsOf(x / 10);
    11+
    12+ public static long SumDigitsOf(long x) =>
    13+ SignedSumDigitsOf(x) * Math.Sign(x);
    14+ }
    1313 }
Code
Diff
  • using System.Linq;
    using System.Collections.Generic;
    
    public class Kata
    {
        public static bool IsIsogram(string str, HashSet<int> hs = null) =>
            str.All(c => (hs ??= new HashSet<int>()).Add(c & 0x1f));
    }
  • 11 using System.Linq;
    2+using System.Collections.Generic;
    22
    33 public class Kata
    44 {
    5 public static bool IsIsogram(string str) => str.Length == str.ToLower().Distinct().Count();
    6+ public static bool IsIsogram(string str, HashSet<int> hs = null) =>
    7+ str.All(c => (hs ??= new HashSet<int>()).Add(c & 0x1f));
    66 }