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

There is a tree.
We define "cut the branch" as deleting one of its non-leaf nodes and making its sons its equal.
A root can also be deleted to form 2 seperate trees.
An example:
1->2, 2->3, 2->4
after deleting 2:
1->3, 1->4
after deleting 1:
3, 4(two seperate trees)

Your job is to decide the maximal amouts of trees you get when there are not any branches left for you to cut.

The input will be like this:
vector1 1 2 2
| | |
/ / /
vector2 2 3 4

#include <vector>
using namespace std;

int CutTheBranch(vector<int> fir, vector<int> sec){
  int thing[20005];
  for(int i=0;i<20005;i++)thing[i]=0;
  int k=sec.size();
  for(int i=0;i<k;i++)thing[fir[i]]++;
  int cnt=0;
  for(int i=0;i<20005;i++)if(thing[i]!=0)cnt++;
  return cnt;
}

Get Max of two integers

section .text
global max ; declare max function
max:            ; int max(int a, int b)
  cmp rdi, rsi ; compare a ?= b
  jle _lower  ; jump to lower if a <= b
  jmp _greater ; jump to greater if a > b
  _lower:
    mov rax, rsi ; assgine rax to b
    jmp _end ; end program
  _greater:
    mov rax, rdi ; assgine rax to a
  _end:
  ret

Macro to get max of two integers
can be used as a new pseudo-command

; A macro with two parameters
; Implements actual int max(int a, int b)
%macro _max 2 
  cmp %2, %1 ; compare a ?= b
  jle _lower ; jump to lower if a <= b
  jmp _greater ; jump to greater if a > b
  
  _lower:
    mov rax, %1 ; assgine rax to b
    jmp _end ; end program
  _greater:
    mov rax, %2 ; assgine rax to a
  _end:
  ret
  
%endmacro

section .text
global max ; declaring int max(int a, int b)

max:            
  _max rdi, rsi ; using inner _max macro 
  ret

Iterating over two vectors at the same time till end of one of them

#include <vector>
#include <tuple>
#include <iostream>
template <class T, class U>
void iterateTwoVectors(std::vector<T> A, std::vector<U> B)
{

   for (auto [a,b] = std::tuple{A.begin(), B.begin()}; a != A.end() && b != B.end(); a++, b++) 
   { 
    std::cout << *a << ":" << *b << "\n";
   }
}

enumerating over vector, equivelent to python's enumerate(list)

#include <vector>
#include <tuple>
#include <iostream>

template <class T>
void enumerate(std::vector<T> A)
{
    for(auto [a,i]=std::tuple{A.begin(), 0}; a < A.end(); a++, i++)
   { 
    std::cout << i << ":" << *a << "\n";
   }
}
Lists
Data Structures

Break lists of lists into a simple list

my_list = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
while (all(isinstance(x, list) for x in my_list)):
    new_list = []
    for sublist in my_list:
        for i in sublist:
            new_list.append(i)
    my_list = new_list
print(my_list)

Endo wraps a function; appEndo unwraps it.

stimes composes a wrapped function with itself n times. It is optimised to take O(log n) time.

module Example where

import Data.Semigroup (Endo(..),stimes)

fib :: Integer -> Integer
fib n = fst $ n `stimes` Endo ( \ (a,b) -> (b,a+b) ) `appEndo` (0,1)
Control Flow
Basic Language Features
Fundamentals

Nigelius is a new Hero in Mobile Legends, and you are invited for an interview by Shanghai Moonton
Technology for a game developer position. Nigelius the Bloodborn has a passive skill that increases
his damage for every 250 HP he has, and your task is to code this skill. The increase goes as
follows:

at  250 HP    +1 damage
at  500 HP    +4 damage
at  750 HP    +9 damage
at 1000 HP    +16 damage
at 1250 HP    +25 damage
.
.
.
at 2000 HP    +64 damage
at 2250 HP    +81 damage
...etc

That is, we determine how many chunks of 250 HP Nigelius has, and then we square it to get the bonus
damage. Write a function getBonusDamage given the current health of Nigelius.

Nigelius does not get fractional damage bonuses, in other words, 500 and 600 HP will give him +4
bonus damage. 749 HP will also give him +4 bonus damage.

function getBonusDamage(currentHP) {
  return Math.trunc(currentHP / 250) ** 2
}
Algorithms
Logic
Arrays
Data Types
Mathematics
Numbers

I had to use this for the kyu 3 binomial expansion kata since I have no clue how to use the BigInts library for Java. Instead of calculating the total value of each factorial, this finds the factors of the numerator and denominator, simplifies, and divides the results. Nothing special and there are better solutions, but I'm proud of it.

Note: The n-choose-k formula

        n!
  ------------
   k!(n - k)!
public class ArrayFactorial{
  public static int factorial(int n, int k) {
          int n_k = n - k;
          int[] numerator = new int[n];
          int[] denominator = new int[k + n_k];
          for(int i = 1; i <= n; i++)
              numerator[i - 1] = i;
          for(int i = 1; i <= k; i++)
              denominator[i - 1] = i;
          for(int i = 1; i <= n_k; i++)
              denominator[k + i - 1] = i;
          for(int i = 0; i < numerator.length; i++){
              for(int j = 0; j < denominator.length; j++){
                  if(numerator[i] == denominator[j]) {
                      numerator[i] = 1;
                      denominator[j] = 1;
                  }
              }
          }
          int n_sum = 1;
          int d_sum = 1;
          for(int v : numerator)
              n_sum *= v;
          for(int v : denominator)
              d_sum *= v;
          return n_sum / d_sum;
      }
  }

Hello! I have a lot of difficulties to write a good description of this kata. I published it some time ago but I met a lot of critics with that description and received good evaluations of the task. I tried to rewrite it several times but it would not get better. If you like thaе kata and you are interested in helping me it would be great!

image for description

What we have:

  • Input: a table pool (two-dimensional array);

  • "X" is a part of the table or cell;

  • One white cue ball is represented by number >=1. This number means the force of shot: one point of the force is equal to one movement of a ball, so the ball with number 3 can move 3 cells (to move to the next cell you should subtract one point from its force). After collision with a coloured ball it gives the rest points of force to a coloured ball and disappears (the white ball only determines the direction).

  • One coloured ball is represented by "O".

  • Pockets are represented by "U". They can be in any place on the borders of the table;

  • The output is: a boolean (whether the coloured ball rolled into a pocket) and a pair of integers (the final coordinates of the coloured ball).

Rules

The balls can move only to their adjacent cells in the horizontal, vertical ou diagonal direction. The line

You should shot the white ball in the direction of the coloured ball using that way that results in the imaginable straight line between them:

Possible collisions:

1) ["O", "X", "X"]  2) ["X", "X", "X"]  3) ["X", "4", "X"]  
   ["X", "X", "X"]     ["O", "X", "4"]     ["X", "X", "X"]  
   ["X", "X", "4"]     ["X", "X", "X"]     ["X", "O", "X"]  

No collisions:

1) ["X", "4", "X"]  2) ["X", "X", "X", "X"]
   ["X", "X", "X"]     ["X", "X", "X", "O"]
   ["O", "X", "X"]     ["4", "X", "X", "X"]

If the white ball can't reach the coloured ball by that way, a collision won't happen.

In the case of collision, the coloured ball "O" starts to move(in the same direction that the white ball moved) until it stops because its force is over or until it enters a pocket. If it hits the "table wall", the ball changes its angle of movement according to physics.

For example, in this case, after collision the ball "O" goes to the opposite direction:

["X", "O", "X"]  ["X", "O", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]
["X", "4", "X"]=>["X", "X", "X"]=>["X", "O", "X"]=>["X", "X", "X"]=>["X", "O", "X"]
["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "O", "X"]  ["X", "X", "X"]

And in the next case, it "reflects":

 ["X", "O", "X"]  ["X", "O", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]
 ["4", "X", "X"]=>["X", "X", "X"]=>["X", "X", "O"]=>["X", "X", "X"]=>["O", "X", "X"]
 ["X", "X", "X"]  ["X", "X", "X"]  ["X", "X", "X"]  ["X", "O", "X"]  ["X", "X", "X"]  

Return true or false if the coloured ball did enter a pocket or not and the coordinates of coloured ball.

Notes

The size of a table can be different but always rectangular.

There are only two balls.

Examples

let table = [["U", "X", "O", "X"], // the collision is possible
             ["X", "X", "X", "X"],
             ["9", "X", "U", "X"]]

 table = [["U", "X", "O", "X"], 
          ["X", "8", "X", "X"], // <-- one movement spends one point of force
          ["X", "X", "U", "X"]]

 table = [["U", "X", "O", "X"], // <-- 7 points of force (the white ball was removed)
          ["X", "X", "X", "X"],
          ["X", "X", "U", "X"]]

 table = [["U", "X", "X", "X"], 
          ["X", "X", "X", "O"],// <--6 points
          ["X", "X", "U", "X"]]
          
 table = [["U", "X", "X", "X"], 
          ["X", "X", "X", "X"],
          ["X", "X", "U", "X"]] // <--the coloured ball entered a pocket

 return [true, [2, 2] ]  
let table = [["U", "X", "X"], // the collision is possible,
             ["1", "O", "U"], // but there is not enough force to reach a pocket
             ["X", "X", "X"]]
return [false, [1, 1] ]
let table =  [["U", "X", "X", "X"], // the collision is not possible
             ["X", "X", "X", "O"],
             ["9", "X", "U", "X"]]
return [false, [1, 3] ]

!The last critic of a Moderator:

  1. would be more readable to have the input as an array of strings, rather than an array of arays of strings, no? (and you could use whitespaces instead of X for "empty" places)

  2. the description really needs a rewrite... ;) General advises:
    a) don't give the examples before the specifications
    b) especially, don't mix explanations and examples
    c) give the general idea of the task right at the beginning before going into details. Like, that's the very first thing that should be in the description, before the picture.

  3. I believe you didn't specify that no pockets will ever be under the starting positions of the balls

  4. the notes are parts of the specs

  5. did you say that the force/speed may be greater than 9 (and is never negative)?

*3 - I'm going t fix it

function poolgame(table) {
         let force;
         let coordBall1 = {};
         let coordBall2 = {};

         for (let i = 0; i < table.length; i++) {
            for (let k = 0; k < table[i].length; k++) {
               if (Object.keys(coordBall1).length && Object.keys(coordBall2).length) break;
               if (!isNaN(table[i][k])) {
                  coordBall1.X = k;
                  coordBall1.Y = i;
                  force = table[i][k];
               } else if (table[i][k] == "O") {
                  coordBall2.X = k;
                  coordBall2.Y = i;
               }
            }
         }

         let dir = { X: 0, Y: 0 };
         let y = Math.abs(coordBall1.Y - coordBall2.Y);
         let x = Math.abs(coordBall1.X - coordBall2.X);

         if (coordBall1.Y === coordBall2.Y) {
            force -= x;
         } else if (coordBall1.X === coordBall2.X) {
            force -= y;
         } else {
            if (y !== x) return [false, [coordBall2.Y, coordBall2.X]]; //check if there is no collision
            force -= x;
         }
         if (force <= 0) return [false, [coordBall2.Y, coordBall2.X]]; //check if there is no collision 

         dir.X = (coordBall1.X > coordBall2.X) ? -1 : (coordBall1.X === coordBall2.X) ? 0 : 1;
         dir.Y = (coordBall1.Y > coordBall2.Y) ? -1 : (coordBall1.Y === coordBall2.Y) ? 0 : 1;

         let tableLimit = {
            X: table[0].length - 1,
            Y: table.length - 1
         }

         while (force--) {
            let possibleX = coordBall2.X + dir.X;
            let possibleY = coordBall2.Y + dir.Y;
            if (possibleX > tableLimit.X || possibleX < 0) dir.X = -dir.X;
            if (possibleY > tableLimit.Y || possibleY < 0) dir.Y = -dir.Y;
            coordBall2.X += dir.X;
            coordBall2.Y += dir.Y;
            if (table[coordBall2.Y][coordBall2.X] === "U") return [true, [coordBall2.Y, coordBall2.X]];
         }
         return [false, [coordBall2.Y, coordBall2.X]];
      }