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
It ain't getting any smaller than this (I think)
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 blahauthordate*/- 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
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;
- }
- }
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;
- }
- }
Vectorized instructions to immensely speed up search process
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;
- }
- }
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
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";elseif (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);
- }
- }
And even more by removing linq namespace and comparing and setting all elements to 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];}}
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];}}
shortened even more
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];}}
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);
- }
- }
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);
- }
- }