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.

Code
Diff
  • const main = () =>  {
      let = ['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'];
      return [...new Set(links)].length;
    };
    
    // Functions should be passed arguments, not have the data already inside them.
    // Test case:
    // Test.assertEquals(['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'], 3);
    
  • 1-const main = () => {
    2-
    3- let links = [
    4- 'https://www.uol',
    5- 'https://www.bol.com.br',
    6- 'https://www.uol',
    7- 'https://noticias.uol.com.br',
    8- 'https://www.bol.com.br',
    9- ];
    10-
    11- // converter para ES6
    12- var uniqueLinks = links.reduce(function (carry, item) {
    13- if (carry.indexOf(item) === -1) {
    14- carry.push(item);
    15- }
    16- return carry;
    17- }, []);
    18-
    19-
    20- return uniqueLinks.length;
    1+const main = () => {
    2+ let = ['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'];
    3+ return [...new Set(links)].length;
    2121 };
    5+
    6+// Functions should be passed arguments, not have the data already inside them.
    7+// Test case:
    8+// Test.assertEquals(['https://www.uol','https://www.bol.com.br','https://www.uol','https://noticias.uol.com.br','https://www.bol.com.br'], 3);

With parameter int 011, the tests fail.

Code
Diff
  • const alphabetPosition = s => s.toLowerCase().replace(/[a-z]|(.)/g, (m, c) => c ? '' : ` ${m.charCodeAt(0) - 96}`).trim();
  • 1-function alphabetPosition(text) {
    2- let obj = {
    3- "a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7,
    4- "h": 8, "i": 9, "j": 10, "k": 11, "l":12, "m": 13, "n": 14, "o": 15, "p": 16, "q": 17, "r": 18, "s": 19, "t": 20,
    5- "u": 21, "v": 22, "w": 23, "x": 24, "y": 25, "z": 26
    6- }
    7- let arr = [];
    8- let str = '';
    9-
    10- text = text.replace(/[^a-zA-Z]+/g, '');
    11- for (let key in obj) {
    12- for (let i = 0; i < text.length; i++) {
    13- if(text[i].toLowerCase() == key.toLowerCase() && text[i] !== '') {
    14- arr[i] = obj[key];
    15- }
    16- }
    17- }
    18-
    19- str = arr.join(' ');
    20- return str;
    21-}
    1+const alphabetPosition = s => s.toLowerCase().replace(/[a-z]|(.)/g, (m, c) => c ? '' : ` ${m.charCodeAt(0) - 96}`).trim();

Your code is good and possibly faster but I think this is a lot shorter and somewhat easier to understand.

Code
Diff
  • function descendingOrder(n){
      return Number(String(n).split('').sort(function(a, b){return b-a}).join(''));
    }
  • 11 function descendingOrder(n){
    2- let x= n.toString();
    3- let arr = x.split('');
    4- let N = arr.length;
    5-
    6- if (n.length === 1) {
    7- return parseInt(n, 10);
    8- }
    9-
    10- for (let i = 0; i < N; i++) {
    11-
    12- for (let j = 0; j < N-i-1; j++) {
    13- if (arr[j] < arr[j+1]) {
    14- let k = arr[j];
    15- arr[j] = arr[j+1];
    16- arr[j+1] = k;
    17- }
    18- }
    19- }
    20-
    21- n = arr.join('');
    22- n = parseInt(n, 10);
    23- return n;
    2+ return Number(String(n).split('').sort(function(a, b){return b-a}).join(''));
    2424 }
Numbers
Integers
Algorithms

match point

Code
Diff
  • // Removed loop
    fn digits (n: u64) -> usize
    {
      match n {
        0 ..= 9 => 1,
        10 ..= 99 => 2,
        100 ..= 999 => 3,
        1000 ..= 9999 => 4,
        10000 ..= 99999 => 5,
        100000 ..= 999999 => 6,
        1000000 ..= 9999999 => 7,
        10000000 ..= 99999999 => 8,
        100000000 ..= 999999999 => 9,
        1000000000 ..= 9999999999 => 10,
        10000000000 ..= 99999999999 => 11,
        100000000000 ..= 999999999999 => 12,
        1000000000000 ..= 9999999999999 => 13,
        10000000000000 ..= 99999999999999 => 14,
        100000000000000 ..= 999999999999999 => 15,
        1000000000000000 ..= 9999999999999999 => 16,
        10000000000000000 ..= 99999999999999999 => 17,
        100000000000000000 ..= 999999999999999999 => 18,
        1000000000000000000 ..= 9999999999999999999 => 19,
        _ => 20,
      }
    }
    
  • 1-// Unrolled div loop
    2-fn digits(mut n: u64) -> usize {
    3- let mut l = 1;
    4- loop {
    5- if n < 10 {
    6- return l;
    7- }
    8- if n < 100 {
    9- return l + 1;
    10- }
    11- if n < 1000 {
    12- return l + 2;
    13- }
    14- if n < 10000 {
    15- return l + 3;
    16- }
    17- n /= 10000;
    18- l += 4;
    1+// Removed loop
    2+fn digits (n: u64) -> usize
    3+{
    4+ match n {
    5+ 0 ..= 9 => 1,
    6+ 10 ..= 99 => 2,
    7+ 100 ..= 999 => 3,
    8+ 1000 ..= 9999 => 4,
    9+ 10000 ..= 99999 => 5,
    10+ 100000 ..= 999999 => 6,
    11+ 1000000 ..= 9999999 => 7,
    12+ 10000000 ..= 99999999 => 8,
    13+ 100000000 ..= 999999999 => 9,
    14+ 1000000000 ..= 9999999999 => 10,
    15+ 10000000000 ..= 99999999999 => 11,
    16+ 100000000000 ..= 999999999999 => 12,
    17+ 1000000000000 ..= 9999999999999 => 13,
    18+ 10000000000000 ..= 99999999999999 => 14,
    19+ 100000000000000 ..= 999999999999999 => 15,
    20+ 1000000000000000 ..= 9999999999999999 => 16,
    21+ 10000000000000000 ..= 99999999999999999 => 17,
    22+ 100000000000000000 ..= 999999999999999999 => 18,
    23+ 1000000000000000000 ..= 9999999999999999999 => 19,
    24+ _ => 20,
    1919 }
    2020 }
Code
Diff
  • char* Hi (void)
    {
      char* ans;
      asprintf(&ans, "Hello World.");
      return ans;
    }
    
  • 11 char* Hi (void)
    22 {
    3- char* ans=malloc(13);
    4- strcpy(ans,"Hello World.");
    3+ char* ans;
    4+ asprintf(&ans, "Hello World.");
    55 return ans;
    66 }
def ShellSort(ary):
    length = len(ary)
    gap = round(length)
    step = length//2
    while step:
        for i in range(step,length,step):
            tmp = ary[i]
            for j in range(i,0,-step):
                if ary[j-step] > tmp:
                    ary[j] = ary[j-step]
                else:
                    break
            ary[j] = tmp
        if step == 1:
            break
        step = step//2
      
        
ary = [1,23,4,53,55,34,3,99,14]
ShellSort(ary)
print(ary)

inputOrder: [5,4,1]

#include <stdio.h>

struct _Node{
  int value;
  struct _Node *left;
  struct _Node *right;
  int height;
};

typedef struct _Node Node;

int max( int a, int b )
{
    return(a>b?a:b);
}

int Height( Node* tree )
{
    if( NULL == tree ) {
        return -1;
    }
    
    return tree->height;
}

int InitTree( Node* tree, int value )
{
  if( tree == NULL ) {
      return -1;
  }
  tree->value = value;
  tree->left = NULL;
  tree->right = NULL;
  tree->height = 0;
  return 0;
}

Node* SingleRotateWithLeft( Node* k1 )
{
    Node* k2;
    
    k2 = k1->left;
    k1->left = k2->right;
    k2->right = k1;
    
    k1->height = max(Height(k1->left), Height(k1->right)) + 1;
    k2->height = max( Height(k2->left), Height(k1) ) + 1;
    return k2;
}

Node* InsertTree(Node *tree, Node *new_node )
{
  if( new_node->value > tree->value ) {
    if( tree->right == NULL ) {
        tree->right = new_node;
    } else {
        InsertTree( tree->right, new_node );
    } 
  } else {
    if( tree->left == NULL ) {
        tree->left = new_node;
    } else {
        InsertTree( tree->left, new_node );
        if( Height(tree->left) - Height(tree->right) == 2 ) {
            if( new_node->value < tree->left->value ) {
                tree = SingleRotateWithLeft(tree);
            }
        }
    }
  }
  tree->height = max(Height(tree->left),Height(tree->right)) + 1;
  return tree;
}

void OrderTraverse( Node* tree )
{
  if( tree != NULL ) {
      printf("value:%d height:%d\t\n", tree->value,tree->height);
      OrderTraverse( tree->left );
      OrderTraverse( tree->right );
  }
}

int main()
{
  Node *tree = NULL;
  tree = (Node*)malloc(sizeof(Node));
  InitTree(tree, 5);
  int i = 0;
  int array[10] = {4,1};
  
  for( i = 0; i < 2; i++ ) {
      Node *node = NULL;
      node = (Node*)malloc(sizeof(Node));
      if( NULL == node ) {
        continue;
      }
      InitTree(node,array[i]);
      tree = InsertTree(tree,node);
  }
  printf("preOrderTraverse:\n");
  OrderTraverse(tree);
  printf("succeed!\n");
  return 0;
}