Ad
Code
Diff
  • public static class Kata
    {
      public static int SameCase(char a, char b) =>
        char.IsLetter(a) && char.IsLetter(b) ? 
          (char.IsUpper(a) == char.IsUpper(b) ? 1 : 0)
        : -1;
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b)
    • {
    • return char.IsLetter(a) && char.IsLetter(b) ? (char.IsUpper(a) == char.IsUpper(b) ? 1 : 0) : -1;
    • }
    • public static int SameCase(char a, char b) =>
    • char.IsLetter(a) && char.IsLetter(b) ?
    • (char.IsUpper(a) == char.IsUpper(b) ? 1 : 0)
    • : -1;
    • }
Code
Diff
  • public static class Kata 
    {
      public static int SameCase(char a, char b) =>
        !char.IsLetter(a) || !char.IsLetter(b)
          ? -1 
        : char.IsUpper(a) == char.IsUpper(b)
          ? 1 : 0;
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b)
    • {
    • if (!char.IsLetter(a) || !char.IsLetter(b))
    • {
    • return -1;
    • }
    • return char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
    • }
    • }
    • // 3ms faster :)
    • public static int SameCase(char a, char b) =>
    • !char.IsLetter(a) || !char.IsLetter(b)
    • ? -1
    • : char.IsUpper(a) == char.IsUpper(b)
    • ? 1 : 0;
    • }
Code
Diff
  • public static class Kata 
    {
      public static int SameCase(char a, char b) =>
        char.IsLetter(a) && char.IsLetter(b)
          ? char.IsUpper(a) == char.IsUpper(b)
          ? 1 : 0
          : -1;
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b)
    • {
    • return (char.IsLetter(a) && char.IsLetter(b))
    • ? (char.IsUpper(a) && char.IsUpper(b)) || (char.IsLower(a) && char.IsLower(b))
    • ? 1 : 0
    • : -1;
    • }
    • public static int SameCase(char a, char b) =>
    • char.IsLetter(a) && char.IsLetter(b)
    • ? char.IsUpper(a) == char.IsUpper(b)
    • ? 1 : 0
    • : -1;
    • }
Code
Diff
  • public static class Kata 
    {
      public static int SameCase(char a, char b) =>
        (!char.IsLetter(a) || !char.IsLetter(b))
          ? -1
        
        : (char.IsLower(a) == char.IsLower(b))
          ? 1
          : 0;
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b)
    • {
    • if (!char.IsLetter(a) || !char.IsLetter(b))
    • return -1;
    • public static int SameCase(char a, char b) =>
    • (!char.IsLetter(a) || !char.IsLetter(b))
    • ? -1
    • return char.IsUpper(a) == char.IsUpper(b)
    • : (char.IsLower(a) == char.IsLower(b))
    • ? 1
    • : 0;
    • }
    • }
Fundamentals
Logic
Code
Diff
  • using System;
    using System.Diagnostics;
    using System.Numerics;
    using System.Linq;
    
    public class Math
    {
        public static T Max<T>(params T[] nums) where T : struct, IComparable<T>
        {
            if (nums.Length == 0)
                throw new InvalidOperationException("No nums were provided to find the maximum value.");
          
            T max;
            Span<T> items = nums;
            if (items.Length / Vector<T>.Count > 1)
            {
                // We have at least two vectors
                var maxVector = new Vector<T>(items); // first vector
                items = items[Vector<T>.Count..]; // skip first vector
                while (items.Length >= Vector<T>.Count)
                {
                    var nextVector = new Vector<T>(items);  // next vector
                    items = items[Vector<T>.Count..]; // skip next vector
                    maxVector = Vector.Max(maxVector, nextVector); // evaluate maxVector
                }
              
                // Evaluate Max for maxVector
                max = maxVector[0];
                for (var i = 1; i < Vector<T>.Count; ++i)
                    if (maxVector[i] is var item && item.CompareTo(max) > 0)
                        max = item;
            }
            else
            {
                max = items[0]; // max is first item
                items = items[1..]; // skip first item
            }
    
            // Evaluate Max for items in span
            for (var i = 0; i < items.Length; ++i)
                if (items[i] is var item && item.CompareTo(max) > 0)
                    max = item;
          
            Debug.Assert(max.CompareTo(nums.Max()) == 0);
            return max;
        }
    }  
    • using System;
    • using System.Diagnostics;
    • using System.Numerics;
    • using System.Linq;
    • public class Math
    • {
    • public static int Max(params int[] nums)
    • public static T Max<T>(params T[] nums) where T : struct, IComparable<T>
    • {
    • var blockSize = Vector<int>.Count;
    • var blockCount = nums.Length / blockSize;
    • var vectorMax = Vector<int>.Zero;
    • for (var block = 0; block < blockCount; block++)
    • if (nums.Length == 0)
    • throw new InvalidOperationException("No nums were provided to find the maximum value.");
    • T max;
    • Span<T> items = nums;
    • if (items.Length / Vector<T>.Count > 1)
    • {
    • var blockIndex = block * blockSize;
    • var blockVector = new Vector<int>(nums, blockIndex);
    • vectorMax = Vector.Max(vectorMax, blockVector);
    • // We have at least two vectors
    • var maxVector = new Vector<T>(items); // first vector
    • items = items[Vector<T>.Count..]; // skip first vector
    • while (items.Length >= Vector<T>.Count)
    • {
    • var nextVector = new Vector<T>(items); // next vector
    • items = items[Vector<T>.Count..]; // skip next vector
    • maxVector = Vector.Max(maxVector, nextVector); // evaluate maxVector
    • }
    • // Evaluate Max for maxVector
    • max = maxVector[0];
    • for (var i = 1; i < Vector<T>.Count; ++i)
    • if (maxVector[i] is var item && item.CompareTo(max) > 0)
    • max = item;
    • }
    • var max = MaxWithinVector(vectorMax);
    • var leftover = nums.Length % blockSize;
    • for (var i = nums.Length - leftover; i < nums.Length; i++)
    • {
    • max = System.Math.Max(nums[i], max);
    • }
    • Debug.Assert(max == nums.Max());
    • return max;
    • }
    • public static int MaxWithinVector(Vector<int> vector)
    • {
    • var max = vector[0];
    • for (var i = 1; i < Vector<int>.Count; i++)
    • else
    • {
    • max = System.Math.Max(vector[i], max);
    • max = items[0]; // max is first item
    • items = items[1..]; // skip first item
    • }
    • // Evaluate Max for items in span
    • for (var i = 0; i < items.Length; ++i)
    • if (items[i] is var item && item.CompareTo(max) > 0)
    • max = item;
    • Debug.Assert(max.CompareTo(nums.Max()) == 0);
    • return max;
    • }
    • }
Fundamentals
Logic

If the items array is empty, it throws an InvalidOperationException with an error message.

The method initializes a variable max with the first element of the items array.

The method iterates over the items array starting from the second element, and uses the CompareTo method of each element to compare it to max. If an element is greater than max, it updates max value.

The method returns the final max value.

Code
Diff
  • using System;
    public static class Math
    {
        public static T Max<T>(params T[] items) where T : IComparable<T>
        {
            if (items.Length == 0)
                throw new InvalidOperationException("No items were provided to find the maximum value.");
    
            T max = items[0];
            for (int i = 1; i < items.Length; i++)
                if (items[i].CompareTo(max) > 0)
                    max = items[i];
    
            return max;
        }
    }
    
    • using System;
    • public static class Math
    • {
    • public static T Max<T>(params T[] items) where T : IComparable<T>
    • {
    • if (items == null || items.Length == 0)
    • if (items.Length == 0)
    • throw new InvalidOperationException("No items were provided to find the maximum value.");
    • T max = items[0];
    • for (int i = 1; i < items.Length; i++)
    • {
    • max = items[i].CompareTo(max) > 0 ? items[i] : max;
    • }
    • if (items[i].CompareTo(max) > 0)
    • max = items[i];
    • return max;
    • }
    • }
Code
Diff
  • """
    Returns True if year is a Leap Year else False.
    if (year is not divisible by 4) then (it is a common year)
    else if (year is not divisible by 100) then (it is a leap year)
    else if (year is divisible by 400) then (it is a leap year)
    else (it is a common year)
    """
    def is_leap_year(year: str) -> bool:
        return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
    
    • """
    • Returns True if year is a Leap Year else False.
    • if (year is not divisible by 4) then (it is a common year)
    • else if (year is not divisible by 100) then (it is a leap year)
    • else if (year is divisible by 400) then (it is a leap year)
    • else (it is a common year)
    • """
    • def is_leap_year(year: str) -> bool:
    • """Returns True if year is a Leap Year else False."""
    • case1: bool = year % 4 == 0
    • case2: bool = year % 100 == 0
    • case3: bool = year % 400 == 0
    • return case1 and not case2 or case3
    • return year % 4 == 0 and year % 100 != 0 or year % 400 == 0
Code
Diff
  • using System;
    
    public static class LeapYears
    {
      // if (year is not divisible by 4) then (it is a common year)
      // else if (year is not divisible by 100) then (it is a leap year)
      // else if (year is divisible by 400) then (it is a leap year)
      // else (it is a common year) 
      public static bool IsLeapYear(int year) => year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }
    • using System;
    • public class LeapYears
    • public static class LeapYears
    • {
    • // A leap year is any year that's divisible by 4 EXCEPT for years that're both divisible by 100 and not divisible by 400.
    • public static bool IsLeapYear(int year) => year % 400 == 0 || year % 100 != 0 && year % 4 == 0;
    • // if (year is not divisible by 4) then (it is a common year)
    • // else if (year is not divisible by 100) then (it is a leap year)
    • // else if (year is divisible by 400) then (it is a leap year)
    • // else (it is a common year)
    • public static bool IsLeapYear(int year) => year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    • }
Fundamentals
Logic
Code
Diff
  • using System;
    
    public static class Math
    {
        public static T Max<T>(params T[] items) where T : IComparable<T>
        {
            if (items.Length <= 0)
                throw new InvalidOperationException();
    
            var max = items[0];
            for (var i = 1; i < items.Length; ++i)
                if (max.CompareTo(items[i]) < 0)
                    max = items[i];
    
            return max;
        }
    }
    
    • using System;public class Math{public static T Max<T>(params T[]p)where T:IComparable<T>{foreach(T a in p){p[0]=p[0].CompareTo(a)<0?a:p[0];}return p[0];}}
    • using System;
    • public static class Math
    • {
    • public static T Max<T>(params T[] items) where T : IComparable<T>
    • {
    • if (items.Length <= 0)
    • throw new InvalidOperationException();
    • var max = items[0];
    • for (var i = 1; i < items.Length; ++i)
    • if (max.CompareTo(items[i]) < 0)
    • max = items[i];
    • return max;
    • }
    • }
Code
Diff
  • public static class Kata 
    {
      public static int SameCase(char a, char b) => 
        (!char.IsLetter(a) || !char.IsLetter(b)) ? -1 :
        (char.IsLower(a) == char.IsLower(b)) ? 1 : 0;
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b) => !(char.IsLetter(a) && char.IsLetter(b)) ? -1 : ((a&' ') == (b&' ')) ? 1 : 0;
    • public static int SameCase(char a, char b) =>
    • (!char.IsLetter(a) || !char.IsLetter(b)) ? -1 :
    • (char.IsLower(a) == char.IsLower(b)) ? 1 : 0;
    • }