Start a new Kumite
AllAgda (Beta)BF (Beta)CCFML (Beta)ClojureCOBOL (Beta)CoffeeScriptCommonLisp (Beta)CoqC++CrystalC#D (Beta)DartElixirElm (Beta)Erlang (Beta)Factor (Beta)Forth (Beta)Fortran (Beta)F#GoGroovyHaskellHaxe (Beta)Idris (Beta)JavaJavaScriptJulia (Beta)Kotlinλ Calculus (Beta)LeanLuaNASMNim (Beta)Objective-C (Beta)OCaml (Beta)Pascal (Beta)Perl (Beta)PHPPowerShell (Beta)Prolog (Beta)PureScript (Beta)PythonR (Beta)RacketRaku (Beta)Reason (Beta)RISC-V (Beta)RubyRustScalaShellSolidity (Beta)SQLSwiftTypeScriptVB (Beta)
Show only mine

Kumite (ko͞omiˌtā) is the practice of taking techniques learned from Kata and applying them through the act of freestyle sparring.

You can create a new kumite by providing some initial code and optionally some test cases. From there other warriors can spar with you, by enhancing, refactoring and translating your code. There is no limit to how many warriors you can spar with.

A great use for kumite is to begin an idea for a kata as one. You can collaborate with other code warriors until you have it right, then you can convert it to a kata.

Ad
Ad

How to read the user's solution in your test cases (PHP)

The PHP version of @jhoffner's infamous Kumite on the foolproof method to obtain the user's solution source code within your test cases whereby it is impossible for the user to cheat/hack. I cannot believe it took me this long to figure it out! Previously, I've always wanted to translate Kata that involved banning certain syntax/features into PHP but ended up giving up and moving on because I couldn't figure out a way to obtain a user's solution code (the Reflection API doesn't include methods to obtain a function/method's source code).

Code
Diff
  • // TODO: Write a function that doesn't use the add symbol
    function add($a, $b) {
      return $a + $b;
    }
    
    // Make sure to view the test cases to see how this test fails
    • // write a add function that doesn't use the plus symbol
    • function add(a, b){
    • return a + b;
    • // TODO: Write a function that doesn't use the add symbol
    • function add($a, $b) {
    • return $a + $b;
    • }
    • // Make sure to view the test cases to see how this test fails
Code
Diff
  • function fizzbuzzy(n) {
      var three = Math.floor(n/3)+1;
      var five = Math.floor(n/5)+1;
      return three+five;
    }
    • function fizzbuzzy(n) {
    • //insert code here
    • var three = Math.floor(n/3)+1;
    • var five = Math.floor(n/5)+1;
    • return three+five;
    • }
(println "Hello Clojure!")
Code
Diff
  • def Complicated_Greetings(name):
        return f"Hello {name}" if name != '' else "Hello, World!!"
    • def Complicated_Greetings(name):
    • h=["H","e","l","l","o"]
    • c=","
    • w=["W","o","r","l","d"]
    • e="!"
    • k=""
    • for i in h:
    • k+=i
    • if name=="":
    • k+=c
    • k+=" "
    • for i in w:
    • k+=i
    • k=k+e+e
    • else:
    • k+=" "
    • k+=name
    • return k
    • return f"Hello {name}" if name != '' else "Hello, World!!"
Code
Diff
  • using System.Text.RegularExpressions;
    public class Kata {
      public static int SameCase(char a, char b) {
        
        string lowerCase = @"[a-z]";
        string upperCase = @"[A-Z]";
        string symbols =  @"[^a-zA-Z]";
        
          if   ((Regex.IsMatch($"{a}", lowerCase) && Regex.IsMatch($"{b}", lowerCase))
             ||(Regex.IsMatch($"{a}", upperCase) && Regex.IsMatch($"{b}", upperCase)))
          {
            return 1;
          }
          else if (Regex.IsMatch($"{a}", symbols) || Regex.IsMatch($"{b}", symbols))
          {
            return -1;
          }
          else
          {
            return 0;
          }
             
      }
    }
    • using System.Text.RegularExpressions;
    • public class Kata {
    • public static int SameCase(char a, char b) {
    • string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    • char tempA = a;
    • char tempB = b;
    • if((!letters.Contains(tempA = char.ToUpper(tempA)) && letters.Contains(tempB = char.ToUpper(tempB))) ||
    • (!letters.Contains(tempB = char.ToUpper(tempB)) && letters.Contains(tempA = char.ToUpper(tempA))) ||
    • (!letters.Contains(tempA = char.ToUpper(tempA)) && !letters.Contains(tempB = char.ToUpper(tempB)))) return -1;
    • if(char.IsUpper(a) && char.IsUpper(b)){
    • return 1;
    • } else if (char.IsLower(a) && char.IsLower(b)){
    • return 1;
    • } else {
    • return 0;
    • }
    • string lowerCase = @"[a-z]";
    • string upperCase = @"[A-Z]";
    • string symbols = @"[^a-zA-Z]";
    • if ((Regex.IsMatch($"{a}", lowerCase) && Regex.IsMatch($"{b}", lowerCase))
    • ||(Regex.IsMatch($"{a}", upperCase) && Regex.IsMatch($"{b}", upperCase)))
    • {
    • return 1;
    • }
    • else if (Regex.IsMatch($"{a}", symbols) || Regex.IsMatch($"{b}", symbols))
    • {
    • return -1;
    • }
    • else
    • {
    • return 0;
    • }
    • }
    • }
Code
Diff
  • import java.util.*;
    import java.util.stream.Collectors;
    
    public class MaxNumber {
        public static long print(long number) {
          return Long.parseLong(
            String.valueOf(number)
                .chars()
                .mapToObj(ch -> String.valueOf(Character.getNumericValue(ch)))
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.joining())
        );
      }
    }
    • import java.util.Arrays;
    • import java.util.Scanner;
    • import java.util.*;
    • import java.util.stream.Collectors;
    • public class MaxNumber {
    • public static long print(long number) {
    • String numeroString = Long.toString(number);
    • char[] digitos = numeroString.toCharArray();
    • Arrays.sort(digitos);
    • StringBuilder numeroOrdenadoStr = new StringBuilder(new String(digitos));
    • numeroOrdenadoStr.reverse();
    • return Long.parseLong(numeroOrdenadoStr.toString());
    • }
    • public static void main(String[] args) {
    • Scanner scanner = new Scanner(System.in);
    • System.out.print("Ingrese un numero: ");
    • long number = scanner.nextLong();
    • long maxNumber = print(number);
    • System.out.println("El número más alto que se puede formar con estos dígitos es: " + maxNumber);
    • return Long.parseLong(
    • String.valueOf(number)
    • .chars()
    • .mapToObj(ch -> String.valueOf(Character.getNumericValue(ch)))
    • .sorted(Comparator.reverseOrder())
    • .collect(Collectors.joining())
    • );
    • }
    • }
Functional Programming
Algorithms
Mathematics
Code
Diff
  • export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
      return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));
    }
    const CalculateFine = (speed: number, limit: number) => {
      return speed >= limit + 30? 500 : 
        speed >= limit + 20? 250 : 
        speed >= limit + 10? 100 : 0
    }
    • export const checkIfAtOrBelowLimit = (driverSpeeds: number[], speedLimit: number): number[] => {
    • return driverSpeeds.map(speed => CalculateFine(speed, speedLimit));
    • }
    • const CalculateFine = (speed: number, limit: number) => {
    • if(speed >= limit + 30) return 500
    • if (speed >= limit + 20 && speed <= limit + 29) return 250
    • if (speed >= limit + 10 && speed <= limit + 19) return 100
    • return 0
    • return speed >= limit + 30? 500 :
    • speed >= limit + 20? 250 :
    • speed >= limit + 10? 100 : 0
    • }

Fixed your issue with finding

Code
Diff
  • function closestToZero(array $ints) {
      if(!empty($ints)){
        $closest = $ints[0];
      }else{
        return 0;
      }
      foreach($ints as $num){
        if(abs($num)< abs($closest)){
          $closest = $num;
        }
      }
      if($closest<0 && array_search(abs($closest), $ints)){
        return abs($closest);
      }else{
        return $closest;
      }
    }
    ?>
    • function closestToZero(array $ints) {
    • if(!empty($ints)){
    • $closest = $ints[0];
    • }else{
    • return 0;
    • }
    • foreach($ints as $num){
    • if(abs($num)< abs($closest)){
    • $closest = $num;
    • }
    • }
    • if($closest<0 && array_search(abs($closest), $ints)){
    • return abs($closest);
    • }else{
    • return $closest;
    • }
    • }
    • ?>

function rgbToHsv(rgb) {

let red = rgb[0], green = rgb[1], blue = rgb[2];
const hsvValue = Math.max(red, green, blue) / 255;

if (hsvValue == 0) return Array.of(0, 0, 0);
red /= hsvValue;
green /= hsvValue;
blue /= hsvValue;
const saturation = 1 - Math.min(red, green, blue) / 255;
if (saturation == 0) return Array.of(0, 0, hsvValue * 100);
red = 255 - (255 - r) / saturation;
green = 255 - (255 - g) / saturation;
blue = 255 - (255 - b) / saturation;
const peak = Math.max(red, green, blue);
let hue = 0;
if (red == peak) hue = green < blue ? 360 - blue * 60 / 255 : green * 60 / 255;
else if (green == peak) hue = red > blue ? 120 - red * 60 / 255 : 120 + blue * 60 / 255;
else hue = red > green ? 240 + red * 60 / 255 : 240 - green * 60 / 255;
return Array.of(Math.round(hue), Math.round(saturation * 100), Math.round(hsvValue * 100));
}

Code
Diff
  • function rgbToHsv(rgb) {
    
      let red = rgb[0], green = rgb[1], blue = rgb[2];
      const hsvValue = Math.max(red, green, blue) / 255;
      
      if (hsvValue == 0) return Array.of(0, 0, 0);
    
      red /= hsvValue;
      green /= hsvValue;
      blue /= hsvValue;
    
      const saturation = 1 - Math.min(red, green, blue) / 255;
    
      if (saturation == 0) return Array.of(0, 0, hsvValue * 100);
    
      red = 255 - (255 - red) / saturation;
      green = 255 - (255 - green) / saturation;
      blue = 255 - (255 - blue) / saturation;
      const peak = Math.max(red, green, blue);
      let hue = 0;
      
      if (red == peak) hue = green < blue ? 360 - blue * 60 / 255 : green * 60 / 255;
      else if (green == peak) hue = red > blue ? 120 - red * 60 / 255 : 120 + blue * 60 / 255;
      else hue = red > green ? 240 + red * 60 / 255 : 240 - green * 60 / 255;
      return Array.of(Math.round(hue), Math.round(saturation * 100), Math.round(hsvValue *  100));
    }
    • function rgbToHsv(rgb) {
    • let r = rgb[0], g = rgb[1], b = rgb[2];
    • const v = Math.max(r, g, b) / 255;
    • if (v == 0) return Array.of(0, 0, 0);
    • r /= v;
    • g /= v;
    • b /= v;
    • const s = 1 - Math.min(r, g, b) / 255;
    • if (s == 0) return Array.of(0, 0, v * 100);
    • r = 255 - (255 - r) / s;
    • g = 255 - (255 - g) / s;
    • b = 255 - (255 - b) / s;
    • const peak = Math.max(r, g, b);
    • let h = 0;
    • if (r == peak) h = g < b ? 360 - b * 60 / 255 : g * 60 / 255;
    • else if (g == peak) h = r > b ? 120 - r * 60 / 255 : 120 + b * 60 / 255;
    • else h = r > g ? 240 + r * 60 / 255 : 240 - g * 60 / 255;
    • return Array.of(Math.round(h), Math.round(s * 100), Math.round(v * 100));
    • let red = rgb[0], green = rgb[1], blue = rgb[2];
    • const hsvValue = Math.max(red, green, blue) / 255;
    • if (hsvValue == 0) return Array.of(0, 0, 0);
    • red /= hsvValue;
    • green /= hsvValue;
    • blue /= hsvValue;
    • const saturation = 1 - Math.min(red, green, blue) / 255;
    • if (saturation == 0) return Array.of(0, 0, hsvValue * 100);
    • red = 255 - (255 - red) / saturation;
    • green = 255 - (255 - green) / saturation;
    • blue = 255 - (255 - blue) / saturation;
    • const peak = Math.max(red, green, blue);
    • let hue = 0;
    • if (red == peak) hue = green < blue ? 360 - blue * 60 / 255 : green * 60 / 255;
    • else if (green == peak) hue = red > blue ? 120 - red * 60 / 255 : 120 + blue * 60 / 255;
    • else hue = red > green ? 240 + red * 60 / 255 : 240 - green * 60 / 255;
    • return Array.of(Math.round(hue), Math.round(saturation * 100), Math.round(hsvValue * 100));
    • }