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

added a bigger prime number to test and fixed the bug that one was a prime, since it is not.

Code
Diff
  • public class Primes {
      public static boolean isAPrime(int number) {
        if(number > 1 && number == 2) return true; //1 is not a prime number by definition
        else {
          for(int i = 3; i*i < number; i +=2) {
            if (number % i == 0) return false;
          }
        }
        return true;
      }
    }
    • public class Primes {
    • public static boolean isAPrime(int number) {
    • if(number == 1 || number == 2) return true;
    • if(number > 1 && number == 2) return true; //1 is not a prime number by definition
    • else {
    • for(int i = 3; i*i < number; i +=2) {
    • if (number % i == 0) return false;
    • }
    • }
    • return true;
    • }
    • }

Changing "if n % i == 0" on line 6 to "if not(n % i)" made the time drop from 4126ms to 3810ms (as the longest time).

Code
Diff
  • from math import sqrt, floor
    
    def divisors(n):
        fact = [];
        for i in range(1, int(floor(sqrt(n))) + 1):
            if not(n % i):
                fact.append(i)
                if n / i != i:
                    fact.append(n / i)
        fact.sort()
        return fact
    • from math import sqrt, floor
    • def divisors(n):
    • fact = [];
    • for i in range(1, int(floor(sqrt(n))) + 1):
    • if n % i == 0:
    • if not(n % i):
    • fact.append(i)
    • if n / i != i:
    • fact.append(n / i)
    • fact.sort()
    • return fact
Numbers
Data Types
Integers
Algorithms
Logic

Translation of henninglive's unrolled divison loop for calculating amount of digits to C#

Code
Diff
  • public class Kumite
    {
    // Unrolled div loop
      public static int Digits(ulong n)
      {
        var l = 1;
        while(true)
        {
          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;
        }
      }
    }
    • public class Kumite
    • {
    • // Unrolled div loop
    • fn digits(mut n: u64) -> usize {
    • let mut l = 1;
    • loop {
    • if n < 10 {
    • return l;
    • public static int Digits(ulong n)
    • {
    • var l = 1;
    • while(true)
    • {
    • 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;
    • }
    • if n < 100 {
    • return l + 1;
    • }
    • if n < 1000 {
    • return l + 2;
    • }
    • if n < 10000 {
    • return l + 3;
    • }
    • n /= 10000;
    • l += 4;
    • }
    • }

Quick implementation of splitter algorithm with use of regular expressions.

Probably could be done better if offset characters would be cut off & added to result array rather than adding some random chars to _str and removing them right before returning result.

Also instead of using regexp straightforward for-loop can make it better - because of lack of positive lookbehind in js rexegp implementations results are not always OK (problem with strings which length is not divisible by _n).

Code
Diff
  • function splitter (_str, _n) {
      _n = parseInt(_n);
      
      var offset = _n - _str.length % _n;
      
      if (offset !== _n) {
        _str += Math.pow(10, offset - 1);
      }
      
      var result = _str.split(new RegExp('(?=(?:.{' + _n + '})+$)', 'g'));
      
      if (offset !== _n) {
        var len = result.length,
            lastGroup = result[len - 1];
        
        result[len - 1] = lastGroup.substring(0, lastGroup.length - offset);
      }
      
      return result;
    }
    • console.log(
    • '123456'.split('')
    • );
    • function splitter (_str, _n) {
    • _n = parseInt(_n);
    • var offset = _n - _str.length % _n;
    • if (offset !== _n) {
    • _str += Math.pow(10, offset - 1);
    • }
    • var result = _str.split(new RegExp('(?=(?:.{' + _n + '})+$)', 'g'));
    • if (offset !== _n) {
    • var len = result.length,
    • lastGroup = result[len - 1];
    • result[len - 1] = lastGroup.substring(0, lastGroup.length - offset);
    • }
    • return result;
    • }
Code
Diff
  • require 'rubygems'
    
    #gems are stored in a hash with the key set to the name of the gem and the value is the version.
    #the trick is to get the hash to an array and join the version number to the gem name.
    #Feedback appreciated.
    
    def local_gems
       Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
    end
    
    puts local_gems.map{ |name, specs| 
      [ 
        name,
        specs.map{ |spec| spec.version.to_s }.join(',')
      ].join(' ') 
    }
    • puts `gem list`
    • require 'rubygems'
    • #gems are stored in a hash with the key set to the name of the gem and the value is the version.
    • #the trick is to get the hash to an array and join the version number to the gem name.
    • #Feedback appreciated.
    • def local_gems
    • Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
    • end
    • puts local_gems.map{ |name, specs|
    • [
    • name,
    • specs.map{ |spec| spec.version.to_s }.join(',')
    • ].join(' ')
    • }
Code
Diff
  • require 'prime'
    
    def is_prime(n)
       n.prime?
    end
    • require 'prime'
    • def is_prime(n)
    • n.prime?
    • end
ES2015
Babel
Objects
Data Types

Joeun's implementation of the pick function in the 'underscore' library. I have extended the library with a new function with a clever name. :) Called 'flick' which is the inverse of 'pick'. Some object/booger picking operations. lol

Code
Diff
  • var _ = {};
    
    // Joeun's Pick implementation (GREAT JOB!)
    _.pick = (target, ...keys) => {
      if (typeof keys[0] == 'function') {
    
        var predicate = keys[0];
        keys = Object.keys(target);
        
        return keys.reduce((obj, key) => {
          return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj;
        }, {})
      }
    
      return keys.reduce((obj, key) => {
        return obj[key] = target[key], obj;
      }, {});
    };
    
    // Robert.Cutright's implementation of the inverse of pick (called flick);
    // In other words, you supply the keys you want to throw away.
    _.flick = (target, ...keys) => {
      if (typeof keys[0] == 'function') {
    
        var predicate = keys[0];
        keys = Object.keys(target);
        
        return keys.reduce((obj, key) => {
          return predicate(target[key], key, target) ? obj : (obj[key] = target[key], obj);
        }, {})
      }
    
      var obj = Object.assign({}, target);
      Object.keys(obj).filter(key => keys.includes(key) ? delete obj[key] : obj[key]);
      return obj;
    };
    
    • var _ = {};
    • // Joeun's Pick implementation (GREAT JOB!)
    • _.pick = (target, ...keys) => {
    • if (typeof keys[0] == 'function') {
    • var predicate = keys[0];
    • keys = Object.keys(target);
    • return keys.reduce((obj, key) => {
    • return predicate(target[key], key, target) ? (obj[key] = target[key], obj) : obj;
    • }, {})
    • }
    • return keys.reduce((obj, key) => {
    • return obj[key] = target[key], obj;
    • }, {});
    • };
    • };
    • // Robert.Cutright's implementation of the inverse of pick (called flick);
    • // In other words, you supply the keys you want to throw away.
    • _.flick = (target, ...keys) => {
    • if (typeof keys[0] == 'function') {
    • var predicate = keys[0];
    • keys = Object.keys(target);
    • return keys.reduce((obj, key) => {
    • return predicate(target[key], key, target) ? obj : (obj[key] = target[key], obj);
    • }, {})
    • }
    • var obj = Object.assign({}, target);
    • Object.keys(obj).filter(key => keys.includes(key) ? delete obj[key] : obj[key]);
    • return obj;
    • };
Code
Diff
  • using System;
    class Kata {
        public static string Main(string greeting, string language) {
          Console.WriteLine(Greeting(greeting, language));
          return Greeting(greeting, language);
        }
        
        public static string Greeting(string greeting, string language) => $"{greeting} {language}!";
    }
    • using System;
    • class Kata {
    • public static string Main(string greeting, string language) {
    • Console.WriteLine(Greeting(greeting, language));
    • return Greeting(greeting, language);
    • }
    • public static string Greeting(string greeting, string language) => $"{greeting}, {language}!";
    • public static string Greeting(string greeting, string language) => $"{greeting} {language}!";
    • }
Algorithms
Logic
Mathematics
Numbers

For greater efficiency, I give you a lookup table that cuts down execution time by up to 40%!

6035ms from 10958ms

Code
Diff
  • def factorial(x):
        result = 1
        if len(factorial.lookup) <= x:
            factorial.lookup = [1] * (x + 1)
            for i in range(2, x + 1):
                result *= i
                factorial.lookup[i] = result
            return result
        return factorial.lookup[x]
    factorial.lookup = []
    • def factorial(x):
    • result = 1
    • for i in range(2, x + 1):
    • result *= i
    • return result
    • if len(factorial.lookup) <= x:
    • factorial.lookup = [1] * (x + 1)
    • for i in range(2, x + 1):
    • result *= i
    • factorial.lookup[i] = result
    • return result
    • return factorial.lookup[x]
    • factorial.lookup = []