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.
Create a function wich takes in a list of tuples which contain a name of a worker and their annualy salary and return a
tuple with the five workers with the highest salary in descending order. Unluckily for you, the salary of some workers is unknown and hence represented as "-". Those workers shouldn't be in your final tuple.
Examples:
Input: [("max", "20000"), ("jason", "40000"), ("christian", "70000"), ("carl", "50000")] => Output: ("christian", "carl", "jason", "max")
Input: [("worker0", "-"), ("stephy", "100000"), ("king", "10000")] => Output: ("stephy", "king")
Input: [("ironman", "20000"), ("batman", "15000"), ("kenobi", "150000"), ("mr. x", "40000"), ("spiderman", "75000"), ("ramsay", "115000")] =>
Output: ("kenobi", "ramsay", "spiderman", "mr. x", "ironman")
Happy coding :-)
def sort_by_salary(workers):
filtered = list(filter(lambda x: x[1] != "-", workers))
if not filtered:
return tuple()
new = list(sorted(filtered, key=lambda x: int(x[1])))[::-1]
return tuple(i[0] for i in new)[:5]
ax^2 + bx + c = 0
(Vietnamese)
//Usefull for grade 9 in VN
//I'm form VN :)
//Sorry I don't know much about English
//IamQuan :3
//Libraly
#include<iostream>
#include <math.h>
using namespace std;
//Code
//Ham Phu de goi an cho a b c
double GoiAn()
{
double ABC{};
std::cin >> ABC;
return ABC;
}
//Code
int main()
{
//Khai bao cho DK
int A{};
//Khai bao cho tinh toan
double x1, x2;
//--------------------------------------------------
// User nhap a b c
cout << "Chuong trinh tinh phuong trinh bac 2 mot an\n";
cout << "Vui long nhap cac he so a , b , c\n";
cout << "---------------------------------------------" << endl;
//Nhap he so a
cout << "Nhap he so a: " ;
double a{GoiAn()};
//Dieu kien
while (a == 0 && A < 1000)
{
cout << "Phuong trinh da chuyen ve \nphuong trinh bac nhat mot an" << endl;
cout << "Ban co muon nhap lai ham so a ?\nNeu muon thi nhap lai.\na:" << endl;
cin >> a;
A++;
}
//Nhap he so b
cout << "Nhap he so b: ";
double b{ GoiAn() };
//Nhap he so c
cout << "Nhap he so c: ";
double c{ GoiAn() };
// Thong bao cho user
cout << "Dang tinh delta va nghiem ......." << endl;
//Khai bao Delta
double delta{ (b * b) - 4 * a * c };
//Tinh delta
if (delta < 0)
{
cout << "Delta = " << delta << "\n";
cout << " => Phuong trinh vo nghiem " << endl;
}
else if (delta == 0)
{
cout << "Delta = " << delta << "\n";
cout << " => Phuong trinh co nghiem kep " << endl;
}
else if (delta > 0)
{
cout << "Delta = " << delta << "\n";
cout << " => Phuong trinh co 2 nghiem phan biet " << endl;
}
cout << "\n\n";
//Tinh nghiem
if (delta > 0)
{
x1 = (-b - sqrt(delta)) / (2 * a);
x2 = (-b + sqrt(delta)) / (2 * a);
cout << "Nghiem cua phuong trinh la: \n";
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (delta == 0) {
x1 = x2 = -b / (2 * a);
cout << "Nghiem cua phuong trinh la: \n";
cout << "x1 = x2 = " << x1 << endl;
}
else
{
cout << "Vi phuong trinh vo nghiem nen khong co nghiem" << endl;
}
system("pause");
return 0;
}
Create a function called sort_names()
which takes a string of names each separated by a space and returns them as a string with the names sorted and in title case. See below.
'Kate Amara Bob Justin' --> 'Amara Bob Justin Kate'
'kate amara bob justin' --> 'Amara Bob Justin Kate'
def sort_names(s):
return ' '.join(sorted(s.title().split()))
@test.describe('Fixed Tests')
def fixed():
@test.it('Basic Tests')
def basic():
test.assert_equals(sort_names('Kate Amara Bob Justin'),'Amara Bob Justin Kate')
test.assert_equals(sort_names('kate amara justin bob'),'Amara Bob Justin Kate')
@test.describe('Random Tests')
def random_tests():
@test.it("Random Tests")
def test_cases():
for i in range(100):
input = get_input(random.randint(0,15))
result = sort_names_correct(input[:])
test.it(f'Testing {input}: should equal {result}')
test.assert_equals(sort_names(input),result)
Create a function to return True if the number is even and False if odd.
e.g. check_even(2) => True, check_even(9) => False
check_even = lambda x: x%2==0
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Test1")
def test_group():
@test.it("test true: 2")
def test_case():
test.assert_equals(check_even(2), True)
@test.describe("Test2")
def test_group():
@test.it("test false: 5")
def test_case():
test.assert_equals(check_even(5), False)
@test.describe("Test3")
def test_group():
@test.it("test false: 11")
def test_case():
test.assert_equals(check_even(11), False)
@test.describe("Test4")
def test_group():
@test.it("test true: 22")
def test_case():
test.assert_equals(check_even(22), True)
Your task is to find the minimum SQUARE area in which you can place two identical rectangles. The sides of the rectangles should be parallel to the sides of the square.
- You are given two identical rectangles with side lengths
a
andb
with1 <= a, b <= 100
. - Find the square with minimum area that contains both given rectangles. Rectangles can be rotated (both or just one) and moved, but the sides of the rectangles should be parallel to the sides of the desired square.
def minimal_square(a, b):
return max(min(a, b)*2, max(a, b))**2
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Simple tests")
def test_group():
@test.it("Test 1")
def test_case1():
test.assert_equals(minimal_square(2, 3), 16)
@test.it("Test 2")
def test_case2():
test.assert_equals(minimal_square(3, 7), 49)
Given an integer array and an positive integer variable, find the maximum or largest product you get after multiplying it by the elements in the array.
Input: arr = {1,2,3};
num = 3;
Output: 6
Because, 3 * 2 = 6
class Kata1 {
public static int maxPairProduct(int[] arr, int num) {
//Never Gonna Give You Up, Never Gonna Let You Down, Never Gonna Run Around And Desert You!
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD by writing your own tests
public class SolutionTest {
@Test
public void testSomething() {
// assertEquals("expected", "actual");
int[] arr1 = {1,2,3};
int[] arr2 = {5,8,3,7,10};
int[] arr3 = {21,34,2,12,65,4};
assertEquals(6, Kata1.maxPairProduct(arr1, 3));
assertEquals(80, Kata1.maxPairProduct(arr2,8));
assertEquals(780, Kata1.maxPairProduct(arr3, 12));
}
}
Basics
Verlan is a type of argot in the french language and is common in slang and youth language.
It rests on a long french tradition of transposing syllables of individual words to create slang words.
The word itself is an example for verlan.
Task
In this example you will create a function that takes a word and makes a verlan word out of it.
e.g.
- Maestro => Stromae
- Verlan => Lanver
function maestroSplit(string) {
let newWord = string.slice(parseInt(string.length / 2)) + string.slice(0, parseInt(string.length / 2));
return newWord.toLowerCase().charAt(0).toUpperCase() + newWord.toLowerCase().slice(1);
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
const Test = require("@codewars/test-compat");
describe("maestroSplit", function() {
it("should transform verLan to Lanver", () => {
Test.assertEquals(maestroSplit("verLan"), "Lanver");
});
it("should transform français to Çaisfran", () => {
Test.assertEquals(maestroSplit("français"), "Çaisfran");
});
it("should transform Maestro to Stromae", () => {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
Test.assertEquals(maestroSplit("Maestro"), "Stromae");
});
it("should transform MaEsTrO to Stromae", () => {
Test.assertEquals(maestroSplit("MaEsTrO"), "Stromae");
});
it("should transform warscode to Codewars", () => {
Test.assertEquals(maestroSplit("wArScODE"), "Codewars");
});
});
Given the following numbers:
100 75 50 25 6 3
and the following operators:
(*) (-) (+) (/)
build an Expression
, that yields 952
when it is evaluated (evaluate
applies all the operators bottom - up)
Doubles are used for convenience, number is rounded and checked against an integer to mitigate floating point error that might accumulate.
module Example (solve, Expression(Value, Expr)) where
data Expression = Value Double | Expr Expression (Double -> Double -> Double) Expression
solve :: [Double] -> Expression
--solve = undefined
--- my brute-force solution
solve xs = head . filter validate . allExpressions os . allOrders $ xs
where os = getPermutationsAtDepth (length xs - 1) ops
evaluate :: Expression -> Double
evaluate (Value x) = x
evaluate (Expr i o j) = (evaluate i) `o` (evaluate j)
validate :: Expression -> Bool
validate = (==952) . round . evaluate
-- ok because all the numbers are unique
removed :: Eq a => a -> [a] -> [a]
removed x = filter (/=x)
allOrders :: Eq a => [a] -> [[a]]
allOrders [x] = [[x]]
allOrders xs = concat [ [ x:ns | ns <- allOrders (removed x xs)] | x <- xs]
ops = [(*), (-), (+), (/)] :: [Op]
type Op = Double -> Double -> Double
data NodeTree a = Node a [NodeTree a] | Leaf a deriving (Show)
genTree :: a -> [a] -> Int -> NodeTree a
genTree root es 1 = Node root (map Leaf es)
genTree root es depth = Node root (map (\x -> genTree x es (depth-1)) es)
getPaths :: NodeTree a -> [[a]]
getPaths (Leaf x) = [[x]]
getPaths (Node r xs) = concat [ [ r:t | t <- getPaths c] | c <- xs]
getPermutationsAtDepth :: Int -> [a] -> [[a]]
getPermutationsAtDepth 1 xs = [xs]
getPermutationsAtDepth depth xs = concat . map getPaths . map (\x -> genTree x xs d) $ xs where d = depth - 1
toExpression :: [Op] -> [Double] -> Expression
toExpression _ [] = error "should not be more operators than values!"
toExpression [] [e] = Value e
toExpression (o:os) (e:es) = Expr (Value e) o (toExpression os es)
allExpressions :: [[Op]] -> [[Double]] -> [Expression]
allExpressions os es = [toExpression o e | o <- os, e <- es]
module ExampleSpec where
-- Tests can be written using Hspec http://hspec.github.io/
-- Replace this with your own tests.
import Test.Hspec
import Example
import Data.List
numbers = [100, 75, 50, 25, 6, 3] :: [Double]
collectNums :: Expression -> [Double]
collectNums (Value x) = [x]
collectNums (Expr x _ y) = collectNums x ++ collectNums y
evaluate :: Expression -> Double
evaluate (Value x) = x
evaluate (Expr i o j) = (evaluate i) `o` (evaluate j)
validate :: Expression -> Bool
validate = (==952) . round . evaluate
-- `spec` of type `Spec` must exist
spec :: Spec
spec = do
describe "get the solution" $ do
it "must use each number once and only once" $ do
(sort . collectNums . solve $ numbers) `shouldBe` (sort numbers)
it "must equal 952" $ do
-- round because of floating point error
(round . evaluate . solve $ numbers) `shouldBe` (952 :: Int)
-- the following line is optional for 8.2
main = hspec spec
Write a function that will solve a 9x9 Sudoku puzzle. The function will take one argument consisting of the 2D puzzle array, with the value 0 representing an unknown square.
The Sudokus tested against your function will be "easy" (i.e. determinable; there will be no need to assume and test possibilities on unknowns) and can be solved with a brute-force approach.
For Sudoku rules, see the Wikipedia article.
const int sudoku[9][9] = {
{0,0,0,0,0,0,6,0,0},
{0,0,0,0,0,7,0,0,1},
{2,0,8,0,0,0,0,4,0},
{6,0,0,0,0,0,0,0,0},
{0,5,1,4,0,9,0,0,0},
{0,0,0,5,0,0,0,2,0},
{0,0,4,0,0,0,0,5,0},
{0,0,7,1,9,6,0,0,0},
{0,8,0,0,0,0,3,0,0},
};
#include <stddef.h>
typedef int (*T)[9];
T solve (const T sudoku) {
/* Write your code here */
return NULL;
}
// TODO: Replace examples and use TDD by writing your own tests. The code provided here is just a how-to example.
#include <criterion/criterion.h>
#include <stdbool.h>
typedef int (*T)[9];
extern T solve(const int (*)[9]);
const int a[9][9] = {
{0,0,0,0,0,0,6,0,0},
{0,0,0,0,0,7,0,0,1},
{2,0,8,0,0,0,0,4,0},
{6,0,0,0,0,0,0,0,0},
{0,5,1,4,0,9,0,0,0},
{0,0,0,5,0,0,0,2,0},
{0,0,4,0,0,0,0,5,0},
{0,0,7,1,9,6,0,0,0},
{0,8,0,0,0,0,3,0,0},
};
const int b[9][9] = {
{0,0,0,0,0,6,6,0,0},
{0,0,0,0,0,7,0,0,1},
{2,0,8,0,0,0,0,4,0},
{6,0,0,0,0,0,0,0,0},
{0,5,1,4,0,9,0,0,0},
{0,0,0,5,0,0,0,2,0},
{0,0,4,0,0,0,0,5,0},
{0,0,7,1,9,6,0,0,0},
{0,8,0,0,0,0,3,0,0},
};
bool check( int(*x)[9] );
Test(the_multiply_function, should_pass_all_the_tests_provided) {
cr_assert_eq(check(solve(a)), true);
cr_assert_eq(solve(b), NULL);
}
bool check( int(*x)[9] ) {
if(!x) return false;
for(int i=0;i<9;i++){
for(int j=0,k[9]={0};j<9;j++){
if(!x[i][j])return false;
if(k[x[i][j]-1])return false;
else k[x[i][j]-1] = 1;
}
for(int j=0,k[9]={0};j<9;j++)
if(k[x[j][i]-1])return false;
else k[x[j][i]-1] = 1;
}
for(int i=0;i<3;i++)for(int j=0;j<3;j++)
for(int m[9]={0},k=0;k<3;k++)for(int l=0;l<3;l++)
if(m[x[i*3+k][j*3+l]-1])return false;
else m[x[i*3+k][j*3+l]-1] = 1;
return true;
}
test
public class test{
public void testSomething(){
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD by writing your own tests
public class SolutionTest {
@Test
public void testSomething() {
// assertEquals("expected", "actual");
}
}