Ad
Code
Diff
  • struct Test {
        const int t = 42;
    };
    
    template <class T>
    struct TestTemplateThis : T {
        int get_t() const { return this->t; }
    };
    
    template <class T>
    struct TestTemplateNoThis : T {
        int get_t() const { return this->t; }
    };
    
    • struct Test {
    • const int t = 42;
    • };
    • template <class T>
    • struct TestTemplateThis : T {
    • int get_t() const { return this->t; }
    • };
    • template <class T>
    • struct TestTemplateNoThis : T {
    • int get_t() const { return t; }
    • int get_t() const { return this->t; }
    • };

If x contains the digit 3 return true, otherwise return false

Code
Diff
  • using System.Linq;
    
    public class Kumite {
      public static bool IsThree(int x) =>
        $"{x}".Any(x => x == '3');
    }
    • fn solution(x: i32) -> bool {
    • x.to_string().chars().any(|x| x == '3')
    • using System.Linq;
    • public class Kumite {
    • public static bool IsThree(int x) =>
    • $"{x}".Any(x => x == '3');
    • }
  • Replaced the Where(char.IsDigit) with Math.Abs() for negatives

  • Changed from query format to method format

  • You can convert each digit to a number in .Sum(), no need to use select

  • Instead of using char.GetNumericValue(), you can avoid casting back to int by subtracting the char by 48, the ASCII value of '0', which will return an int

Code
Diff
  • using System;
    using System.Linq;
    using System.Collections.Generic;
    
    namespace Kumite
    {
      public class Problem
      {
        public static int SumDigitsOf(long integer) => 
          Math.Abs(integer)
              .ToString()
              .Sum(x => x-48); 
        
      }
    }
    • 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) =>
    • Math.Abs(integer)
    • .ToString()
    • .Sum(x => x-48);
    • }
    • }
Code
Diff
  • public class FizzBuzz
    {
        public string GetOutput(int number) {
          bool divisThree = number % 3 == 0;
          bool divisFive = number % 5 == 0;
          // If the two values are different, one is true
          // If the two values are the same, both or neither are true
          return divisThree != divisFive
               ? divisThree ? "Fizz" : "Buzz"
               : divisThree ? "FizzBuzz" : number.ToString();
          // Fizz buzz is a popular computer science interview question.  
          // The function above is given a number - if the number is
          // divisible by 3, return "fizz", if it's divisible by 5, 
          // return "buzz", if not divisble by 3 or 5 - return the
          // number itself.
        }
    }
    • public class FizzBuzz
    • {
    • public string GetOutput(int number) {
    • if (number % 15 == 0) return "FizzBuzz";
    • else if (number % 3 == 0) return "Fizz";
    • else if (number % 5 == 0) return "Buzz";
    • else return number.ToString();
    • bool divisThree = number % 3 == 0;
    • bool divisFive = number % 5 == 0;
    • // If the two values are different, one is true
    • // If the two values are the same, both or neither are true
    • return divisThree != divisFive
    • ? divisThree ? "Fizz" : "Buzz"
    • : divisThree ? "FizzBuzz" : number.ToString();
    • // Fizz buzz is a popular computer science interview question.
    • // The function above is given a number - if the number is
    • // divisible by 3, return "fizz", if it's divisible by 5,
    • // return "buzz", if not divisble by 3 or 5 - return the
    • // number itself.
    • }
    • }