Fundamentals
Strings
Data Types
Logic
public class FizzBuzz { public string GetOutput(int number) => (number % 3, number % 5) switch { (0, 0) => "FizzBuzz", (0, _) => "Fizz", (_, 0) => "Buzz", (_, _) => $"{number}" }; }
- public class FizzBuzz
{public string GetOutput(int number){var ret = string.Empty;if (number % 3 == 0)ret += "Fizz";if (number % 5 == 0)ret += "Buzz";if (ret.Length == 0)ret = number.ToString();return ret;}- {
- public string GetOutput(int number) =>
- (number % 3, number % 5) switch
- {
- (0, 0) => "FizzBuzz",
- (0, _) => "Fizz",
- (_, 0) => "Buzz",
- (_, _) => $"{number}"
- };
- }
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.
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); } }
- 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();}- 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);
- }
- }
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)); }
- using System.Linq;
- using System.Collections.Generic;
- public class Kata
- {
public static bool IsIsogram(string str) => str.Length == str.ToLower().Distinct().Count();- public static bool IsIsogram(string str, HashSet<int> hs = null) =>
- str.All(c => (hs ??= new HashSet<int>()).Add(c & 0x1f));
- }