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

As my debute kata met no enthusiasm from community (and my own solution was claimed inefficient), I'll go with my second attempt on concise and fast array chunking contest:

The task is fairly simple - slice arbitrary array into chunks of size m (without dropping the remainder of length less than m).

Big arrays are thrown in.

Less than 100 chars should be consumed for your code.

No libraries allowed.

const splitArr = (a,s) => a.reduceRight((r,_,__,f) => [...r, f.splice(0, s)],[]);
Advanced Language Features
Fundamentals
Theorem Proving

In Coq, every recursive definition has to provably terminate on all possible inputs; otherwise, the logical consistency of Coq would be undermined. However, it is possible to define well-founded recursive functions which fail to pass Coq's built-in termination checker since it only does a very simple check to ensure that the Fixpoint being defined is structurally recursive:

Fail Fixpoint interleave {A} (l1 l2 : list A) : list A :=
  match l1 with
  | [] => l2
  | x :: xs => x :: interleave l2 xs
  end.
(* => The command has indeed failed with message:
      Cannot guess decreasing argument of fix. *)

One way to define non-structurally-recursive functions which provably terminate in Coq is to use the Function feature. To use the Function feature, one has to first Require Import Recdef before defining the Function and specifying a measure that decreases on every recursive call, followed by a proof that the measure indeed decreases as required ending with Defined instead of the usual Qed. However, in our particular case, even Function fails us since it requires the measure to refer to exactly one argument in the function:

Require Import Recdef.

Fail Function interleave {A} (l1 l2 : list A) {measure length (l1 ++ l2)} : list A :=
  match l1 with
  | [] => l2
  | x :: xs => x :: interleave l2 xs
  end.
(* => The command has indeed failed with message:
      Recursive argument must be specified *)

In this case, we need to use Program Fixpoint instead which is very similar to Function except that the decreasing measure can refer to more than one argument. To use Program Fixpoint, one has to first import Coq.Program.Wf:

From Coq Require Import Program.Wf.

Program Fixpoint interleave {A} (l1 l2 : list A) {measure (length (l1 ++ l2))} : list A :=
  match l1 with
  | [] => l2
  | x :: xs => x :: interleave l2 xs
  end.

However, if we try to use the Program Fixpoint at this stage, Coq will complain that it is not found in the current environment:

Fail Compute (interleave [1;2;3] [4;5;6]).
(* => The command has indeed failed with message:
      The reference interleave was not found
      in the current environment. *)

This is because we haven't proven to Coq that it terminates yet. To do that, we have to focus each proof obligation generated by Program Fixpoint (1 in this particular example) using Next Obligation followed its proof. It is OK to end the proofs with Qed when using Program Fixpoint:

Next Obligation.
  simpl.
  repeat rewrite app_length.
  omega.
Qed.

Now we can compute with it:

Compute (interleave [1;2;3] [4;5;6]).
(* => = [1; 4; 2; 5; 3; 6]
      : list nat *)
From Coq Require Import Lists.List omega.Omega.
Import ListNotations.

Fail Fixpoint interleave {A} (l1 l2 : list A) : list A :=
  match l1 with
  | [] => l2
  | x :: xs => x :: interleave l2 xs
  end.

Require Import Recdef.

Fail Function interleave {A} (l1 l2 : list A) {measure length (l1 ++ l2)} : list A :=
  match l1 with
  | [] => l2
  | x :: xs => x :: interleave l2 xs
  end.

From Coq Require Import Program.Wf.

Program Fixpoint interleave {A} (l1 l2 : list A) {measure (length (l1 ++ l2))} : list A :=
  match l1 with
  | [] => l2
  | x :: xs => x :: interleave l2 xs
  end.

Fail Compute (interleave [1;2;3] [4;5;6]).

Next Obligation.
  simpl.
  repeat rewrite app_length.
  omega.
Qed.

Compute (interleave [1;2;3] [4;5;6]).

Example test_interleave1:
  interleave [1;2;3] [4;5;6] = [1;4;2;5;3;6].
Proof. reflexivity. Qed.
Example test_interleave2:
  interleave [1] [4;5;6] = [1;4;5;6].
Proof. reflexivity. Qed.
Example test_interleave3:
  interleave [1;2;3] [4] = [1;4;2;3].
Proof. reflexivity. Qed.
Example test_interleave4:
  interleave [] [20;30] = [20;30].
Proof. reflexivity. Qed.

You have to create a function that given an array of integers returns the largest product that can be made by multiplying any 3 integers in the array.

Example:

[-4, -4, 2, 8] should return 128 as the largest product can be made by multiplying -4 * -4 * 8 = 128.

def maximum_product_of_three(lst):

    max_pr = 0
    num_num = 0

    for num in lst:
        for i in range(0,len(lst)):
            if i != lst[num_num] and num_num+2 < len(lst):
                try:
                    if (num*lst[i+1]*lst[i+2])>max_pr:
                        max_num = num*lst[i+1]*lst[i+2]
                except:
                    pass
        num_num =+ 1

    return max_num
using System; 
public class Sum
{
   public int GetSum(int a, int b)
   {
      return a + b;
   }
}
Bash
Regular Expressions
Declarative Programming
Advanced Language Features
Programming Paradigms
Fundamentals
Strings

You have two lists you need to know what is in list two, that is not in list one:

missing file1.list file2.list

Two Lists

MacBook-Air:codewar dusty$ cat one
one
two
three
four
six
MacBook-Air:codewar dusty$ cat two
one
two
three
four
five

What is in list two, but not in list one:

MacBook-Air:codewar dusty$ missing one two
five
function missing() {     cat $1 <(cat $1 $2 ) <(cat $2 $1 | sort | uniq -c | tr -d ' ' | grep '^1' | sed 's/1//') | sort |  uniq -c | tr -d ' ' | grep '^2' | sed 's/2//'; }
USING: math ;
IN: multiplier

: multiply ( a b -- a*b ) * ;

In JavaScript the most popular way of inverting strings would be something, similar to [...str].reverse().join('').

And what would be your unusual way?

const invert = str => str.replace(/./g, (_,o,s) => s.charAt(s.length-1-o));
starlightFailed Tests

Test

print(2+4)

When asked about closest to N integer divisible by n, you might think of a neat and simple n*Math.round(N/n).

But, is there anything even more concise and fast?

closestMultiple=(N,n)=>n*(N/n+0.5>>0)

Your task is to write a function sortWithoutSort() that accepts an array of arbitrary length n with integer values v as an argument. sortWithoutSort() should return the sorted array in ascending order. You should not use Array.sort() to achieve this.

function sortWithoutSort(array) {
  for (let i = 0; i < array.length; i++) {
    if (i < array.length - 1) {
      if (array[i] > array[i+1]) {
        var sortVal = array.splice(i,1)[0];
        array.splice(i+1, 0, sortVal);
        sortWithoutSort(array);
      }
    }  
  }  
  
  return array;
}