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.
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).
// 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 symbolfunction 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
class AddWithoutAdditionSignTest extends TestCase { public function testThatAddFunctionDoesNotUseAdditionSign() { // Open the text file containing the user solution code and read the entire file $user_solution_source_code = fread(fopen('/home/codewarrior/solution.txt', 'r'), filesize('/home/codewarrior/solution.txt')); // Print user solution code onto the console (just to show that it works) echo $user_solution_source_code . "\r\n"; // Use a regular expression to test for the (forbidden) '+' symbol in the user solution $this->assertNotRegExp('/\+/', $user_solution_source_code, "Your solution musn't contain the '+' symbol!"); } }
const fs = require('fs');const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8');describe("Check Solution", function(){it("should prevent the '+' symbol from being used anywhere in the code", function(){Test.expect(solution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!");});});- class AddWithoutAdditionSignTest extends TestCase {
- public function testThatAddFunctionDoesNotUseAdditionSign() {
- // Open the text file containing the user solution code and read the entire file
- $user_solution_source_code = fread(fopen('/home/codewarrior/solution.txt', 'r'), filesize('/home/codewarrior/solution.txt'));
- // Print user solution code onto the console (just to show that it works)
- echo $user_solution_source_code . "\r\n";
- // Use a regular expression to test for the (forbidden) '+' symbol in the user solution
- $this->assertNotRegExp('/\+/', $user_solution_source_code, "Your solution musn't contain the '+' symbol!");
- }
- }
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+=iif name=="":k+=ck+=" "for i in w:k+=ik=k+e+eelse:k+=" "k+=namereturn k- return f"Hello {name}" if name != '' else "Hello, World!!"
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;
- }
- }
- }
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())
- );
- }
- }
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 500if (speed >= limit + 20 && speed <= limit + 29) return 250if (speed >= limit + 10 && speed <= limit + 19) return 100return 0- return speed >= limit + 30? 500 :
- speed >= limit + 20? 250 :
- speed >= limit + 10? 100 : 0
- }
Fixed your issue with finding
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));
}
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));
- }