Ad
Code
Diff
  • 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;
      }
    }
    • public static class Kata
    • {
    • public static int SameCase(char a, char b) =>
    • !char.IsLetter(a) || !char.IsLetter(b) ? -1 :
    • char.IsUpper(a).Equals(char.IsUpper(b)) ? 1 : 0;
    • 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;
    • }
    • }

C# generic numbers from .NET 8

Code
Diff
  • public static class Kata
    {
      public static T doubleValue<T>(T x)
        where T : INumber<T>
      {
        return x + x;
      }
    }
    • interface Kata {
    • static int doubleValue(int x) {
    • public static class Kata
    • {
    • public static T doubleValue<T>(T x)
    • where T : INumber<T>
    • {
    • return x + x;
    • }
    • }
Code
Diff
  • package cat;
    public class kata{
      public static int doubleValue(int x) {
        var sum = 0;
        for (var i = 0; i < 2; i++)
          sum += x;
        return sum;
      }
    }
    • package cat;
    • public class kata{
    • public static int doubleValue(int x) {
    • return x * 2;
    • var sum = 0;
    • for (var i = 0; i < 2; i++)
    • sum += x;
    • return sum;
    • }
    • }
Code
Diff
  • package cat;
    public class kata{
      public static int doubleValue(int x) {
          return x << 1;
      }
    }
    • package cat;
    • public class kata{
    • public static int doubleValue(int x) {
    • return x * 2;
    • }
    • public static int doubleValue(int x) {
    • return x << 1;
    • }
    • }
Fundamentals
Logic

It ain't getting any smaller than this (I think)

Code
Diff
  • using System.Linq;class Math{public static T Max<T>(params T[]p)=>p.Max();}
    • class Math{public static T Max<T>(params T[]p)where T:System.IComparable<T>{foreach(T a in p)p[0]=p[0].CompareTo(a)<0?a:p[0];return p[0];}}
    • using System.Linq;class Math{public static T Max<T>(params T[]p)=>p.Max();}
Code
Diff
  • import java.util.*;
    
    class Vehicle {
    
        int currentSpeed;
        Road currentRoad = new Road(60);
    
        public void setCurrentSpeed(int... accelerations) {
            currentSpeed = Arrays.stream(accelerations).sum();
        }
        
        public int getCurrentSpeed() {
            return currentSpeed;
        }
    
        public boolean isWithinSpeedLimit() {
            return currentSpeed <= currentRoad.speedLimit;
        }
    }
    
    final class Road {
    
        int speedLimit;
    
        public Road(int speedLimit) {
            this.speedLimit = speedLimit;
        }
    }
    
    • import java.util.*;
    • /**
    • an object of class blah
    • author
    • date
    • */
    • class Vehicle {
    • /**I make up the class invariant, I describe speed limit*/
    • final int SPEED_LIMIT = 60;
    • /**I make up the class invariant, i describe current speed*/
    • int currentSpeed;
    • public void setCurrentSpeed(int[] accelerations) {
    • currentSpeed = Arrays.stream(accelerations).sum();
    • }
    • /**i describe current speed*/
    • public int getCurrentSpeed() {
    • return currentSpeed;
    • }
    • /**i describe within speed limit*/
    • public boolean isWithinSpeedLimit() {
    • return getCurrentSpeed() <= SPEED_LIMIT;
    • }
    • int currentSpeed;
    • Road currentRoad = new Road(60);
    • public void setCurrentSpeed(int... accelerations) {
    • currentSpeed = Arrays.stream(accelerations).sum();
    • }
    • public int getCurrentSpeed() {
    • return currentSpeed;
    • }
    • public boolean isWithinSpeedLimit() {
    • return currentSpeed <= currentRoad.speedLimit;
    • }
    • }
    • final class Road {
    • int speedLimit;
    • public Road(int speedLimit) {
    • this.speedLimit = speedLimit;
    • }
    • }

Readable

Code
Diff
  • 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;
      }
    }
    • 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 int SameCase(char a, char b)
    • {
    • if (!char.IsLetter(a) || !char.IsLetter(b))
    • return -1;
    • return char.IsUpper(a) == char.IsUpper(b) ? 1 : 0;
    • }
    • }
Code
Diff
  • import java.util.*;
    /** 
    an object of class blah
    author
    date
    */ 
    class Vehicle {
      
      /**I make up the class invariant, I describe speed limit*/
      final int SPEED_LIMIT = 60; 
      
      /**I make up the class invariant, i describe current speed*/
      int currentSpeed; 
      
      /** */
      public void setCurrentSpeed(int[] accelerations) {
        currentSpeed = Arrays.stream(accelerations).sum();
      }
      
      /**i describe current speed*/
      public int getCurrentSpeed() {
        return currentSpeed;
      }
      
      /**i describe within speed limit*/
      public boolean isWithinSpeedLimit() {
        return getCurrentSpeed() <= SPEED_LIMIT; 
      }
      
    }
    
    • import java.util.*;
    • /**
    • an object of class blah
    • author
    • date
    • */
    • class Vehicle {
    • /**I make up the class invariant, I describe speed limit*/
    • final int SPEED_LIMIT = 60;
    • /**I make up the class invariant, i describe current speed*/
    • int currentSpeed;
    • /**I am the name*/
    • String name;
    • /**constructor comment*/
    • public Vehicle(){
    • name="S";
    • StringBuilder b = new StringBuilder();
    • HashMap<Integer,Integer> h = new HashMap<Integer,Integer>();
    • }
    • /** */
    • public void setCurrentSpeed(int[] accelerations) {
    • currentSpeed = Arrays.stream(accelerations).sum();
    • }
    • /**i describe current speed*/
    • public int getCurrentSpeed() {
    • return currentSpeed;
    • }
    • /**i describe within speed limit*/
    • public boolean isWithinSpeedLimit() {
    • return getCurrentSpeed() <= SPEED_LIMIT;
    • }
    • }
Fundamentals
Logic

Vectorized instructions to immensely speed up search process

Code
Diff
  • using System.Diagnostics;
    using System.Numerics;
    using System.Linq;
    
    public class Math
    {
        public static int Max(params int[] nums)
        {
            var blockSize = Vector<int>.Count;
            var blockCount = nums.Length / blockSize;
    
            var vectorMax = Vector<int>.Zero;
            for (var block = 0; block < blockCount; block++)
            {
                var blockIndex = block * blockSize;
                var blockVector = new Vector<int>(nums, blockIndex);
                vectorMax = Vector.Max(vectorMax, blockVector);
            }
    
            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++)
            {
                max = System.Math.Max(vector[i], max);
            }
    
            return max;
        }
    }  
    • using System.Diagnostics;
    • using System.Numerics;
    • using System.Linq;
    • public class Math
    • {
    • public static int Max(params int[] param) => param.Max();
    • public static int Max(params int[] nums)
    • {
    • var blockSize = Vector<int>.Count;
    • var blockCount = nums.Length / blockSize;
    • var vectorMax = Vector<int>.Zero;
    • for (var block = 0; block < blockCount; block++)
    • {
    • var blockIndex = block * blockSize;
    • var blockVector = new Vector<int>(nums, blockIndex);
    • vectorMax = Vector.Max(vectorMax, blockVector);
    • }
    • 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++)
    • {
    • max = System.Math.Max(vector[i], max);
    • }
    • return max;
    • }
    • }
Code
Diff
  • import static java.util.stream.IntStream.rangeClosed;
    import java.util.List;
    
    interface FizzBuzz {
      static List<String> evaluate(int input) {
        return rangeClosed(1, input).mapToObj(FizzBuzz::determineFizzbuzz).toList();
      }
      
      private static String determineFizzbuzz(int n) {
        if (n % 15 == 0) return "FizzBuzz";
        if (n % 3 == 0) return "Fizz";
        if (n % 5 == 0) return "Buzz";
        return Integer.toString(n);
      }
    }
    • import static java.util.stream.IntStream.rangeClosed;
    • import java.util.List;
    • interface FizzBuzz {
    • static List<String> evaluate(int input) {
    • return rangeClosed(1, input).mapToObj(FizzBuzz::determineFizzbuzz).toList();
    • }
    • private static String determineFizzbuzz(int n) {
    • if (n % 15 == 0) return "FizzBuzz";
    • if (n % 3 == 0) return "Fizz";
    • if (n % 5 == 0) return "Buzz";
    • return "" + n;
    • return Integer.toString(n);
    • }
    • }

Just returns "FizzBuzz" instead of the additional length check if divisible by 15

Using StringBuilder instead of string concatenations

Code
Diff
  • import static java.util.stream.IntStream.rangeClosed;
    import java.util.List;
    
    interface FizzBuzz {
      static List<String> evaluate(int input) {
        return rangeClosed(1, input).mapToObj(FizzBuzz::determineFizzbuzz).toList();
      }
      
      private static String determineFizzbuzz(int n) {
        if(n % 15 == 0) return "FizzBuzz";
        
        var ans = new StringBuilder();
        if (n % 3 == 0) ans.append("Fizz");
        else
        if (n % 5 == 0) ans.append("Buzz");
        return ans.length() > 0 ? ans.toString() : Integer.toString(n);
      }
    }
    • import static java.util.stream.IntStream.rangeClosed;
    • import java.util.List;
    • interface FizzBuzz {
    • static List<String> evaluate(int input) {
    • return rangeClosed(1, input).mapToObj(FizzBuzz::determineFizzbuzz).toList();
    • }
    • private static String determineFizzbuzz(int n) {
    • String ans = "";
    • if(n % 15 == 0) ans += "FizzBuzz";
    • if(n % 15 == 0) return "FizzBuzz";
    • var ans = new StringBuilder();
    • if (n % 3 == 0) ans.append("Fizz");
    • else
    • if (n % 3 == 0) ans += "Fizz";
    • else
    • if (n % 5 == 0) ans += "Buzz";
    • return ans.length() > 0 ? ans : Integer.toString(n);
    • if (n % 5 == 0) ans.append("Buzz");
    • return ans.length() > 0 ? ans.toString() : Integer.toString(n);
    • }
    • }
Fundamentals
Logic

And even more by removing linq namespace and comparing and setting all elements to p[0]

Code
Diff
  • 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;using System.Linq;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 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];}}
Fundamentals
Logic

shortened even more

Code
Diff
  • using System;using System.Linq;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;using System.Linq;public class Math{public static T Max<T>(params T[] param)where T:IComparable<T>{return param.Any()?param.Aggregate(param[0],(a,b)=>a.CompareTo(b)>0?a:b):default;}}
    • using System;using System.Linq;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];}}
Code
Diff
  • import static java.util.stream.IntStream.rangeClosed;
    import java.util.List;
    
    interface FizzBuzz {
      static List<String> evaluate(int input) {
        return rangeClosed(1, input).mapToObj(FizzBuzz::determineFizzbuzz).toList();
      }
      
      private static String determineFizzbuzz(int n) {
        String ans = "";
        if (n % 3 == 0)
          ans += "Fizz";
        if (n % 5 == 0)
          ans += "Buzz";
        return ans.length() > 0 ? ans : Integer.toString(n);
      }
    }
    • import static java.util.stream.IntStream.rangeClosed;
    • import java.util.List;
    • interface FizzBuzz {
    • static List<String> evaluate(int input) {
    • return rangeClosed(1, input).mapToObj(i -> i % 15 == 0 ? "FizzBuzz" :
    • i % 5 == 0 ? "Buzz" : i % 3 == 0 ? "Fizz" : "" + i).toList();
    • return rangeClosed(1, input).mapToObj(FizzBuzz::determineFizzbuzz).toList();
    • }
    • private static String determineFizzbuzz(int n) {
    • String ans = "";
    • if (n % 3 == 0)
    • ans += "Fizz";
    • if (n % 5 == 0)
    • ans += "Buzz";
    • return ans.length() > 0 ? ans : Integer.toString(n);
    • }
    • }
Code
Diff
  • import java.util.List;
    import java.util.ArrayList;
    
    public class FizzBuzz {
      
      public static List<String> evaluate(int input) {
        List<String> listOfString = new ArrayList<>(input);
        for (int i = 1; i < input + 1; i++) {
          listOfString.add(parse(i));
        }
        
        return listOfString;
      }
      
      private static String parse(int number) {
        var sb = new StringBuilder();
        if (number == 3) {
          sb.append("Fizz");
        } else if (number == 5) {
          sb.append("Buzz");
        }
        
        return sb.length() > 0 ? sb.toString() : Integer.toString(number);
      }
    }
    • import java.util.List;
    • import java.util.ArrayList;
    • public class FizzBuzz {
    • public static List<String> evaluate(int input) {
    • List<String> listOfString = new ArrayList<>();
    • List<String> listOfString = new ArrayList<>(input);
    • for (int i = 1; i < input + 1; i++) {
    • listOfString.add(parse(i));
    • }
    • return listOfString;
    • }
    • private static String parse(int number) {
    • var sb = new StringBuilder();
    • if (number == 3) {
    • return "Fizz";
    • sb.append("Fizz");
    • } else if (number == 5) {
    • return "Buzz";
    • sb.append("Buzz");
    • }
    • return Integer.toString(number);
    • return sb.length() > 0 ? sb.toString() : Integer.toString(number);
    • }
    • }
Loading more items...