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
import re
def remove_numbers(string):
    return re.sub(r"[0-9]","",string)
const input = ["a", 1, 2, false, "b"];
const output = input.reduce((acc, el) => {
  const type = typeof el;
  acc[type] = acc[type] || [];
  acc[type].push(el);
  return acc;
}, {});
console.log(output);

Your job is to group the words in anagrams.

What is an anagram ?

star and tsar are anagram of each other because you can rearrange the letters for star to obtain tsar.

Example

A typical test could be :

// input
groupAnagrams(["tsar", "rat", "tar", "star", "tars", "cheese"]);

// output
[
  ["tsar", "star", "tars"],
  ["rat", "tar"],
  ["cheese"]
]
## Helpers

The method `assertSimilarUnsorted` has been preloaded for you **in the Solution Sandbox only** to compare to arrays without relying on the sorting of the elements.

`assertSimilarUnsorted([[1,2], [3]], [[3], [1,2]]); // returns true`

Hvae unf !

I'd advise you to find an efficient way for grouping the words in anagrams otherwise you'll probably won't pass the heavy superhero test cases

def group_anagrams(words):
    def sort_word(word):
        charlist = list(word)
        charlist.sort()
        return "".join(charlist)
    
    anagrams = {}
    for word in words:
        sorted = sort_word(word)
        if sorted in anagrams:
            anagrams[sorted].append(word)
        else:
            anagrams[sorted] = [word]
            
    return list(anagrams.values())

In Node v12, if you overwrite Math.pow with a function that throws an error, it will automatically triggered despite there is no code in the Code, Preloaded, and Test Cases section that calls them. Maybe used by the test framework?

This behavior does not appear in Node v8 or v10.

Note

Not tested locally (only tested it on Codewars).

// Switch to Node v12 to see the error
console.log("Hello, World!")

The evaluation order may be different from the left-to-right order if the pipe operator is present.

The generated code for this example is similar to the following code:

Caml_obj.caml_equal(Caml_array.caml_array_get(a.sort(), 0), Caml_array.caml_array_get($$Array.copy(a), 0));
let f = (a: array(int)): int => a[0]

Royal Morse Code
The Royal British Kingdom has enforced a new decree in the society that all princes and princesses must use a new
form of Morse code for all communications.
We know that Morse Code uses dots (.) and dashes (;) to communicate meaning to the receiver. The following rules
are stated according to this decree:

Any message must begin with a dot (.)
Any message must end with a dash (-)
Every dot (.) must have a corresponding dash (-) after it to close it

If a message follows all the mentioned rules, then it is considered compliant with the Royal Decree and the messa
allowed to be sent. Otherwise, it is considered as defaulted.
The Great Prince of Britain has sent out a set of N messages of the new Morse code. You have to figure out the
number of messages which are compliant and ultimately sent forward.
Input Specification:
input1: N, the number of messages.
input2: Sequence of strings, each representing a new message in the set.
Output Specification:
The number of messages compliant to the new decree
Example 1:
input1: 1
input2: ("..---.-")

Output: 0
Explanation:
The given string starts with a dot(.) and ends with a dash (-), but it fails the third criteria as there are 5 dots but only 4
dashes. Thus, O messages are compliant.
Example 2:
input1: 2
input2: (".-.-.-.-","...---.-")
Output: 2
Explanation:
Both the given strings follow all 3 rules. Thus, both 2 messages are compliant.

import java.util.*;
class MorseCodeChecker{
public int decreeCompliant(int input1, String[] input2){
  int result = 0;
		
		for(int i = 0 ; i < input2.length; i++){
			Deque<Character> stack = new ArrayDeque<>();
			if(input2[i] == null || input2[i].length() == 0){
				continue;
			}
			String cur = input2[i];
			
      if(cur.charAt(0) != '.' && cur.charAt(cur.length()-1) != '-'){
					break;
				}
         int j = 0;
			while(j < cur.length()){
				
				if((cur.charAt(j) == '.')){

					while(j < cur.length() && cur.charAt(j) == '.'){
						j++;
						stack.push('.');
					}
				}
				else{
					if(stack.size() == 0){
						break;
					}
					stack.pop();
					j++;
				}
			}
			if(j == cur.length() && stack.size() == 0){
				result++;
			}
		}
		return result;
	}
  }
Basic Language Features
Fundamentals
Control Flow

Roman numerals can be generated with the following letters I, V, X, L, C, D, M, but the generated numbers must be entered in decimal. Let's see examples:

  convert_decimal_roman('46') # 'XLVI'
  convert_decimal_roman('999') # 'CMXCIX'
  convert_decimal_roman('504') # 'DIV'

Requirements:

  • The number per argument must be in string type
  • No validation required for floating numbers
  • Only numbers up to 999 can be entered
def convert_decimal_roman(numero):
    num_romanos_dic = {
        1: 'I',
        5: 'V',
        10: 'X',
        50: 'L',
        100: 'C',
        500: 'D',
        1000: 'M'
    }
    list_num_rom = [{'key': x, 'value': y} for x, y in num_romanos_dic.items()]
    size_num = len(numero)
    output = []
    for i in range(size_num):
        num =  numero[i:]
        num_unit = int(num)
        numextre_izq = int(num[0])
        pref = []
        for i in range(1, len(list_num_rom), 2):
            if num_unit >= list_num_rom[i-1]['key'] and num_unit < list_num_rom[i+1]['key']:
                pref.append(list_num_rom[i-1]['value'])
                pref.append(list_num_rom[i]['value'])
                pref.append(list_num_rom[i+1]['value'])

        if numextre_izq < 4 and numextre_izq > 0:
            output.append(pref[0]*numextre_izq)
        if numextre_izq == 4:
            output.extend(pref[0:2])
        if numextre_izq == 5:
            output.append(pref[1])
        if numextre_izq > 5 and numextre_izq < 9:
            output.append(pref[1] + pref[0]*(numextre_izq-5))
        if numextre_izq == 9:
            output.extend(pref[::2])

    output = ''.join(output)
    return output

Programming challenge description:
The example sequence
011212201220200112 ... is constructed as
follows:

  1. The first element in the sequence is 0.
  2. For each iteration, repeat the following
    action: take a copy of the entire current
    sequence, replace O with 1, 1 with 2, and 2
    with 0, and place it at the end of the
    current sequence. E.g.
    0 ≥ 01 > 0112 > 01121220 > ...
    Create an algorithm which determines what
    number is at the Nth position in the
    sequence (using -based indexing).
    Input:
    Your program should read lines from
    standard input. Each line contains an
    integer N such that O <= N <=
    3000000000.

Output:
Print out the number which is at the Nth
position in the sequence.
Test 1
Test Input: 5
Test Output: 2

Test 2
Test Input: 101
Test Output: 1

Test 3
Test Input: 25684
Test Output: 0

function solution(num){
  if(num === 0) return 0
  let count = 1;
  while(num >1){
    count++;
    let tmp = Math.round(Math.log2(num))
    num -= Math.pow(tmp,2)
    
  }
  
  if(Math.round(count % 3) === 1){
    return 1
  }
  
  if(Math.round(count % 3) === 2){
    return 2
  }
  
  if(Math.round(count % 3) === 0){
    return 0
  }
}

// let test1 = findNth(0)
// console.log(test1)//0

// let test2 = findNth(5)
// console.log(test2)//2 

// let test3 = findNth(101)
// console.log(test3)//1 

// let test4 = findNth(25684)
// console.log(test4)//0

Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5…. The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on.
Find the number on the n-th position of the sequence.
Input: n = 3
Output: 2

Input: n = 55
Output:10

function solution(num){
  let times = 1;
  let count = 0;
  while(num > 0){
    count = num
    num -= times;
    times++;
  }
  return count;
}

In Ruby MRI 2.5.0, If you add assertion messaage with a '\n in it. The message after the newline character will be logged as a regular message instead of an error message.

puts "Uncomment the test cases to see the errors"