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
Code
Diff
  • import java.util.stream.IntStream;
    
    public class Area {
    
      public static long[] workOutArea(long[] values){
        if (values.length % 2 != 0) {
          return null;
        }
        
        return IntStream.range(0, values.length / 2).mapToLong(i -> values[i * 2] * values[i * 2 + 1]).toArray();
      }
    }
    • import java.util.stream.IntStream;
    • public class Area {
    • public static long[] workOutArea(long[] values){
    • if (values.length % 2 != 0) {
    • return null;
    • }
    • return IntStream.range(0, values.length / 2).mapToLong(i -> values[i * 2] * values[i * 2 + 1]).toArray();
    • }
    • }
Code
Diff
  • function wordCount(str) {
      return str.replace(/\s/g, '').length ? str.trim().split(/\s+/).length : 0;
    }
    • function wordCount(str) {
    • var words = 1;
    • for(var i = 1; i < str.length; i++) {
    • if(str[i] == " " && str[i - 1] !== " "&& str[i + 1] !== " ") {
    • words++;
    • }
    • }
    • return words;
    • }
    • return str.replace(/\s/g, '').length ? str.trim().split(/\s+/).length : 0;
    • }
Code
Diff
  • package com.mystuff.juststuff;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.time.format.DateTimeParseException;
    import java.util.Arrays;
    import java.util.Set;
    import java.util.SortedSet;
    import java.util.TreeSet;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    import java.util.stream.IntStream;
    
    public class Palindrome {
    
      private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    
      public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
        final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate));
        return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear())
            .mapToObj(Palindrome::createPalindrome)
            .filter(isDateInRange(sortedDates.first(), sortedDates.last()))
            .collect(Collectors.toCollection(TreeSet::new));
      }
    
      private static LocalDate createPalindrome(final int year) {
        final String yearStr = String.valueOf(year);
        final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString();
        try {
          return LocalDate.parse(datePalindrome, formatter);
        } catch (final DateTimeParseException e) {}
        return null;
      }
    
      private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) {
        return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate));
      }
    }
    • package com.mystuff.juststuff;
    • import java.time.LocalDate;
    • import java.time.format.DateTimeFormatter;
    • import java.time.format.DateTimeParseException;
    • import java.util.Arrays;
    • import java.util.Set;
    • import java.util.SortedSet;
    • import java.util.TreeSet;
    • import java.util.function.Predicate;
    • import java.util.stream.Collectors;
    • import java.util.stream.IntStream;
    • public class Palindrome {
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome)
    • .filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static LocalDate createPalindrome(int year) {
    • String yearStr = String.valueOf(year);
    • String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();
    • try {
    • return LocalDate.parse(datePalindrome, formatter);
    • } catch (DateTimeParseException e) {
    • return null;
    • }
    • }
    • private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {
    • if (date == null)
    • return false;
    • if (startDate.isEqual(date) || endDate.isEqual(date))
    • return true;
    • return startDate.isBefore(date) && endDate.isAfter(date) ||
    • endDate.isBefore(date) && startDate.isAfter(date);
    • }
    • public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
    • final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate));
    • return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear())
    • .mapToObj(Palindrome::createPalindrome)
    • .filter(isDateInRange(sortedDates.first(), sortedDates.last()))
    • .collect(Collectors.toCollection(TreeSet::new));
    • }
    • private static LocalDate createPalindrome(final int year) {
    • final String yearStr = String.valueOf(year);
    • final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString();
    • try {
    • return LocalDate.parse(datePalindrome, formatter);
    • } catch (final DateTimeParseException e) {}
    • return null;
    • }
    • private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) {
    • return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate));
    • }
    • }
Code
Diff
  • function nextGeneration(grid) {
      return grid.map((row, rowIndex) => {
        return row.map((cell, colIndex) => {
          if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
            let neighboursCount = 0;
            if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++;
            if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++;
            if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++;
            if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++;
            if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++;
            if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++;
            if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++;
            if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++;
          
            if (cell === 1) {
              if (neighboursCount === 2 || neighboursCount === 3 ) {
                return 1;
              }
            } else {
              if (neighboursCount === 3 ) {
                return 1;
              }
            }
            return 0;
          }
          return 0;
        });
      });
    }
    • function nextGeneration(grid) {
    • return grid;
    • return grid.map((row, rowIndex) => {
    • return row.map((cell, colIndex) => {
    • if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
    • let neighboursCount = 0;
    • if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++;
    • if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++;
    • if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++;
    • if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++;
    • if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++;
    • if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++;
    • if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++;
    • if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++;
    • if (cell === 1) {
    • if (neighboursCount === 2 || neighboursCount === 3 ) {
    • return 1;
    • }
    • } else {
    • if (neighboursCount === 3 ) {
    • return 1;
    • }
    • }
    • return 0;
    • }
    • return 0;
    • });
    • });
    • }
Code
Diff
  • import java.util.*;
    class Solution {
      
    	      private int[] getNumDigits(int number) {
            int[] digits = new int[10];
    
            while (number != 0) {
                digits[number % 10]++;
                number = number / 10;
            }
    
            return digits;
        }
    
        public int retSmallestPositiveInteger() {
    
            for (int num = 100; num < Integer.MAX_VALUE; num++) {
                boolean isEquals = true;
                int[] digits = getNumDigits(num);
                for (int i = 2; (i <= 6) && isEquals; i++) {
                    isEquals = isEquals && Arrays.equals(digits, getNumDigits(num * i));
                }
                if (isEquals)
                    return num;
            }
            return 0;
        }
    }
    • import java.util.*;
    • class Solution {
    • public static int retSmallestPositiveInteger() {
    • for(int i=1; ; i++) {
    • if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))
    • return i;
    • }
    • }
    • private static boolean hasSameDigits(int x, int y) {
    • char[] xdigits = Integer.toString(x).toCharArray();
    • char[] ydigits = Integer.toString(y).toCharArray();
    • Arrays.sort(xdigits);
    • Arrays.sort(ydigits);
    • return Arrays.equals(xdigits, ydigits);
    • }
    • private int[] getNumDigits(int number) {
    • int[] digits = new int[10];
    • while (number != 0) {
    • digits[number % 10]++;
    • number = number / 10;
    • }
    • return digits;
    • }
    • public int retSmallestPositiveInteger() {
    • for (int num = 100; num < Integer.MAX_VALUE; num++) {
    • boolean isEquals = true;
    • int[] digits = getNumDigits(num);
    • for (int i = 2; (i <= 6) && isEquals; i++) {
    • isEquals = isEquals && Arrays.equals(digits, getNumDigits(num * i));
    • }
    • if (isEquals)
    • return num;
    • }
    • return 0;
    • }
    • }
Numbers
Data Types
Integers
Algorithms
Logic

Find number of digits with an unrolled div loop.

Code
Diff
  • // Unrolled div loop
    fn digits(mut n: u64) -> usize {
      let mut l = 1;
      loop {
        if n < 10 {
          return l;
        }
        if n < 100 {
          return l + 1;
        }
        if n < 1000 {
          return l + 2;
        }
        if n < 10000 {
          return l + 3;
        }
        n /= 10000;
        l += 4;
      }
    }
    • // Unrolled div loop
    • fn digits(mut n: u64) -> usize {
    • let mut l = 1;
    • while n >= 10 {
    • n /= 10;
    • l += 1;
    • let mut l = 1;
    • loop {
    • if n < 10 {
    • return l;
    • }
    • l
    • if n < 100 {
    • return l + 1;
    • }
    • if n < 1000 {
    • return l + 2;
    • }
    • if n < 10000 {
    • return l + 3;
    • }
    • n /= 10000;
    • l += 4;
    • }
    • }
Fundamentals
Code
Diff
  • #include <iostream>
    #include <string>
    using namespace std ;
    
    int helloCplusplus(){
      string str = "Hello, C++!";
      cout << str << '\n';
      return 0;
    }
    • #include <iostream>
    • #include <string>
    • using namespace std ;
    • int helloCplusplus(){
    • std::string str = "Hello, C++!";
    • std::cout << str << '
    • ';
    • string str = "Hello, C++!";
    • cout << str << '
    • ';
    • return 0;
    • }
Code
Diff
  • # write a add function that doesn't use the plus symbol
    def add(a, b)
      a + b
    end
    
    # 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;
    • }
    • # write a add function that doesn't use the plus symbol
    • def add(a, b)
    • a + b
    • end
    • // Make sure to view the test cases to see how this test fails
    • # Make sure to view the test cases to see how this test fails
Strings
Data Types
Code
Diff
  • def last_char(str):
      return str[len(str)-1]
    • def last_char(str):
    • return str[-1]
    • return str[len(str)-1]
Code
Diff
  • var getMin = function (list){
      var list = (!list) ? 0 : list.filter(function(v){ return (v > 0 && v < Number.MAX_VALUE); });
      return Math.min.apply(null,list) | 0;
    }
    • var getMin = function (list){
    • var min = Number.MAX_VALUE;
    • for (var i = 0; i < list.length; i++) {
    • if (+list[i] <= 0) {
    • continue;
    • }
    • min = Math.min(min, +list[i]);
    • }
    • return min = min === Number.MAX_VALUE ? 0 : min;
    • var list = (!list) ? 0 : list.filter(function(v){ return (v > 0 && v < Number.MAX_VALUE); });
    • return Math.min.apply(null,list) | 0;
    • }