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

Using array methods to solve this issue.

Code
Diff
  • function glutenDetector(ingredients){
      const gluten = [
      "wheat",
      "wheat flour",
      "triticale",
      "barley",
      "rye",
      "brewer's yeast",
      "malt",
      "wheatberries",
      "durum",
      "emmer",
      "semolina",
      "spelt",
      "farina",
      "farro",
      "graham",
      "kamut",
      "einkorn"
      ];
      
      return ingredients
              .split(/[,()]/)
              .map((el) => el.trim().toLowerCase())
              .some((el) => gluten.includes(el));
    }
    • function glutenDetector(ingredients){
    • var gluten = [
    • const gluten = [
    • "wheat",
    • "wheat flour",
    • "triticale",
    • "barley",
    • "rye",
    • "brewer's yeast",
    • "malt",
    • "wheatberries",
    • "durum",
    • "emmer",
    • "semolina",
    • "spelt",
    • "farina",
    • "farro",
    • "graham",
    • "kamut",
    • "einkorn"
    • ];
    • var glutenDetect = false;
    • var ingredientsList = ingredients.split(',');
    • ingredientsList.forEach(function(ingredient){
    • var ingredientClean = ingredient.trim().toLowerCase();
    • gluten.forEach(function(glutenIngredient){
    • if(ingredientClean.indexOf(glutenIngredient) !== -1){
    • glutenDetect = true;
    • }
    • });
    • });
    • return glutenDetect;
    • return ingredients
    • .split(/[,()]/)
    • .map((el) => el.trim().toLowerCase())
    • .some((el) => gluten.includes(el));
    • }

Outputs a space-separated list of all integers from 0 to the input byte in descending order. This is a step towards donaldsebleung's goal of 99 Bottles of Beer.

Code
Diff
  • ,[[->+>+<<]>>[-<<+>>]<[->>+>+>>-<<----------[[+]>>+<<]>>[[+]<<<----------<+>>>>]<<<[->+>+<<]>>[-<<+>>]<<<<]>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]>[-]<++++++++++++++++++++++++++++++++.[-]<<<-]++++++++++++++++++++++++++++++++++++++++++++++++.
    • ,[->>+>+>>-<<----------[[+]>>+<<]>>[[+]<<<----------<+>>>>]<<<[->+>+<<]>>[-<<+>>]<<<<]>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]>++++++++++++++++++++++++++++++++++++++++++++++++.
    • ,[[->+>+<<]>>[-<<+>>]<[->>+>+>>-<<----------[[+]>>+<<]>>[[+]<<<----------<+>>>>]<<<[->+>+<<]>>[-<<+>>]<<<<]>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]>[-]<++++++++++++++++++++++++++++++++.[-]<<<-]++++++++++++++++++++++++++++++++++++++++++++++++.
Code
Diff
  • /* Array.reduce(
     * (accumulator, currentValue, currentIndex, array) => {
     * return accumulator + currentValue;
     * },
     * initialValue
     * );
     */
    function sumOfElements(arr) { 
      if(arr==undefined) return 0;
      return arr.reduce((sum, currentValue) => sum + currentValue,0);
    }
    • /* Array.reduce(
    • * (accumulator, currentValue, currentIndex, array) => {
    • * return accumulator + currentValue;
    • * },
    • * initialValue
    • * );
    • */
    • function sumOfElements(arr) {
    • return arr ? arr.reduce( (sum, currentValue) => sum + currentValue) : 0;
    • function sumOfElements(arr) {
    • if(arr==undefined) return 0;
    • return arr.reduce((sum, currentValue) => sum + currentValue,0);
    • }

This kumite is not working as intended so just testing something.

Code
Diff
  • func hello() { 
    
    print("Hello Swift 4!")
    
    }
    • print("Hello Swift!")
    • func hello() {
    • print("Hello Swift 4!")
    • }

No need to declare number outside of for loop.

Code
Diff
  • public class CountTheDigit {
    	public static int nbDig(int n, int d) {
        int count=0;
        for(int i=0; i<n; i++) {
    			String number=(i*i)+"";
    			count +=  number.length() - number.replace(d+"", "").length();
    	  }
      	return count;
      }
    }
    • public class CountTheDigit {
    • public static int nbDig(int n, int d) {
    • String number = "";
    • int count=0;
    • for(int i=0; i<n; i++) {
    • number=(i*i)+"";
    • String number=(i*i)+"";
    • count += number.length() - number.replace(d+"", "").length();
    • }
    • return count;
    • }
    • }

refactorting

Code
Diff
  • function nextGeneration(grid) {
        // pregenerated coords
        const neighboursCords = [[0, 1], [0, -1], [1, 1], [1, 0], [1, -1], [-1, 1], [-1, 0], [-1, -1]];
        const neighboursCollector = (rowIndex, columnIndex) => ([x, y]) => grid[rowIndex + x][columnIndex + y];
        const sum = (a, b) => a + b;
    
        return grid.map((row, rowIndex) =>
            row.map((column, columnIndex) => {
    
                const isValid = rowIndex !== 0 && columnIndex !== 0 &&
                    rowIndex < grid.length - 1 && columnIndex < row.length - 1;
    
                if (!isValid) return 0;
    
                const currentCellValuesCollector = neighboursCollector(rowIndex, columnIndex);
                const neighboursCount = neighboursCords
                    .map(currentCellValuesCollector)
                    .reduce(sum);
    
                return +(neighboursCount === 3 || (column === 1 && neighboursCount === 2));
            })
        );
    }
    • function nextGeneration(grid) {
    • var neighboursCords = [[0, 1], [0, -1], [1, 1], [1, 0], [1, -1], [-1, 1], [-1, 0], [-1, -1]];
    • return grid.map((r, ri) =>
    • r.map((c, ci) => {
    • if (ri !== 0 && ci !== 0 && ri < grid.length - 1 && ci < r.length - 1) {
    • let neighboursCount = neighboursCords
    • .reduce((s, v) => s += grid[ri + v[0]][ci + v[1]], 0);
    • return 0 + (neighboursCount === 3 || (c === 1 && neighboursCount === 2));
    • }
    • return 0;
    • })
    • );
    • // pregenerated coords
    • const neighboursCords = [[0, 1], [0, -1], [1, 1], [1, 0], [1, -1], [-1, 1], [-1, 0], [-1, -1]];
    • const neighboursCollector = (rowIndex, columnIndex) => ([x, y]) => grid[rowIndex + x][columnIndex + y];
    • const sum = (a, b) => a + b;
    • return grid.map((row, rowIndex) =>
    • row.map((column, columnIndex) => {
    • const isValid = rowIndex !== 0 && columnIndex !== 0 &&
    • rowIndex < grid.length - 1 && columnIndex < row.length - 1;
    • if (!isValid) return 0;
    • const currentCellValuesCollector = neighboursCollector(rowIndex, columnIndex);
    • const neighboursCount = neighboursCords
    • .map(currentCellValuesCollector)
    • .reduce(sum);
    • return +(neighboursCount === 3 || (column === 1 && neighboursCount === 2));
    • })
    • );
    • }
Code
Diff
  • #!/bin/bash
    for i in $(eval echo {1..$3});
    do
    if [ $(( $i % ( $1*$2) )) -eq 0 ]
    then
    echo $i
    fi
    done
    • #!/bin/bash
    • for i in $(eval echo {1..$3});
    • do
    • if [ `expr $i % $1` -eq 0 -a `expr $i % $2` -eq 0 ]
    • if [ $(( $i % ( $1*$2) )) -eq 0 ]
    • then
    • echo $i
    • fi
    • done
Code
Diff
  • function wordCount(str) {
      return str.replace(/\s/g, '').length ? str.trim().split(/\s+/).length : 0;
    }
    • function wordCount(str) {
    • return str.length > 2 ? str.split(' ').length : 1;
    • return str.replace(/\s/g, '').length ? str.trim().split(/\s+/).length : 0;
    • }

I've made changes that sum method ignores everything but numbers
Also I've refactored code to ES6

Code
Diff
  • const sum = arr => {
      const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sum
      return arr.reduce(reducer, 0);
    }
    
    
    • function sum(arr) {
    • return arr.reduce((sum, elem) => sum + elem, 0);
    • }
    • const sum = arr => {
    • const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sum
    • return arr.reduce(reducer, 0);
    • }