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
Basic Language Features
Fundamentals
Control Flow

Made the for-loop a little bit slimmer. Also += always creates new string. Join only ones and therefore better performance

Code
Diff
  • def convert_decimal_roman(number):
        number = int(number)
        str = []
        numDct = {1000: "M",
                  900: "CM",
                  500: "D",
                  400: "CD",
                  100: "C",
                  90: "XC",
                  50: "L",
                  40: "XL",
                  10: "X",
                  9: "IX",
                  5: "V",
                  4: "IV",
                  1: "I"}
        
        for key, value in numDct.items():
            str.append(value * (number // key))
            number = number % key
            
        return "".join(str)
    • def convert_decimal_roman(number):
    • number = int(number)
    • str = ""
    • str = []
    • numDct = {1000: "M",
    • 900: "CM",
    • 500: "D",
    • 400: "CD",
    • 100: "C",
    • 90: "XC",
    • 50: "L",
    • 40: "XL",
    • 10: "X",
    • 9: "IX",
    • 5: "V",
    • 4: "IV",
    • 1: "I"}
    • for key in numDct:
    • while number >= key:
    • str += numDct[key]
    • number -= key
    • return str
    • for key, value in numDct.items():
    • str.append(value * (number // key))
    • number = number % key
    • return "".join(str)
Code
Diff
  • 4
    • function relativeNumbers(a, b) {
    • let arrOne = [];
    • let arrTwo = [];
    • for (let i = 2; i < 10; i++) {
    • if (a % i === 0 && a !== i) {
    • arrOne.push(i);
    • }
    • if(b % i === 0 && b !== i){
    • arrTwo.push(i)
    • }
    • }
    • for(let i = 0; i < arrOne.length; i++){
    • for(let j = 0; j < arrTwo.length; j++){
    • if(arrOne[i] === arrTwo[j]){
    • return "non relative"
    • }else return "relative"
    • }
    • }
    • }
    • 4

During development turned out that the draw can end in a way that
a team drawed from the runners-up pot DO NOT have
an eligible opponent in the other pot.
This case is handled and log functions are added to shorten the code.

Code
Diff
  • /*13.Dec.2021. Draw of the UEFA Champions League quarter-finals. 
    This was a joke with a bad software. They had to redraw it. Decided to code a better one.
    
    Rules: there are 16 teams, 8 group-winners in one pot and 8 runners-up in another pot.
    A runner-up team is drawed first. Then it is paired with a group-winner. 
    There are 2 criteria for the opponent:
    it can not go from the same group
    it can not have the same nationality.
    After the pairing, the drawed teams are taken out from their pots before the next pairing.
    
    During development turned out that the draw can end in a way that 
    a team drawed from the runners-up pot DO NOT have 
    an eligible opponent in the other pot.
    This case is handled and log functions are added to shorten the code.*/
    
    
    /*database: 2 arrays of objects*/
    const groupWinners = [
        {
            name: "Manchester City",
            group: "A",
            nationality: "English"
        },
        {
            name: "Liverpool",
            group: "B",
            nationality: "English"
        },
        {
            name: "Ajax Amsterdam",
            group: "C",
            nationality: "Dutch"
        },
        {
            name: "Real Madrid",
            group: "D",
            nationality: "Spanish"
        },
        {
            name: "Bayern Munich",
            group: "E",
            nationality: "German"
        },
        {
            name: "Manchester United",
            group: "F",
            nationality: "English"
        },
        {
            name: "Lille",
            group: "G",
            nationality: "French"
        },
        {
            name: "Juventus",
            group: "H",
            nationality: "Italian"
        }
    ];
    
    const runnersUp = [
        {
            name: "Paris Saint-Germain",
            group: "A",
            nationality: "French"
        },
        {
            name: "Atletico Madrid",
            group: "B",
            nationality: "Spanish"
        },
        {
            name: "Sporting CP",
            group: "C",
            nationality: "Portuguese"
        },
        {
            name: "Internazionale",
            group: "D",
            nationality: "Italian"
        },
        {
            name: "Benfica",
            group: "E",
            nationality: "Portuguese"
        },
        {
            name: "Villarreal",
            group: "F",
            nationality: "Spanish"
        },
        {
            name: "FC Salzburg",
            group: "G",
            nationality: "Austrian"
        },
        {
            name: "Chelsea",
            group: "H",
            nationality: "English"
        }
    ];
    
    /*log function */
    function log(textStr, groupStr, nationalityStr, endStr, teamObj) {
        console.log(
            textStr,
            teamObj.name,
            groupStr,
            teamObj.group,
            nationalityStr,
            teamObj.nationality,
            endStr
        );
    }
    
    /*log the teams */
    console.log("\nWELCOME TO THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!\n");
    console.log("Let's see the teams!\n");
    console.log("Group winners:\n");
    groupWinners.forEach(
        team => {
            log("", " (Group: ", " Nationality: ", ")", team);
        }
    );
    console.log("\nRunners-up:\n");
    runnersUp.forEach(
        team => {
            log("", " (Group: ", " Nationality: ", ")", team);
        }
    );
    
    console.log("\n\nLet's start the draw!\n");
    draw();
    
    function draw() {
        /*arrays containing teams which can be still drawed and an array for the pairings*/
        let groupWinnersToDraw = [...groupWinners];
        let runnersUpToDraw = [...runnersUp];
        let pairings = [];
    
        /*while there are teams to draw */
        while (runnersUpToDraw.length > 0) {
            /*choose a random team from runnersUpToDraw */
            let index = Math.floor(Math.random() * (runnersUpToDraw.length));
            let teamToFace = runnersUpToDraw[index];
            /*updating the runnersUpToDraw array*/
            runnersUpToDraw = runnersUpToDraw
                .slice(0, index)
                .concat(
                    runnersUpToDraw.slice(index + 1, runnersUpToDraw.length)
                );
    
            log("\n\nTeam to face:", " (Group: ", " Nationality: ", ")", teamToFace);
            console.log("\n");
    
            /*selecting the potential opponents */
            let potentialOpponents = groupWinnersToDraw.filter(
                team =>
                    (team.group != teamToFace.group && 
                    team.nationality != teamToFace.nationality)
            );
            if (potentialOpponents.length == 0) {
                potentialOpponents.push(
                    `No eligible opponent for ${teamToFace.name}! We have to re-draw!`
                );
            }
    
            if (typeof potentialOpponents[0] == "string") {
                console.log(potentialOpponents[0]);
                return;
            }
    
            console.log("Potential opponents:\n");
            potentialOpponents.forEach(
                team => 
                    log("", " (Group: ", " Nationality: ", ")", team)
            );
    
            /*choose a random team from potentialOpponents */
            let anotherIndex = Math.floor(Math.random() * (potentialOpponents.length));
            let opponent = potentialOpponents[anotherIndex];
            log("\n\nThe opponent is:", " (Group: ", " Nationality: ", ")", opponent);
            /*updating the groupWinnersToDraw array*/
            let yetAnotherIndex = 0;
            for (let i = 0; i < groupWinnersToDraw.length; i++) {
                if (groupWinnersToDraw[i].name == opponent.name) {
                    yetAnotherIndex = i;
                }
            }
            groupWinnersToDraw = groupWinnersToDraw
                .slice(0, yetAnotherIndex)
                .concat(
                    groupWinnersToDraw.slice(yetAnotherIndex + 1, groupWinnersToDraw.length)
                );
    
            if (runnersUpToDraw.length > 0) {    
                console.log("\n\nRemaining group-winners:\n")
                groupWinnersToDraw.forEach(
                    team => {
                        log("", " (Group: ", " Nationality: ", ")", team);
                    }
                );
    
                console.log("\nRemaining runners-up:\n");
                runnersUpToDraw.forEach(
                    team => {
                        log("", " (Group: ", " Nationality: ", ")", team);
                    }
                );
            }
    
            /*save the pairings */
            let drawing = [];
            drawing.push(teamToFace, opponent);
            pairings.push(drawing);
        }
    
        /*log the draw */
        console.log("\nTHE QUARTER-FINALS: \n");
        pairings.forEach(
            pair => {
                console.log(
                    pair[0].name,
                    " (Group: ",
                    pair[0].group, 
                    " Nationality: ",
                    pair[0].nationality, 
                    ")",
                    "   vs   ",
                    pair[1].name, 
                    " (Group: ",
                    pair[1].group, 
                    " Nationality: ",
                    pair[1].nationality,
                    ")"
                );
            }
        );
    }
    
    
    • /*2021.12.13. Draw of the UEFA Champions League quarter-finals.
    • This was a joke with a bad software. Decided to code a better one.
    • /*13.Dec.2021. Draw of the UEFA Champions League quarter-finals.
    • This was a joke with a bad software. They had to redraw it. Decided to code a better one.
    • There are 16 teams, 8 group-winners and 8 runners-up.
    • A runner-up team is drawed first from the 8. It is paired with a gruop-winner.
    • There are 2 rules:
    • Rules: there are 16 teams, 8 group-winners in one pot and 8 runners-up in another pot.
    • A runner-up team is drawed first. Then it is paired with a group-winner.
    • There are 2 criteria for the opponent:
    • it can not go from the same group
    • it can not have the same nationality.
    • After the pairing, the paired teams are taken out of the list before the next draw.
    • */
    • After the pairing, the drawed teams are taken out from their pots before the next pairing.
    • /*database: 2 arrays of objects*/
    • During development turned out that the draw can end in a way that
    • a team drawed from the runners-up pot DO NOT have
    • an eligible opponent in the other pot.
    • This case is handled and log functions are added to shorten the code.*/
    • /*database: 2 arrays of objects*/
    • const groupWinners = [
    • {
    • name: "Manchester City",
    • group: "A",
    • nationality: "English"
    • },
    • {
    • name: "Liverpool",
    • group: "B",
    • nationality: "English"
    • },
    • {
    • name: "Ajax Amsterdam",
    • group: "C",
    • nationality: "Dutch"
    • },
    • {
    • name: "Real Madrid",
    • group: "D",
    • nationality: "Spanish"
    • },
    • {
    • name: "Bayern Munich",
    • group: "E",
    • nationality: "German"
    • },
    • {
    • name: "Manchester United",
    • group: "F",
    • nationality: "English"
    • },
    • {
    • name: "Lille",
    • group: "G",
    • nationality: "French"
    • },
    • {
    • name: "Juventus",
    • group: "H",
    • nationality: "Italian"
    • }
    • ];
    • const runnersUp = [
    • {
    • name: "Paris Saint-Germain",
    • group: "A",
    • nationality: "French"
    • },
    • {
    • name: "Atletico Madrid",
    • group: "B",
    • nationality: "Spanish"
    • },
    • {
    • name: "Sporting CP",
    • group: "C",
    • nationality: "Portuguese"
    • },
    • {
    • name: "Internazionale",
    • group: "D",
    • nationality: "Italian"
    • },
    • {
    • name: "Benfica",
    • group: "E",
    • nationality: "Portuguese"
    • },
    • {
    • name: "Villarreal",
    • group: "F",
    • nationality: "Spanish"
    • },
    • {
    • name: "FC Salzburg",
    • group: "G",
    • nationality: "Austrian"
    • },
    • {
    • name: "Chelsea",
    • group: "H",
    • nationality: "English"
    • }
    • ];
    • console.log("\n");
    • console.log("WELCOME IN THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!");
    • console.log("\n");
    • /*arrays containing teams which can be still drawed and an array for the pairings*/
    • let groupWinnersToDraw = [...groupWinners];
    • let runnersUpToDraw = [...runnersUp];
    • let pairings = [];
    • /*while there are teams to draw */
    • while (runnersUpToDraw.length > 0) {
    • /*choose a random team from runnersUpToDraw */
    • let index = Math.floor(Math.random()*(runnersUpToDraw.length));
    • let teamToFace = runnersUpToDraw[index];
    • /*updating the runnersUpToDraw array*/
    • runnersUpToDraw = runnersUpToDraw.slice(0, index).concat(runnersUpToDraw.slice(index+1, runnersUpToDraw.length));
    • console.log("Team to face:", teamToFace.name, teamToFace.group, teamToFace.nationality);
    • console.log("\n");
    • /*selecting the potential opponents */
    • let potentialOpponents = groupWinnersToDraw.filter(team =>
    • (team.group != teamToFace.group && team.nationality != teamToFace.nationality)
    • /*log function */
    • function log(textStr, groupStr, nationalityStr, endStr, teamObj) {
    • console.log(
    • textStr,
    • teamObj.name,
    • groupStr,
    • teamObj.group,
    • nationalityStr,
    • teamObj.nationality,
    • endStr
    • );
    • console.log("Potential opponents:")
    • potentialOpponents.forEach(team => console.log(team.name, team.group, team.nationality));
    • console.log("\n");
    • /*choose a random team from potentialOpponents */
    • let anotherIndex = Math.floor(Math.random()*(potentialOpponents.length));
    • let opponent = potentialOpponents[anotherIndex];
    • console.log("The opponent is:", opponent.name, opponent.group, opponent.nationality);
    • console.log("\n");
    • /*updating the groupWinnersToDraw array*/
    • let yetAnotherIndex = 0;
    • for (let i = 0; i < groupWinnersToDraw.length; i++) {
    • if (groupWinnersToDraw[i].name == opponent.name) {
    • yetAnotherIndex = i;
    • }
    • }
    • groupWinnersToDraw = groupWinnersToDraw.slice(0, yetAnotherIndex).concat(groupWinnersToDraw.slice(yetAnotherIndex+1, groupWinnersToDraw.length));
    • console.log("Remaining group-winners:")
    • groupWinnersToDraw.forEach(team => console.log(team.name));
    • console.log("\n");
    • /*save the pairings */
    • let drawing = [];
    • drawing.push(teamToFace);
    • drawing.push(opponent);
    • pairings.push(drawing);
    • }
    • /*log the draw */
    • /*log the teams */
    • console.log("\nWELCOME TO THE DRAW OF THE UEFA CHAMPIONS LEAGUE QUARTER-FINALS!\n");
    • console.log("Let's see the teams!\n");
    • console.log("Group winners:\n");
    • groupWinners.forEach(
    • team => {
    • log("", " (Group: ", " Nationality: ", ")", team);
    • }
    • );
    • console.log("\nRunners-up:\n");
    • runnersUp.forEach(
    • team => {
    • log("", " (Group: ", " Nationality: ", ")", team);
    • }
    • );
    • console.log("\n\nLet's start the draw!\n");
    • draw();
    • console.log("THE QUARTER-FINALS: ");
    • console.log("
    • ");
    • pairings.forEach(pair => {
    • console.log(pair[0].name, pair[0].group, pair[0].nationality, " vs ",
    • pair[1].name, pair[1].group, pair[1].nationality );
    • });
    • function draw() {
    • /*arrays containing teams which can be still drawed and an array for the pairings*/
    • let groupWinnersToDraw = [...groupWinners];
    • let runnersUpToDraw = [...runnersUp];
    • let pairings = [];
    • /*while there are teams to draw */
    • while (runnersUpToDraw.length > 0) {
    • /*choose a random team from runnersUpToDraw */
    • let index = Math.floor(Math.random() * (runnersUpToDraw.length));
    • let teamToFace = runnersUpToDraw[index];
    • /*updating the runnersUpToDraw array*/
    • runnersUpToDraw = runnersUpToDraw
    • .slice(0, index)
    • .concat(
    • runnersUpToDraw.slice(index + 1, runnersUpToDraw.length)
    • );
    • log("\n\nTeam to face:", " (Group: ", " Nationality: ", ")", teamToFace);
    • console.log("\n");
    • /*selecting the potential opponents */
    • let potentialOpponents = groupWinnersToDraw.filter(
    • team =>
    • (team.group != teamToFace.group &&
    • team.nationality != teamToFace.nationality)
    • );
    • if (potentialOpponents.length == 0) {
    • potentialOpponents.push(
    • `No eligible opponent for ${teamToFace.name}! We have to re-draw!`
    • );
    • }
    • if (typeof potentialOpponents[0] == "string") {
    • console.log(potentialOpponents[0]);
    • return;
    • }
    • console.log("Potential opponents:\n");
    • potentialOpponents.forEach(
    • team =>
    • log("", " (Group: ", " Nationality: ", ")", team)
    • );
    • /*choose a random team from potentialOpponents */
    • let anotherIndex = Math.floor(Math.random() * (potentialOpponents.length));
    • let opponent = potentialOpponents[anotherIndex];
    • log("\n\nThe opponent is:", " (Group: ", " Nationality: ", ")", opponent);
    • /*updating the groupWinnersToDraw array*/
    • let yetAnotherIndex = 0;
    • for (let i = 0; i < groupWinnersToDraw.length; i++) {
    • if (groupWinnersToDraw[i].name == opponent.name) {
    • yetAnotherIndex = i;
    • }
    • }
    • groupWinnersToDraw = groupWinnersToDraw
    • .slice(0, yetAnotherIndex)
    • .concat(
    • groupWinnersToDraw.slice(yetAnotherIndex + 1, groupWinnersToDraw.length)
    • );
    • if (runnersUpToDraw.length > 0) {
    • console.log("\n\nRemaining group-winners:\n")
    • groupWinnersToDraw.forEach(
    • team => {
    • log("", " (Group: ", " Nationality: ", ")", team);
    • }
    • );
    • console.log("\nRemaining runners-up:\n");
    • runnersUpToDraw.forEach(
    • team => {
    • log("", " (Group: ", " Nationality: ", ")", team);
    • }
    • );
    • }
    • /*save the pairings */
    • let drawing = [];
    • drawing.push(teamToFace, opponent);
    • pairings.push(drawing);
    • }
    • /*log the draw */
    • console.log("
    • THE QUARTER-FINALS: \n");
    • pairings.forEach(
    • pair => {
    • console.log(
    • pair[0].name,
    • " (Group: ",
    • pair[0].group,
    • " Nationality: ",
    • pair[0].nationality,
    • ")",
    • " vs ",
    • pair[1].name,
    • " (Group: ",
    • pair[1].group,
    • " Nationality: ",
    • pair[1].nationality,
    • ")"
    • );
    • }
    • );
    • }
Code
Diff
  • using System.Collections.Generic;
    using System.Diagnostics;
    
    public class TowerOfHanoi
    {
        public static int Tower(int numOfDisks)
        {
          //only numOfDisks is given, fill in the other gaps, call the other method that moves disks, and return the return statement from the other method
          return Tower(numOfDisks, "source", "aux", "dest", new Dictionary<int, int>());
        }
    
        private static int Tower(int n, string source, string aux, string dest, Dictionary<int, int> cache)
        {
          //dictionary has keys and values.
           if (n > 0) // if one or more disks
            {
              if (cache.ContainsKey(n)) //check if dictionary contains an element with the specified key
              {
                  return cache[n]; 
              }
              //variable declared at method scope
              //outcome will be the number of moves in test
              var outcome = Tower(n - 1, source, aux, dest, cache); //move disk from rod to rod using recursion
              outcome += Tower(n - 1, aux, dest, source, cache); 
              outcome += 1;
              cache.Add(n, outcome); 
              return outcome; 
            }
            else { //if no disks given, no moves possible
              return 0;
            }
        }
    }
    
    //mathematical way to calculate moves
    //using System;
    //static int moves = 0;
    //method body
    // return moves = (int) Math.Pow(2,numOfDisks)-1;
    • using System.Collections.Generic;
    • using System.Diagnostics;
    • public class TowerOfHanoi
    • {
    • public static int Tower(int numOfDisks)
    • {
    • return 0;
    • //only numOfDisks is given, fill in the other gaps, call the other method that moves disks, and return the return statement from the other method
    • return Tower(numOfDisks, "source", "aux", "dest", new Dictionary<int, int>());
    • }
    • private static int Tower(int n, string source, string aux, string dest, Dictionary<int, int> cache)
    • {
    • return 0;
    • //dictionary has keys and values.
    • if (n > 0) // if one or more disks
    • {
    • if (cache.ContainsKey(n)) //check if dictionary contains an element with the specified key
    • {
    • return cache[n];
    • }
    • //variable declared at method scope
    • //outcome will be the number of moves in test
    • var outcome = Tower(n - 1, source, aux, dest, cache); //move disk from rod to rod using recursion
    • outcome += Tower(n - 1, aux, dest, source, cache);
    • outcome += 1;
    • cache.Add(n, outcome);
    • return outcome;
    • }
    • else { //if no disks given, no moves possible
    • return 0;
    • }
    • }
    • }
    • }
    • //mathematical way to calculate moves
    • //using System;
    • //static int moves = 0;
    • //method body
    • // return moves = (int) Math.Pow(2,numOfDisks)-1;
Strings
Data Types
Iterators
Control Flow
Object-oriented Programming
Basic Language Features
Fundamentals
Programming Paradigms
Code
Diff
  • removeEverySecond = lambda string: string[::2]
    • def removeEverySecond(string):
    • a = list(string)
    • b = a[::2]
    • b= "".join(b)
    • return b
    • removeEverySecond = lambda string: string[::2]
Code
Diff
  • public class FizzBuzz
    {
        public string GetOutput(int number) {
          if (number % 15 == 0) {
            return "FizzBuzz";
          }
          else if (number % 3 == 0) {
            return "Fizz";
          }
          else if (number % 5 == 0) {
            return "Buzz";
          }
          else {
            return number.ToString();
          }
          // Fizz buzz is a popular computer science interview question.  
          // The function above is given a number - if the number is
          // divisible by 3, return "fizz", if it's divisible by 5, 
          // return "buzz", if not divisble by 3 or 5 - return the
          // number itself.
        }
    }
    • public class FizzBuzz
    • {
    • public string GetOutput(int number) {
    • if (number % 3 == 0 && number % 5 == 0) {
    • if (number % 15 == 0) {
    • return "FizzBuzz";
    • }
    • else if (number % 3 == 0) {
    • return "Fizz";
    • }
    • else if (number % 5 == 0) {
    • return "Buzz";
    • }
    • else {
    • return number.ToString();
    • }
    • // Fizz buzz is a popular computer science interview question.
    • // The function above is given a number - if the number is
    • // divisible by 3, return "fizz", if it's divisible by 5,
    • // return "buzz", if not divisble by 3 or 5 - return the
    • // number itself.
    • }
    • }
Code
Diff
  • import java.util.Arrays;
    import java.util.Collections;
    
    public class Kata {
        public static int findMax(int[] my_array) {
            // Write a method that returns the largest integer in the list.
            // You can assume that the list has at least one element.
          
            // without using ready implementation
            return Arrays.stream(my_array)
                          .reduce(my_array[0], (acc, elem) -> acc > elem ? acc : elem);
        }
    }
    • import java.util.Arrays;
    • import java.util.Collections;
    • public class Kata {
    • public static int findMax(int[] my_array) {
    • // Write a method that returns the largest integer in the list.
    • // You can assume that the list has at least one element.
    • // without using ready implementation
    • return Arrays.stream(my_array)
    • .max()
    • .getAsInt();
    • .reduce(my_array[0], (acc, elem) -> acc > elem ? acc : elem);
    • }
    • }
Code
Diff
  • function removeEveryThird(str) {
      return (str[0] || '') + str
        .slice(1)
        .replace(/(..)./g, '$1')
    }
    • function removeEveryThird(str) {
    • return str
    • .split('')
    • .filter((x, i) => i === 0 || i % 3)
    • .join('')
    • return (str[0] || '') + str
    • .slice(1)
    • .replace(/(..)./g, '$1')
    • }