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.
Just realized bug in the problem. If there are 2 negative lengths and 1 positive lengths, it will still return a positive integer. Thus, we test if the cube is valid in the first place.
def kube(l,w,h): if l < 0 or w < 0 or h < 0: return "Invalid Cube" else: return l * w * h
kube = lambda l, w, h: max(l * w * h, 0)- def kube(l,w,h):
- if l < 0 or w < 0 or h < 0:
- return "Invalid Cube"
- else:
- return l * w * h
import codewars_test as test # TODO Write tests from solution import kube # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(kube(7, 9, 4), 252) test.assert_equals(kube(5, 12, 7), 420) test.assert_equals(kube(3, 7, 49), 1029) test.assert_equals(kube(-1,1,4), "Invalid Cube")
- import codewars_test as test
- # TODO Write tests
- from solution import kube
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(kube(7, 9, 4), 252)
- test.assert_equals(kube(5, 12, 7), 420)
- test.assert_equals(kube(3, 7, 49), 1029)
test.assert_equals(kube(-1,1,4), 0)- test.assert_equals(kube(-1,1,4), "Invalid Cube")
def cap(low, high, num): x, y = min(low, high, num), max(low, high, num) return sum([low, high, num]) - sum([x, y])
- def cap(low, high, num):
if num < low: return lowelif num > high: return highelse: return num- x, y = min(low, high, num), max(low, high, num)
- return sum([low, high, num]) - sum([x, y])
from inspect import getsource def quine(): source = getsource(quine) message = 'CodeWars' return source
- from inspect import getsource
- def quine():
q = '"' * 3s = """def quine():q = '"' * 3s = %s%s%sreturn s %% (q, s, q)"""return s % (q, s, q)- source = getsource(quine)
- message = 'CodeWars'
- return source
import codewars_test as test # TODO Write tests import solution # or from solution import example # test.assert_equals(actual, expected, [optional] message) @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(1,1)
- import codewars_test as test
- # TODO Write tests
- import solution # or from solution import example
- # test.assert_equals(actual, expected, [optional] message)
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
test.assert_equals(p(1),1)- test.assert_equals(1,1)
Naive solution.
I think this would be more interesting if the challenge was to generate as few characters of b**infuck as possible.
maybe add a limit to *ra*nfuck characters in the test cases? might be fun.
#include<stddef.h> #include<string.h> #include<stdlib.h> char buf[4096] = {0}; char* s2bf(char *arr) { char cv = 0; char *r; while(*arr){ while(cv != *arr){ strcat(buf, "+"); cv++; } strcat(buf, "."); arr++; } r = malloc(4096); strcpy(r, buf); return r; }
- #include<stddef.h>
- #include<string.h>
- #include<stdlib.h>
char s[4096] = "";- char buf[4096] = {0};
- char*
s2bf(char *argv) {register int i = 0,j;register int k,m;register int l,last = 0;while (argv[++i]!=0)continue;for (j=0; j<i; j++) {register int t;t = argv[j];l = k = t-last;last = t;#ifdef DEBUGprintf("\n%d, %d, %d, %d\n",last,k,j,i);#endifif (k >= 0) {k >>= 3; // k /= 8l &= 7; //在更多的机器工作,求余if (k == 1)strcat(s,">++++++++");else {if (k != 0) {strcat(s,"++++++++[>");for (m=0; m<k; m++)strcat(s,"+");strcat(s,"<-]>");} else {strcat(s,">");}}for (m=0; m<l; m++)strcat(s,"+");strcat(s,".");} else {k = -k >> 3; // k /= 8l = -l & 7; //在更多的机器工作,求余if (k == 1)strcat(s,">--------");else {strcat(s,"++++++++[>");for (m=0; m<k; m++)strcat(s,"-");strcat(s,"<-]>");}for (m=0; m<l; m++)strcat(s,"-");strcat(s,".");- s2bf(char *arr) {
- char cv = 0;
- char *r;
- while(*arr){
- while(cv != *arr){
- strcat(buf, "+");
- cv++;
- }
#ifdef DEBUGprintf("\n%d, %d, %d\n",m,k,l);#endifstrcat(s,"<");- strcat(buf, ".");
- arr++;
- }
char* q = malloc(4096);strcpy(q, s);return q;- r = malloc(4096);
- strcpy(r, buf);
- return r;
- }
#include<criterion/criterion.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #include<stdlib.h> char* s2bf(char*); int validbf(char *a) { int b; b = 0; while(*a){ switch(*a){ case '[': b++; break; case ']': b--; if(b < 0) return 0; break; } a++; } if(b) return 0; return 1; } void runbf(char**cell, char*bf, char **o) { int lc; while(*bf){ switch(*bf){ case '[': if(**cell){ runbf(cell, bf+1, o); bf--; }else{ lc = 0; do{ switch(*bf){ case '[': lc++; break; case ']': lc--; break; } bf++; }while(lc); continue; } break; case ']': return; break; case ',': **cell = rand(); break; case '.': **o = **cell; (*o)++; break; case '<': (*cell)--; break; case '>': (*cell)++; break; case '+': (**cell)++; break; case '-': (**cell)--; break; } bf++; } } char* evalbf(char *bf) { char out[400] = {0}; char cells[4096] = {0}; char *cp = cells + 2048; char *o = out; runbf(&cp, bf, &o); char*r = malloc(o - out + 1); strcpy(r, out); return r; } Test(s2bf, bf_validity) { char *bf; bf = s2bf("ab"); cr_assert_eq(validbf(bf), 1); free(bf); } Test(s2bf, hello_world){ char *bf; char *op; bf = s2bf("Hello World!"); printf("%s\n", bf); op = evalbf(bf); cr_assert_eq(strcmp(op,"Hello World!"),0); free(bf); free(op); }
- #include<criterion/criterion.h>
- #include<string.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<stdlib.h>
- char* s2bf(char*);
- int
- validbf(char *a)
- {
- int b;
- b = 0;
- while(*a){
- switch(*a){
- case '[':
- b++;
- break;
- case ']':
- b--;
- if(b < 0)
- return 0;
- break;
- }
- a++;
- }
- if(b)
- return 0;
- return 1;
- }
- void
- runbf(char**cell, char*bf, char **o)
- {
- int lc;
- while(*bf){
- switch(*bf){
- case '[':
- if(**cell){
- runbf(cell, bf+1, o);
- bf--;
- }else{
- lc = 0;
- do{
- switch(*bf){
- case '[':
- lc++;
- break;
- case ']':
- lc--;
- break;
- }
- bf++;
- }while(lc);
- continue;
- }
- break;
- case ']':
- return;
- break;
- case ',':
- **cell = rand();
- break;
- case '.':
- **o = **cell;
- (*o)++;
- break;
- case '<':
- (*cell)--;
- break;
- case '>':
- (*cell)++;
- break;
- case '+':
- (**cell)++;
- break;
- case '-':
- (**cell)--;
- break;
- }
- bf++;
- }
- }
- char*
- evalbf(char *bf)
- {
- char out[400] = {0};
- char cells[4096] = {0};
- char *cp = cells + 2048;
- char *o = out;
- runbf(&cp, bf, &o);
- char*r = malloc(o - out + 1);
- strcpy(r, out);
- return r;
- }
- Test(s2bf, bf_validity) {
- char *bf;
- bf = s2bf("ab");
- cr_assert_eq(validbf(bf), 1);
- free(bf);
- }
- Test(s2bf, hello_world){
- char *bf;
- char *op;
- bf = s2bf("Hello World!");
- printf("%s\n", bf);
- op = evalbf(bf);
- cr_assert_eq(strcmp(op,"Hello World!"),0);
- free(bf);
- free(op);
- }