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.
example: cap(0, 10, 11) => 10
11 is greater than 10, therefore it caps to 10
example: cap(5, 10, 1) => 5
1 is less than 5, therefore it caps to 5
example: cap(0, 10, 5) => 5
5 is within 0 < x < 10, therefore it returns the number
def cap(low, high, num):
if num < low: return low
elif num > high: return high
else: return num
import codewars_test as test
from solution import cap
@test.describe("Basic")
def test_group():
@test.it("1")
def _():
test.assert_equals(cap(0, 5, 4), 4)
@test.it("2")
def _():
test.assert_equals(cap(3, 5, 2), 3)
@test.it("3")
def _():
test.assert_equals(cap(3, 5, 9), 5)
Take an n
long as argument and return a int
type representing the number of trailing zero's of n
bits
public class CtzKumite
{
public static int CountTrailingZero(long num)
{
if (num == 0) return 32;
uint res = 0;
num &= (uint)-(int)num;
var tmp = num >> 16; if (tmp != 0) { num = tmp; res += 16; }
tmp = num >> 8; if (tmp != 0) { num = tmp; res += 8; }
tmp = num >> 4; if (tmp != 0) { num = tmp; res += 4; }
return (int)(res + ((num >> 1) - (num >> 3)));
}
}
namespace Solution {
using NUnit.Framework;
using System;
[TestFixture]
public class CountTrailingZeroTests
{
[TestCase(0, ExpectedResult = 32)]
[TestCase(28_384, ExpectedResult = 5)]
[TestCase(2_648_292, ExpectedResult = 2)]
[TestCase(48_357_352, ExpectedResult = 3)]
[TestCase(48_357_352, ExpectedResult = 3)]
[TestCase(74_845_242_422_848_928, ExpectedResult = 5)]
public int Test(long n)
{
return CtzKumite.CountTrailingZero(n);
}
}
}
float AverageDisplacement(float u, float v, float t) { return ((u + v) / 2) * t; }
Describe(Average_Displacement)
{
It(Calculate_Average_Displacement)
{
Assert::That(AverageDisplacement(5, 8, 2), Equals(13));
}
};
Base64 is a url encoding scheme that many websites use in their urls as sometimes some characters are not allowed or can become malformed in a url. So Base64 turns any text into allowed ascii characters that will not become malformed in a url.
Your job is to create a program that takes in a String and returns the equivalent Base64 String without using any imports or built in functions.
The base64 encoding scheme goes as follows: You first take in your String, and convert every character into the 8bit binary reprentation of each ascii value ie (a is 97, 97 is 01100001). You you then make sure that the entirety of your string is a multiple of 6 (as Base64 works with 6 bits at a time) you then get the ascii value of each 6 bit chunk and you return that.
Sounds easy enough, right? However here are some things to remember, any characters you add to make sure that your binary String is a multiple are 6 are called, "Padding" in Base64 you represent the padding as, "=" and the end of your string.
some resouces to help you:
https://en.wikipedia.org/wiki/Base64 // more information on implementation
https://www.base64decode.org // a website that correctly implements base64 to check your work
Here are some examples:
"This is Base64" >> "VGhpcyBpcyBCYXNlNjQ="
"Test" >> "VGVzdAo="
"Purple" >> "UHVycGxl"
Good luck!!
public class Convert {
public static void main(String[] args) {
}
public static String toBinary(int n) {
String returnValue = "";
char[] encodingTable = "01".toCharArray();
while (n > 0) {
returnValue = encodingTable[n % 2] + returnValue;
n /= 2;
}
while (returnValue.length() < 8) {
returnValue = "0" + returnValue;
}
return returnValue;
}
public static int fromBinary(String n) {
int result = 0;
int pos = n.length()-1;
for(int i = 0; i < n.length(); i++) {
if (n.substring(i, i + 1).equals("1")) {
result += Math.pow(1 * 2,pos);
}
pos--;
}
return result;
}
public static String toBase64(String str) {
String strAsBinary = "";
String returnValue = "";
char[] encodingTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
int padding = 0;
for(int i = 0; i < str.length(); i++) {
strAsBinary += toBinary((int) (str.substring(i, i + 1).charAt(0)));
}
padding = strAsBinary.length() % 3;
while (strAsBinary.length() % 6 != 0) {strAsBinary = strAsBinary + "0";}
for(int i = 0; i < strAsBinary.length(); i += 6) {
returnValue += encodingTable[fromBinary(strAsBinary.substring(i, i + 6))];
}
for(int i = 0; i < padding; i++) {returnValue = returnValue + "=";}
return returnValue;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
assertEquals("VGhpcyBpcyBCYXNlNjQ=",Convert.toBase64("This is Base64"));
assertEquals("VGVzdA==", Convert.toBase64("Test"));
assertEquals("UHVycGxl", Convert.toBase64("Purple"));
}
}
class Mastermind{
static String evaluate(String secret, String guess) {
return "";
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
assertEquals(Mastermind.evaluate("[blue]", "[red]"), "[0,0]");
}
}
% Try enabling and disabling this import, compare the differences
:- use_module(library(clpfd)).
% explicitely include N. Always works
foo(Xs, N, Rs) :-
maplist({N}/[L,R] >> (R is L * N), Xs, Rs).
% do not include N, throws an error dependant on the import
bar(Xs, N, Rs) :-
maplist([L,R] >> (R is L * N), Xs, Rs).
% plunit can be used to test solution
:- begin_tests(example).
:- include(example).
test(test_foo) :-
Z = [1, 2, 3],
foo(Z, 4, Y),
assertion(Y == [4, 8, 12]).
test(test_bar) :-
Z = [1, 2, 3],
bar(Z, 4, Y),
assertion(Y == [4, 8, 12]).
:- end_tests(example).
Quipu
Quipu is an ancient recording device from Andean South America. It was likely used as an accounting record.
We chose to represent each knot as a number, and the positions as the position in an array, being the rightmost item the furthest knot in the rope.
It also had an error prevention method, being the first number the sum of the rest.
Return the number if the sum checks out, if not return -1.
import java.util.ArrayList;
public class Quipu {
public static int calculate(ArrayList<int[]> strings){
if (strings==null||strings.isEmpty())return -1;
int primero=0;
int suma=0;
for (int i = 0; i < strings.size(); i++) {
StringBuilder sb1 = new StringBuilder();
for (int num : strings.get(i)) {
sb1.append(num);
}
if (i == 0) {
primero=Integer.parseInt(sb1.toString());
}else{
suma+=Integer.parseInt(sb1.toString());
}
}
if(primero==suma)
return primero;
else return -1;
}
}
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.RepeatedTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.ArrayList;
import junit.framework.TestCase;
import java.util.Random;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void test() {
//Correcto
final ArrayList<int[]> aContar = new ArrayList<int[]>();
final int a1[] = { 6, 5, 8 };
final int a2[] = { 8, 9 };
final int a3[] = { 2, 5, 8 };
final int a4[] = { 2, 7, 3 };
final int a5[] = { 3, 8 };
// aContar = List.of(new int[]{6,5,8};new int[]{ 8, 9 };new int[]{ 2, 5, 8 };new int[]{ 2, 7, 3 };new int[]{ 3, 8 })
aContar.add(a1);
aContar.add(a2);
aContar.add(a3);
aContar.add(a4);
aContar.add(a5);
assertEquals(658, Quipu.calculate(aContar));
//Falso
final ArrayList<int[]> Contar1 = new ArrayList<int[]>();
final int a6[] = { 7, 3, 1 };
final int a7[] = { 1, 2 };
final int a8[] = { 4, 3, 2 };
final int a9[] = { 6, 6, 7 };
final int a11[] = { 4, 1 };
aContar.add(a6);
aContar.add(a7);
aContar.add(a8);
aContar.add(a9);
aContar.add(a11);
assertEquals(-1, Quipu.calculate(Contar1));
//vacio
final ArrayList<int[]> vacio = new ArrayList<int[]>();
assertEquals(-1, Quipu.calculate(vacio));
//nulo
assertEquals(-1, Quipu.calculate(null));
assertNotNull(Quipu.calculate(null));
assertNotNull(Quipu.calculate(vacio));
assertNotNull(Quipu.calculate(aContar));
assertNotNull(Quipu.calculate(Contar1));
}
@RepeatedTest(10)
void testRandom() {
for (int i = 0; i < 10; i++) {
final ArrayList<int[]> x = this.arrayAleatorio();
assertEquals(SolutionTest.calculate(x), Quipu.calculate(x));
}
}
final int min = 0, max = 9;
ArrayList<int[]> arrayAleatorio() {
final ArrayList<int[]> ContarRandom = new ArrayList<int[]>();
final Random r = new Random();
final int randomNum1 = r.nextInt(max - min + 1) + min;
final int randomNum2 = r.nextInt(max - min + 1) + min;
final int randomNum3 = r.nextInt(max - min + 1) + min;
final int randomNum4 = r.nextInt(max - min + 1) + min;
final int randomNum5 = r.nextInt(max - min + 1) + min;
final int randomNum6 = r.nextInt(max - min + 1) + min;
final int a1[] = { randomNum1, randomNum2, randomNum3 };
final int a2[] = { randomNum4, randomNum5 };
final int a3[] = { randomNum3, randomNum2, randomNum1 };
final int a4[] = { randomNum6, randomNum4 };
final int a5[] = { randomNum3, randomNum4, randomNum6 };
ContarRandom.add(a1);
ContarRandom.add(a2);
ContarRandom.add(a3);
ContarRandom.add(a4);
ContarRandom.add(a5);
return ContarRandom;
}
private static int calculate(ArrayList<int[]> strings) {
if (strings == null || strings.isEmpty()) {
return -1;
}
int primero = 0;
int suma = 0;
for (int i = 0; i < strings.size(); i++) {
final StringBuilder sb1 = new StringBuilder();
for (final int num : strings.get(i)) {
sb1.append(num);
}
if (i == 0) {
primero = Integer.parseInt(sb1.toString());
} else {
suma += Integer.parseInt(sb1.toString());
}
}
if (primero == suma) {
return primero;
} else {
return -1;
}
}
}
A delivery man has to deliver a package in 15 minutes,the address is in the coordinates [4][5], but his gps is not working right, will he arrive at time?.
Every move takes a minute.
Moves can be NORTH,SOUTH,EST,WEST.
Representing directions ['n','s','e','w'] (No case sensitive).
Also you can find a traffic jam, is represented by '/' and consumes 2 minutes.
Your task is to create a function that return true if he arrives on time, else return false.
Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', 'w' or '' only). It will never give you an empty array (return false).
public class ArriveAtTime{
public static boolean arriveAtTime(char [] movements){
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSimple() {
assertFalse(ArriveAtTime.arriveAtTime(new char[] {'n', 'E', 'N', 'E', 'W', 'S', 'W', 'N', 'W', 'S', 'S', 'E', 'S', 'E' , 'S'}));
assertTrue(ArriveAtTime.arriveAtTime(new char[] {'n', 'N', 'N', 'N', 'E', 'E', 'E', 'E', 'E', 'E', 'S', 'e', 'S', 'E' , 'S'}));
assertFalse(ArriveAtTime.arriveAtTime(new char[] {'n', 'N', 'N', 'N', 'E', 'e', 'E', '/', '/', '/', 'S', 'E', 'S', 'E' , 'S'}));
assertTrue(ArriveAtTime.arriveAtTime(new char[] {'N', 'N', 'N', 'n', 'S', 's', 'N', 'N', 'E', 'E', '/', 'E', 'E', 'E' , 'E'}));
}
}
Your Task
Your bank is tired of its mainframe COBOL accounting software and they hired both of you for a greenfield project in - what a happy coincidence
your favorite programming language!
Your task is to show them that your TDD-fu and your new-age programming language can cope with good ole’ COBOL!
Requirements
Write a class Account that offers the following methods void deposit(int) void withdraw(int) String printStatement()
An example statement would be:
Date Amount Balance
24.12.2015 +500 500
23.8.2016 -100 400
class Account {
void deposit(int amount){
}
String getStatement() {
return "100";
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
// TODO: Replace examples and use TDD by writing your own tests
class AccountTest {
@Test
void deposit_adds_money_to_initial_balance() {
Account account = new Account();
account.deposit(100);
String statement = account.getStatement();
assertTrue(statement.contains("100"));
}
}
File "c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 68, in __exec_python_cmd
txt = exec_python(*cmd, env=pp_env)
File "c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\compat.py", line 526, in exec_python
return exec_command(*cmdargs, **kwargs)
File "c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\compat.py", line 321, in exec_command
out = out.decode(encoding)
AttributeError: 'str' object has no attribute 'decode'
1234567
File "c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\utils\hooks\__init__.py", line 68, in __exec_python_cmd
txt = exec_python(*cmd, env=pp_env)
File "c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\compat.py", line 526, in exec_python
return exec_command(*cmdargs, **kwargs)
File "c:\users\administrator\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\compat.py", line 321, in exec_command
out = out.decode(encoding)
AttributeError: 'str' object has no attribute 'decode'
1234567