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

Readable RGB to HSV, with the fog machine turned off.

Same algorithm, fewer riddles. This fork names the moving parts instead of asking the reader to spelunk through nested ternaries and magic constants:

  • normalized RGB channels
  • value, minimum, and chroma
  • hue sector calculation
  • saturation and value percentages

No shade to the ancestors; the math was already there. I just made it stop whispering and start explaining itself.

Code
Diff
  • const RGB_MAX = 255;
    const HUE_SECTOR_DEGREES = 60;
    const PERCENT = 100;
    
    function rgbToHsv(rgb) {
      const [red, green, blue] = rgb.map(normalizeChannel);
    
      const value = Math.max(red, green, blue);
      const minimum = Math.min(red, green, blue);
      const chroma = value - minimum;
    
      const hue = chroma === 0
        ? 0
        : hueSector(red, green, blue, chroma, value) * HUE_SECTOR_DEGREES;
    
      const saturation = value === 0 ? 0 : chroma / value;
    
      return [
        roundDegrees(hue),
        roundPercent(saturation),
        roundPercent(value)
      ];
    }
    
    function normalizeChannel(channel) {
      return channel / RGB_MAX;
    }
    
    function hueSector(red, green, blue, chroma, value) {
      if (value === red) return ((green - blue) / chroma + 6) % 6;
      if (value === green) return (blue - red) / chroma + 2;
      return (red - green) / chroma + 4;
    }
    
    function roundDegrees(degrees) {
      return Math.round(degrees) % 360;
    }
    
    function roundPercent(ratio) {
      return Math.round(ratio * PERCENT);
    }
    • function rgbToHsv([r,g,b]){
    • [r,g,b] = [r,g,b].map(x => x/255);
    • let v = Math.max(r,g,b)
    • ,d = v - Math.min(r,g,b)
    • ,h = d ? (v == r ? (g - b) / d:v == g ?(b - r) / d + 2:(r - g)/d + 4):0;
    • return [Math.round((h * 60 + 360) %360)
    • ,Math.round(v ? d / v * 100 : 0)
    • ,Math.round(v * 100)]
    • const RGB_MAX = 255;
    • const HUE_SECTOR_DEGREES = 60;
    • const PERCENT = 100;
    • function rgbToHsv(rgb) {
    • const [red, green, blue] = rgb.map(normalizeChannel);
    • const value = Math.max(red, green, blue);
    • const minimum = Math.min(red, green, blue);
    • const chroma = value - minimum;
    • const hue = chroma === 0
    • ? 0
    • : hueSector(red, green, blue, chroma, value) * HUE_SECTOR_DEGREES;
    • const saturation = value === 0 ? 0 : chroma / value;
    • return [
    • roundDegrees(hue),
    • roundPercent(saturation),
    • roundPercent(value)
    • ];
    • }
    • function normalizeChannel(channel) {
    • return channel / RGB_MAX;
    • }
    • function hueSector(red, green, blue, chroma, value) {
    • if (value === red) return ((green - blue) / chroma + 6) % 6;
    • if (value === green) return (blue - red) / chroma + 2;
    • return (red - green) / chroma + 4;
    • }
    • function roundDegrees(degrees) {
    • return Math.round(degrees) % 360;
    • }
    • function roundPercent(ratio) {
    • return Math.round(ratio * PERCENT);
    • }
Sets
Code
Diff
  • dumbRockPaperScissors = lambda a,b: ["Draw","Player 1 wins","Player 2 wins"][("RPS".index(a[0])- "RPS".index(b[0]))%3]
    • dumbRockPaperScissors=lambda a,b:('Draw','Player 1 wins','Player 2 wins')['RPS'.find(a[0])-'RPS'.find(b[0])]
    • def rps (p1,p2):
    • retval= {
    • 'R':{'R': 0, 'S':-1, "P": 1},
    • 'S':{'R': 1, 'S': 0, 'P':-1},
    • 'P':{'R':-1, 'S': 1, 'P':0}
    • }
    • return retval[p1][p2]
    • dumbRockPaperScissors = lambda a,b: ["Draw","Player 1 wins","Player 2 wins"][("RPS".index(a[0])- "RPS".index(b[0]))%3]
Games
Arrays

some may have failed

Code
Diff
  • console.log("testing 1 2 3");
      
          
    • const canvas = document.getElementById("gameCanvas");
    • const ctx = canvas.getContext("2d");
    • console.log("testing 1 2 3");
    • const gridSize = 20;
    • const tileCount = canvas.width / gridSize;
    • let snake = [{ x: 10, y: 10 }];
    • let direction = { x: 1, y: 0 };
    • let nextDirection = { x: 1, y: 0 };
    • let food = spawnFood();
    • let score = 0;
    • function spawnFood() {
    • let newFood;
    • while (true) {
    • newFood = {
    • x: Math.floor(Math.random() * tileCount),
    • y: Math.floor(Math.random() * tileCount)
    • };
    • if (!snake.some(part => part.x === newFood.x && part.y === newFood.y)) {
    • return newFood;
    • }
    • }
    • }
    • function draw() {
    • ctx.clearRect(0, 0, canvas.width, canvas.height);
    • ctx.fillStyle = "red";
    • ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize, gridSize);
    • // Draw snake
    • snake.forEach((part, index) => {
    • ctx.fillStyle = index === 0 ? "lime" : "green";
    • ctx.fillRect(part.x * gridSize, part.y * gridSize, gridSize, gridSize);
    • });
    • }
    • function update() {
    • direction = nextDirection;
    • const head = {
    • x: snake[0].x + direction.x,
    • y: snake[0].y + direction.y
    • };
    • // Wall collision
    • if (
    • head.x < 0 || head.x >= tileCount ||
    • head.y < 0 || head.y >= tileCount
    • ) {
    • resetGame();
    • return;
    • }
    • // Self collision
    • if (snake.some(part => part.x === head.x && part.y === head.y)) {
    • resetGame();
    • return;
    • }
    • snake.unshift(head);
    • // Eat food
    • if (head.x === food.x && head.y === food.y) {
    • score++;
    • document.getElementById("score").textContent = score;
    • food = spawnFood();
    • } else {
    • snake.pop();
    • }
    • draw();
    • }
    • function resetGame() {
    • snake = [{ x: 10, y: 10 }];
    • direction = { x: 1, y: 0 };
    • nextDirection = { x: 1, y: 0 };
    • food = spawnFood();
    • score = 0;
    • document.getElementById("score").textContent = score;
    • }
    • draw();
    • setInterval(update, 120);