Begin a new Kumite
Search
About
  • Filter by Language:
  • 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.

function primesLessThan(limit) {
    var primes = [];
    if (limit >= 2) {
        var sqrtlmt = Math.sqrt(limit) - 2;
        var nums = new Array(); // start with an empty Array...
        for (var i = 2; i <= limit; i++) // and
            nums.push(i); // only initialize the Array once...
        for (var i = 0; i <= sqrtlmt; i++) {
            var p = nums[i]
            if (p)
                for (var j = p * p - 2; j < nums.length; j += p)
                    nums[j] = 0;
        }
        for (var i = 0; i < nums.length - 1; i++) {
            var p = nums[i];
            if (p) primes.push(p);
        }
    }
    return primes;
}
require 'prime'

def primes_less_than(n)
    Prime.take_while {|p| p < n }
end
import numpy

def primes(n):
    sieve = numpy.ones(n/3 + (n%6==2), dtype=numpy.bool)
    for j in xrange(1,int(n**0.5)/3+1):
        if sieve[j]:
            k=3*j+1|1
            sieve[k*k/3::2*k] = False
            sieve[k*(k-2*(j&1)+4)/3::2*k] = False
    return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0][1:]+1)|1)]

def primes_less_than(n):
    #n should be higher or equal 6. If n is a prime will not be part of the result """
    return primes(n).tolist()
Tips & Tricks

This example shows how to do random numbers in Swift.

import Glibc
for count in 1...20 {
  print(random() % 100)
}

Explanation

The awk command takes the input from /proc/cpuinfo, matches lines containing "cpu MHz", and appends the " / 1000" to the CPU frequency, so it's ready for piping to bc
The echo scale=2 is for bc, to get floating point numbers with a precision of maximum two decimal points
Group the echo scale=2 and the awk for piping to bc, by enclosing the commands within { ...; }
Run the commands in a $(...) subshell
Wrap the subshell within (...) to store the output lines as an array

From the cpus array, you can extract the individual CPU values with:

cpu0=${cpus[0]}
cpu1=${cpus[1]}
cpu2=${cpus[2]}
cpu3=${cpus[3]}

If you don't need the values in GHz, but MHz is enough, then the command is a lot simpler:

cpus=($(awk '/cpu MHz/ {print $4}' /proc/cpuinfo))

Limitations

Arrays are Bash specific, might not work in older /bin/sh.

/proc/cpuinfo exists only in Linux.

cpus=($({ echo scale=2; awk '/cpu MHz/ {print $4 " / 1000"}' /proc/cpuinfo; } | bc))
type Person = { Name : string; Gender : string }

let alice = { Name = "Alice"; Gender = "Female" }
let bob = { Name = "Bob"; Gender = "Male" }

let femaleOrMale p =
    match p with
    | { Gender = "Female" } -> p.Name + " is female."
    | { Gender = "Male" } -> p.Name + " is male."
    | _ -> "???"
Code
Diff
  • function getMin(list) {
        let ret = list.reduce((prev, curr) => {
            if (curr >= 1) {
                if (prev <= 0) {
                    return curr;
                }
                else {
                    return Math.min(curr, prev);
                }
            }
            return prev;
        }, -1);
        return ret >= 1 ? ret : 0;
    }
  • 1
    var getMin = function (list){
    
    2
      var min = Number.MAX_VALUE;
    
    3
      for (var i = 0; i < list.length; i++) {
    
    4
        if (+list[i] <= 0) {
    
    5
          continue;
    
    6
        }
    
    7
        min = Math.min(min, +list[i]);
    
    8
      }
    
    9
      return min = min === Number.MAX_VALUE ? 0 : min;
    
    1+
    function getMin(list) {
    
    2+
        let ret = list.reduce((prev, curr) => {
    
    3+
            if (curr >= 1) {
    
    4+
                if (prev <= 0) {
    
    5+
                    return curr;
    
    6+
                }
    
    7+
                else {
    
    8+
                    return Math.min(curr, prev);
    
    9+
                }
    
    10+
            }
    
    11+
            return prev;
    
    12+
        }, -1);
    
    13+
        return ret >= 1 ? ret : 0;
    
    1010
    }
    

Recent Moves:

Fundamentals
Strings
Code
Diff
  • function minutes(num){
    if(num<0)
    {
      num=-num;
    }
      const minutes = num % 60
      const hours = (num - minutes)/60
      var result="";
      
    
      result+=(hours+":");
      if(minutes<10)
      {
        result+=("0");
        result+=(minutes);
      }else{
        result+=(minutes);
      }
      return result;
    }
    console.log(minutes(120))
  • 11
    function minutes(num){
    
    2+
    if(num<0)
    
    3+
    {
    
    4+
      num=-num;
    
    5+
    }
    
    22
      const minutes = num % 60
    
    33
      const hours = (num - minutes)/60
    
    4
      return `${hours}:${("0" + minutes).slice(-2)}`
    
    8+
      var result="";
    
    9+
      
    
    10+
    11+
      result+=(hours+":");
    
    12+
      if(minutes<10)
    
    13+
      {
    
    14+
        result+=("0");
    
    15+
        result+=(minutes);
    
    16+
      }else{
    
    17+
        result+=(minutes);
    
    18+
      }
    
    19+
      return result;
    
    55
    }
    
    21+
    console.log(minutes(120))
    

Recent Moves: