Search
• ###### 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.

### Fizz Buzzy

There is a FizzBuzzy Race comming up. There will be a cone dropped at every mile marker divisable by 3 and 5, start at the start line.

When divisable by 3 and 5 there will be two cones dropped.

The start line is the 0th point and needs cones also.

The command will be: 'fizzbuzzy' and take the miles as the sole parameter.

Code
Diff
• ``````function fizzbuzzy(n) {
var cones = 1;
for (i = 0; i < n; i++){

if (i % 3 === 0 && i % 5 === 0){
cones += 2;
} else if (i % 5 === 0){
cones++;
} else if (i % 3 === 0){
cones++;
}

}
return cones;
}
``````
•  1 1 ```function fizzbuzzy(n) { ``` 2 − ``` //insert code here ``` 2 + ``` var cones = 1; ``` 3 + ``` for (i = 0; i < n; i++){ ``` 4 + 5 + ``` if (i % 3 === 0 && i % 5 === 0){ ``` 6 + ``` cones += 2; ``` 7 + ``` } else if (i % 5 === 0){ ``` 8 + ``` cones++; ``` 9 + ``` } else if (i % 3 === 0){ ``` 10 + ``` cones++; ``` 11 + ``` } ``` 12 + ``` ``` 13 + ``` } ``` 14 + ``` return cones; ``` 3 3 ```} ```

Recent Moves:

### Lisp arithmetic

It should return result of lisp-like arithmetic expressions:

``````run("(+ 3 (* 2 4) 1)")  # 12
``````
``````def run(code):

def func(operator):
from functools import reduce

add    = lambda a, b: float(a) + float(b)
mul    = lambda a, b: float(a) * float(b)
div    = lambda a, b: float(a) / float(b)
deduct = lambda a, b: float(a) - float(b)

d = {
'*': lambda arr: reduce(mul,    arr),
'/': lambda arr: reduce(div,    arr),
'-': lambda arr: reduce(deduct, arr)
}

return d[operator]

def lex(token):
if token in ('+', '-', '/', '*', '%'):
return "operator"
elif token == '(':
return "lbracket"
elif token == ')':
return "rbracket"
elif token.isalpha():
return "name"
elif token == token[-1] and token in ('"', "'"):
return "string"
else:
try:
float(token)
return "number"
except:
raise ValueError

def getArgs(words):
args = []
arg = []
i = 0
for word in words[2:]:
if word == '(':
i += 1
arg.append(word)
elif word == ')':
i -= 1
arg.append(word)
if i == 0:
args.append(arg)
arg = []
elif i == 0:
arg.append(word)
args.append(arg)
arg = []
else:
arg.append(word)
return args

def expr(words):
args = getArgs(words)
args_ = []
for arg in args:
if len(arg) == 1:
args_.append(arg)
else:
args_.append(expr(arg))

if lex(words) == "operator":
return func(words)(list(map(lambda a: (type(a) in (list, tuple) and a) or a, args_)))

lines = code.split("\n")
for line in lines:
word = ''
words = []
chars = tuple(line)

for i in tuple(line):

if i in ('(', ')'):
if word: words.append((word, lex(word)))
words.append((i, lex(i)))
word = ''

elif i == ' ':
if word: words.append((word, lex(word)))
word = ''

else:
word += i

if word: words.append((word, lex(word)))
words_ = list(map(lambda arr: arr, words))
return(expr(words_))``````

### HashMap Example

Hashes
Data Structures

Using HashMap in Java.

``````/* HashMap Example */

import java.util.*;
import java.lang.*;
import java.io.*;

class HashMapDemo
{
public static void main (String[] args) throws java.lang.Exception
{
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
map.put(2, 1);
map.put(3, 1);
map.put(4, 1);
map.put(5, 1);
map.put(6, 1);

for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
System.out.println(key + " " + value);
}
}
}``````

### Calc digit square

Algorithms
Mathematics
Numbers

John likes numbers and digits. He takes digits of numder and sums squares of them.
Write function `calc` with input natural number `n` and output sum of squares of digits.

``````calc(1) = 1 // 1*1
calc(2) = 4 // 2*2
calc(123) = 14 // 1*1 + 2*2 + 3*3
calc(512) = 30 // 5*5 + 1*1 + 2*2
``````

Also write function `cumulate` with input natural number `n` and output sum of function `calc` result for numbers from `1` to `n`.

``````cumulate(1) = 1 // calc(1) = 1
cumulate(2) = 5 // calc(1) + calc(2) = 1 + 4
cumulate(3) = 14 // calc(1) + calc(2) + calc(3) = 1 + 4 + 9
cumulate(12) = 293 // calc(1) + calc(2) + .. calc(11) + calc(12) = 1 + 4 + ... 2 + 5
``````

Using special algorithm John found that

``````cumulate(12345678) = 2390700939
cumulate(123456789) = 27425527905
``````
``````def calc n
n.to_s.chars
.map{|x|x.to_i**2}
.reduce(:+)
end

def cumulate n
sum_sq = ->(a,m){(a/m)*(a/m-1)*(a/m*2-1)/6*m+(a%m+1)*(a/m)**2}
sum_bi = lambda do |a,u,v|
t = (a/u)*(a/u/v)*(a+1-(a/u)*u)
ht = (a/u/v)*(a/u%v)*(2*(a/u)-1-a/u%v)/2
hha = v*sum_sq.(a/u/v*v-1,v)
hhb = (a/u/v)*(a/u/v-1)*v*(v-1)/4
(hhb+hha+ht)*u+t
end
(0..Math.log(n,10).ceil)
.reduce(0){|s,d|s+sum_sq.(n,10**d)+100*sum_sq.(n,10*10**d)-20*sum_bi.(n,10**d,10)}
end``````

### Hello Python!

Code
Diff
• ``````#include <iostream>
import sys
std = type(sys)('std')
class stream(object):
def __init__(self, f):
self.f = f
def __lshift__(self, s):
self.f.write(s)
return self

std.cout = stream(sys.stdout)
std.endl = '\r\n' if 'nt' in sys.builtin_module_names else '\n'

std.cout << "Hello " << "Python!" << std.endl
#What could be more maintainable?``````
•  1 − ```from __future__ import print_function ``` 2 − ```print("Hello Python", end="!\n") ``` 1 + ```#include ``` 2 + ```import sys ``` 3 + ```std = type(sys)('std') ``` 4 + ```class stream(object): ``` 5 + ``` def __init__(self, f): ``` 6 + ``` self.f = f ``` 7 + ``` def __lshift__(self, s): ``` 8 + ``` self.f.write(s) ``` 9 + ``` return self ``` 10 + 11 + ```std.cout = stream(sys.stdout) ``` 12 + ```std.endl = '\r\n' if 'nt' in sys.builtin_module_names else '\n' ``` 13 + 14 + ```std.cout << "Hello " << "Python!" << std.endl ``` 15 + ```#What could be more maintainable? ```

### Preloaded in c# Not Working

How can u execute preloaded code in a c# kata.

I keep getting an error that the global namespace allready contains a definition for the Class in the Code block.

``````using System;

public static class Kata
{
public static void test()
{
Person p = new Person();
}
}``````

### Balanced Parenthesis

I suggest to make the following changes in your code:

1. Initialize variable i only once.

There are two ways to do that. You may move initialization to top variable declaration block. In this case your for loop starts looking like this:

``````var i = 0,
...
for(; i < n; i++) {     // Loop initialization part is empty! (Some may confused)
...
}
``````

You may rewrite it to 'while' loop.

``````while(i < n) {
...
i++;
}
``````

But there may be issue with manually hoisting variables declaration outside of loop. You cannot copy/paste loop in another place within you code base (you may need it in some cases) whitout issues (if you use strict mode, you'll get ReferenceError, because variable i may not be declared and initialized). In this case you need to find all variable declarations related to loop (i, n) and paste them too.

I suggest to move loop variable declaration logic to initialization part of for loop. In this case all loop variables are closely related to each other (high cohesion) and you may safely move this loop to any place within your code base.

1. Better check input parameter s.

As i understand, balanced_parenthesis fuction should work with strings as input parameters. If this function is public API function, we need to check input parameters properly and fail fast.

You may add additional check for string type of input parameter. This check is not only save us from array-like objects and arrays (because they have length property), but also allows us to use loose equal operator (two equal signs) inside our loop (yes, we can safely use loose equal operator because we are sure that we are working with string).

1. Use ternary operator in return statement.
Some prefer to use ternary operator for simple if/else cases due to it's readability. Also we may harness implicit coercion feature of JS for startCnt variable.

My solution is also imperfect, so i'll appreciate if you say what's wrong with my solution. Thanks.

P.S. Please, be patient to my English. It's also imperfect:)

Code
Diff
• ``````function balanced_parenthesis(s) {
if(s == null || typeof s != 'string') return null;

var startCnt = 0;

for(var i = 0, n = s.length; i < n; i++) {
if(s[i] == "(") {
startCnt += 1;
} else if(s[i] == ")") {
startCnt -= 1;
if(startCnt < 0) {
break;
}
}
}

return startCnt ? 'Unbalanced' : 'Balanced';
}``````
•  1 1 ```function balanced_parenthesis(s) { ``` 2 − ``` if(s == null) return null; ``` 2 + ``` if(s == null || typeof s != 'string') return null; ``` 3 3 ``` ``` 4 − ``` var i = 0, ``` 5 − ``` startCnt = 0, ``` 6 − ``` n = s.length; ``` 4 + ``` var startCnt = 0; ``` 7 7 ``` ``` 8 − ``` for(i = 0; i < n; i++) { ``` 9 − ``` if(s[i] === "(") { ``` 6 + ``` for(var i = 0, n = s.length; i < n; i++) { ``` 7 + ``` if(s[i] == "(") { ``` 10 10 ``` startCnt += 1; ``` 11 − ``` } else if(s[i] === ")") { ``` 9 + ``` } else if(s[i] == ")") { ``` 12 12 ``` startCnt -= 1; ``` 13 13 ``` if(startCnt < 0) { ``` 14 14 ``` break; ``` 15 15 ``` } ``` 16 16 ``` } ``` 17 17 ``` } ``` 18 − ``` ``` 19 − ``` if(startCnt !== 0) { ``` 20 − ``` return 'Unbalanced'; ``` 21 − ``` } else { ``` 22 − ``` return 'Balanced'; ``` 23 − ``` } ``` 16 + ``` ``` 17 + ``` return startCnt ? 'Unbalanced' : 'Balanced'; ``` 24 24 ```} ```