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.

Basic Language Features
Fundamentals
Control Flow
Code
Diff
  • 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
  • 11
    def convert_decimal_roman(numero):
    
    22
        num_romanos_dic = {
    
    33
            1: 'I',
    
    44
            5: 'V',
    
    55
            10: 'X',
    
    66
            50: 'L',
    
    77
            100: 'C',
    
    88
            500: 'D',
    
    99
            1000: 'M'
    
    1010
        }
    
    1111
        list_num_rom = [{'key': x, 'value': y} for x, y in num_romanos_dic.items()]
    
    1212
        size_num = len(numero)
    
    1313
        output = []
    
    1414
        for i in range(size_num):
    
    1515
            num =  numero[i:]
    
    1616
            num_unit = int(num)
    
    1717
            numextre_izq = int(num[0])
    
    1818
            pref = []
    
    1919
            for i in range(1, len(list_num_rom), 2):
    
    2020
                if num_unit >= list_num_rom[i-1]['key'] and num_unit < list_num_rom[i+1]['key']:
    
    2121
                    pref.append(list_num_rom[i-1]['value'])
    
    2222
                    pref.append(list_num_rom[i]['value'])
    
    2323
                    pref.append(list_num_rom[i+1]['value'])
    
    2424
    2525
            if numextre_izq < 4 and numextre_izq > 0:
    
    2626
                output.append(pref[0]*numextre_izq)
    
    2727
            if numextre_izq == 4:
    
    2828
                output.extend(pref[0:2])
    
    2929
            if numextre_izq == 5:
    
    3030
                output.append(pref[1])
    
    3131
            if numextre_izq > 5 and numextre_izq < 9:
    
    3232
                output.append(pref[1] + pref[0]*(numextre_izq-5))
    
    3333
            if numextre_izq == 9:
    
    3434
                output.extend(pref[::2])
    
    3535
    3636
        output = ''.join(output)
    
    3737
        return output
    

Recent Moves:

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;
}

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
Memory

In this kata you have to determine which memory segment a pointer belongs to.
The memory layout of a C program is divided in the following segments:

  • global and static variables are stored in the .data segment;
    • if they are constant they are stored in the .rodata (read-only data) segment instead;
    • else if they are uninitialized or initialized to 0, they are stored in the .bss (block starting symbol) segment instead;
  • local variables are stored onto the stack;
  • malloc() &co allocate memory on the heap
  • the program's machine code is stored in the .text segment;

The NULL pointer does not belong to any of these segments.

The kata contains an enum corresponding to these different memory types. Determine which kind of pointer was passed to your function and return the correct enum member.

#include <stdlib.h>
#include <stddef.h>

enum memory_type {DATA, RODATA, BSS, STACK, HEAP, NIL, TEXT};

ptrdiff_t array_min (size_t length, ptrdiff_t array[])
{
	if (length == 0)
		return 0;

	ptrdiff_t min = array[0];

	for (size_t i = 1; i < length; i++)
		min = (array[i] < min) ? array[i] : min;

	return min;
}

enum memory_type guess_memory_type (const void *ptr)
{
	if (ptr == NULL)
		return NIL;

	static char *string_literal = "some string literal";
  void *rodata_ptr = string_literal;

	static char static_var = '@';
	void *data_ptr = &static_var;

	static char uninitialized_static_var;
	void *bss_ptr = &uninitialized_static_var;

	char local_var;
	void *stack_ptr = &local_var;

	void *heap_ptr = malloc(1);

  void *text_ptr = guess_memory_type;

  // it's UB to substract pointers unless they point to the same array, but whatever
	ptrdiff_t  stack_diff = llabs((char *)ptr - (char *)stack_ptr);
	ptrdiff_t   heap_diff = llabs((char *)ptr - (char *)heap_ptr);
	ptrdiff_t    bss_diff = llabs((char *)ptr - (char *)bss_ptr);
	ptrdiff_t rodata_diff = llabs((char *)ptr - (char *)rodata_ptr);
	ptrdiff_t   data_diff = llabs((char *)ptr - (char *)data_ptr);
	ptrdiff_t   text_diff = llabs((char *)ptr - (char *)text_ptr);

	ptrdiff_t diffs_array[] = { stack_diff, heap_diff, bss_diff, rodata_diff, data_diff, text_diff};
	ptrdiff_t smallest = array_min(sizeof(diffs_array) / sizeof(diffs_array[0]), diffs_array);

	free(heap_ptr);
// that wont work if pointers are close to the segment boundaries
	if (smallest == stack_diff)  return STACK;
	if (smallest == heap_diff)   return HEAP;
	if (smallest == bss_diff)    return BSS;
	if (smallest == rodata_diff) return RODATA;
	if (smallest == data_diff)   return DATA;
	if (smallest == text_diff)   return TEXT;

	return -1;
}

Input data now come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases. Modified the original test case and added a new one.

Code
Diff
  • /*input data come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases*/
    const yearlyElectricCosts = (m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0, m8 = 0, m9 = 0, m10 = 0, m11 = 0, m12 = 0 ) => {
      /* Create an object with 12 keys and the input values*/
      const monthlyCosts = {
       januaryBill : m1,
       februaryBill : m2,
       marchBill : m3,
       aprilBill : m4,
       mayBill : m5,
       juneBill : m6,
       julyBill : m7,
       augustBill : m8,
       septemberBill : m9,
       octoberBill : m10,
       novemberBill : m11,
       decemberBill : m12
      }
      /*print the object*/
      console.log(monthlyCosts);
      // Decide on a variable name and sum all the monthly charges
      const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0);
      
      // Return the value of the variable by typing it in after the `return` keyword below
      console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2)));
      /*toFixed converts the result to string, we have to convert it back to number*/
      return parseFloat(totalCharges.toFixed(2));
    }
  • 1
    const yearlyElectricCosts = () => {
    
    2
      // Create an object with 12 keys with the correct values
    
    1+
    /*input data come from parameters with zero default value, so it can calculate for any data and can be tested with different test cases*/
    
    2+
    const yearlyElectricCosts = (m1 = 0, m2 = 0, m3 = 0, m4 = 0, m5 = 0, m6 = 0, m7 = 0, m8 = 0, m9 = 0, m10 = 0, m11 = 0, m12 = 0 ) => {
    
    3+
      /* Create an object with 12 keys and the input values*/
    
    33
      const monthlyCosts = {
    
    4
       januaryBill : 43.11,
    
    5
       februaryBill : 50.21,
    
    6
       marchBill : 48.92,
    
    7
       aprilBill : 62.36,
    
    8
       mayBill : 54.64,
    
    9
       juneBill : 49.30,
    
    10
       julyBill : 61.93,
    
    11
       augustBill : 68.54,
    
    12
       septemberBill : 71.77,
    
    13
       octoberBill : 70.28,
    
    14
       novemberBill : 59.56,
    
    15
       decemberBill : 62.04
    
    5+
       januaryBill : m1,
    
    6+
       februaryBill : m2,
    
    7+
       marchBill : m3,
    
    8+
       aprilBill : m4,
    
    9+
       mayBill : m5,
    
    10+
       juneBill : m6,
    
    11+
       julyBill : m7,
    
    12+
       augustBill : m8,
    
    13+
       septemberBill : m9,
    
    14+
       octoberBill : m10,
    
    15+
       novemberBill : m11,
    
    16+
       decemberBill : m12
    
    1616
      }
    
    17
      // Decide on a variable name and add all the monthly charges
    
    18+
      /*print the object*/
    
    19+
      console.log(monthlyCosts);
    
    20+
      // Decide on a variable name and sum all the monthly charges
    
    1818
      const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr, 0);
    
    1919
      
    
    2020
      // Return the value of the variable by typing it in after the `return` keyword below
    
    2121
      console.log(totalCharges.toFixed(2), typeof(totalCharges.toFixed(2)));
    
    2222
      /*toFixed converts the result to string, we have to convert it back to number*/
    
    2323
      return parseFloat(totalCharges.toFixed(2));
    
    2424
    }
    

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;
	}
  }

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]
Fundamentals
Games
Code
Diff
  • def riddle _;/#{?\\+??}/=~_ end
  • 1
    def riddle(word):
    
    2
        return word.rindex("?")
    
    1+
    def riddle _;/#{?\\+??}/=~_ end
    

Recent Moves: