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

Make a Function that returns a comma separated list in the order of

"DirectoryName, Filename, FileExtension"

#!/bin/bash
filename=$1
echo "$(dirname $filename), $(basename ${filename%.*}), ${filename##*.}"

Find the number that only odd times form an array.

For example:

findOnlyOddTimesNumber([1,1,1,2,3,4,2,4,2,1,3]); // 2 
const findOnlyOddTimesNumber = arr => arr.reduce((p,c) => p^c);

findOnlyOddTimesNumber([1,1,1,2,3,4,2,4,2,1,3]);

This is how you prevent the user from importing certain modules in your kata.

import sys
sys.modules['module name'] = None

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

function nameScores(names) {
  var t1 = console.time("names");;
  names.sort();
  var sum = 0;
  var index = 1;
  for (var name = 0; name < names.length; name++) {
    var nameSum = 0;
    for (var character = 0; character < names[name].length; character++) {
      nameSum += (names[name][character].charCodeAt(0) - 64);
    }
    sum += (nameSum * index);
    index++;
  }
  var t2 = console.timeEnd('names');
  return sum;
}

I found a way to make random tests in python not random.

import random
random.seed(0)#Or any other number

This is a way to block the random.seed command which breaks your random tests

import random
random.seed = None

Sometimes we need a fast code to do this operation as fast as possible. Perhaps this one may help.

function primeFactorization(num){
    var root = Math.sqrt(num),  
    result = arguments[1] || [],x = 2; 
    if(num % x){
        x = 3;
        while((num % x) && ((x = x + 2) < root)){}
    }
    x = (x <= root) ? x : num;
    result.push(x);
    return (x === num) ? result : primeFactorization(num/x, result);
}

A similar kata in python to get the prime factors of a number.

from math import sqrt, floor
def fac(n):  
    step = lambda x: 1 + (x<<2) - ((x>>1)<<1)  
    maxq = long(floor(sqrt(n))) 
    d = 1
    q = n % 2 == 0 and 2 or 3 
    while q <= maxq and n % q != 0:
        q = step(d)
        d += 1
    return q <= maxq and [q] + fac(n//q) or [n]

With this one we close the trilogy Javascript-Python-Ruby to get the primes factor.

def prime_fac(i)
    factors = []
    check = proc do |p|
        while(q, r = i.divmod(p) 
        	   r.zero?)
            factors << p
            i = q
        end
    end
    check[2]
    check[3]
    p = 5
    while p * p <= i
        check[p]
        p += 2
        check[p]
        p += 4    # skip multiples of 2 and 3
    end
    factors << i if i > 1
    factors
end
def get_frames(seq)
  rev = seq.gsub(/[ATGC]/, 'A' => 'T', 'T' => 'A', 'G' => 'C', 'C' => 'G').reverse
  { "1":  seq, "2":  seq[1..-1], "3": seq[2..-1],
    "-1": rev, "-2": rev[1..-1], "-3": rev[2..-1] }
end

def find_orf_in_frame(seq,frame,min,len)
  orfs    = []
  tri_nts = seq.scan(/.{3}/)
  start   = stop = seq = ""
  seq     = seq.reverse.tr("ACTG","TGAC")

  is_neg  = frame < 0
  neg_offset = {"1" =>  1, "2" =>  3, "3" => 5}
  offset     = neg_offset[frame.abs().to_s]

  tri_nts.each_with_index do |tri_nt,i|
    if  (i == tri_nts.size - 1)
        stop  = ((i*3)+frame+2)
        start = (len - start.to_i - offset) if is_neg
        stop  = (len - stop.to_i  - offset) if is_neg
      orfs << [start.to_s,">#{stop}",seq] if (seq.length + 3) >= min
      break
    end
    if seq == "" && tri_nt == "ATG"
      # p "ATG"
      seq  += tri_nt
      start = (i * 3) + frame
    elsif (!$codons.select {|k,v| v == "*"}.keys.include?(tri_nt)) && seq != ""
      seq  += tri_nt
    elsif $codons.select {|k,v| v == "*"}.keys.include?(tri_nt)
      print "\n-----\n"
      print seq.scan(/.{3}/).map{|a| $codons[a]}.join()
      if (seq.length + 3) >= min
        stop  = ((i*3)+frame+2)
        start = (len - start.to_i - offset) if is_neg
        stop  = (len - stop.to_i  - offset) if is_neg
        orfs << [start.to_s,stop.to_s,seq]
      end
      start = stop = seq = ""
    end
  end
  orfs
end

def find_orfs(seq,min)
  frames = get_frames(seq)
  len    = seq.length
  all_orfs  = Hash.new()
  count = 1
  # [1,2,3,-1,-2,-3].each do |frame|
  [1].each do |frame|
    is_neg = frame < 0 ? true : false
    seq    = frames[frame.to_s.to_sym]
    orfs   = find_orf_in_frame(seq,frame,min,len)
    orfs.each do |orf|
      orf_info = {
        start:  orf[0],
        stop:   orf[1],
        strand: frame > 0 ? "+" : "-",
        seq:    orf[2].scan(/.{3}/).map{|a| $codons[a]}.join(),
        bp:     orf[2].length + 3,
        aa:     orf[2].length / 3,
        frame:  frame,
      }
      all_orfs["ORF#{count}"] = orf_info
      count += 1
    end
  end
  all_orfs
end