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

Find the longest word in a given string.
Assume all strings will contain only letters and spaces.

function longestString(str){
  return str.split(' ').sort(function(a,b) { 
   return b.length - a.length
  })[0]
}

For a given number (num) add up all numbers from 0 to num.
This is similar to a factorial, but using addition.

function numSum(num){
  var sum = 0;
  for (var i = 1; num >= i; i++) {
   sum += i
  }
  return sum
};
Fundamentals
Strings
Data Types

You will be given a number of minutes. Your task is to return a string that formats the number into 'hours:minutes". Make sure you always return two intigers for the minutes section of the conversion.
ie ('0:01' instead of '0:1')

Example:

minutes(90) => '1:30'

function minutes(num){
  return num % 60 < 10 ? Math.floor(num/60) + ':' + '0' + (num % 60) : Math.floor(num/60) + ':' + (num % 60)
}
Fundamentals
Arrays
Data Types

Given an array of numbers, return the third greatest number in the array.
You will always be given an array of valid numbers with at least three values.

Ex:
thirdGreatest([4,8,1,5,3]) -> 4

function thirdGreatest(arr){
  return arr.sort( (a,b) => a - b)[arr.length-3]
}
let sq = seq {
    yield 1
    yield! seq { 2 .. 6 }
    yield 7
    yield! seq [8; 9; 10]
}

let count = Seq.length sq
let list = List.ofSeq sq
type SafeStringNumbers() =

    member this.Bind(x, f) =
        match System.Int32.TryParse(x) with 
        | (true, v) -> f v
        | _ -> f 0

    member this.Return(x) =
        x

let safeStringNumbers = new SafeStringNumbers()

let v1 = safeStringNumbers {
    let! a = "1"
    let! b = "2"
    let! c = "3"
    return a + b + c
}

let v2 = safeStringNumbers {
    let! a = "1"
    let! b = "foo"
    let! c = "3"
    return a + b + c
}

get smallest positive number form array.
if no positive number in array, return 0.
example:

var list1 = [0, 1, 1, 3];
var list2 = [7, 2, null, 3, 0, 10];
var list2 = [null];
getMin(list1); //should return 1
getMin(list2); //should return 2
getMin(list3); //should return 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;
}
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."
    | _ -> "???"

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))
Tips & Tricks

This example shows how to do random numbers in Swift.

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