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.
Write a function that processes the properties of a given object, by the following rules:
- Each property that has an underscore (
_
) in its name will be converted to a nested object - Properties that have only underscores or no properties, will remain the same
- The last nested property will hold the original value
Example:
const beforeTransformation = {
a_b_c: "value"
}
const afterTransformation = {
a: {
b: {
c: "value"
}
}
}
export const transform = (source: Record<string, any>): Record<string, any> => { const target = Object.create(null); // TODO: handle invalid property Object.entries(source).forEach(([key, value]) => { key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => { return node[element] ??= {}; }, target)[key.slice(key.lastIndexOf("_") + 1)] = value; }); return target; }
export function transform(source: any) {const target = {};Object.entries(source).forEach(function ([k, v]) {k.split("_").slice(0, -1).reduce(function (node: {[key: string]: any}, element: string) {- export const transform = (source: Record<string, any>): Record<string, any> => {
- const target = Object.create(null);
- // TODO: handle invalid property
- Object.entries(source).forEach(([key, value]) => {
- key.split("_").slice(0, -1).reduce((node: Record<string, any>, element: string) => {
- return node[element] ??= {};
}, target)[k.slice(k.lastIndexOf("_") + 1)] = v;- }, target)[key.slice(key.lastIndexOf("_") + 1)] = value;
- });
- return target;
- }
// See https://www.chaijs.com for how to use Chai. import { assert } from "chai"; import { transform } from "./solution"; describe("object transformer function", () => { it("should handle single property", () => { const before = { a_b_c : 'value' } const after = { a : { b: { c: 'value' } } } assert.deepEqual(transform(before), after); }); it("should handle multiple property", () => { const before = { a_b_c : 'value', d:'value_d' } const after = { a : { b: { c: 'value' } }, d:'value_d' } assert.deepEqual(transform(before), after); }) it("should handle invalid property", () => { const before = { _: 'value' }; const after = { _:'value_d'}; assert.deepEqual(transform(before), after); }) it("should handle empty object", () => { const before = Object.create(null); const after = Object.create(null); assert.deepEqual(transform(before), after); }) });
- // See https://www.chaijs.com for how to use Chai.
- import { assert } from "chai";
- import { transform } from "./solution";
// TODO Add your tests heredescribe("example", function() {const before = { a_b_c : 'value' }const before_2 = { a_b_c : 'value', d:'value_d' }const after = {a : {b: {c: 'value'}}}const after_2 = {a : {b: {c: 'value'- describe("object transformer function", () => {
- it("should handle single property", () => {
- const before = { a_b_c : 'value' }
- const after = {
- a : {
- b: {
- c: 'value'
- }
- }
- }
},d:'value_d'}it("test", function() {- assert.deepEqual(transform(before), after);
assert.deepEqual(transform(before_2), after_2);- });
- it("should handle multiple property", () => {
- const before = { a_b_c : 'value', d:'value_d' }
- const after = {
- a : {
- b: {
- c: 'value'
- }
- },
- d:'value_d'
- }
- assert.deepEqual(transform(before), after);
- })
- it("should handle invalid property", () => {
- const before = { _: 'value' };
- const after = { _:'value_d'};
- assert.deepEqual(transform(before), after);
- })
- it("should handle empty object", () => {
- const before = Object.create(null);
- const after = Object.create(null);
- assert.deepEqual(transform(before), after);
- })
- });
Given a string, complete the function so it returns the string with every 2nd character removed.
example:
removeEverySecond('hello world') should return 'hlowrd' ```
def generate_binary_list(a, b, c, d): (a, b) = (a, b, a)[d&1:][:2] return [*((a, b)*(c-d>>1)), *(a,)[:c>d][:c-d&1]]
- def generate_binary_list(a, b, c, d):
(a, b) = ((a, b), (b, a))[d&1]return [*((a, b)*(c-d>>1)), *((), (a,))[c>d][:c-d&1]]- (a, b) = (a, b, a)[d&1:][:2]
- return [*((a, b)*(c-d>>1)), *(a,)[:c>d][:c-d&1]]
class StringParity { public static boolean isEvenLength(String str) { return (str.length() & 1) == 0; } }
- class StringParity {
public static boolean isEvenLength(String str) {return str.length() % 2 == 0;}- public static boolean isEvenLength(String str) {
- return (str.length() & 1) == 0;
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void simpleTests() { String ss[] = {"even", "odd", "", "Instant Noodles", "\n\n"}; for (String s : ss) { boolean expected = s.length() % 2 == 0; boolean actual = StringParity.isEvenLength(s); if (expected != actual) System.out.println("input string: " + s); assertEquals(expected, actual); } } @Test void randomTests() { for (int i = 0; i < 1000; ++i) { String s = java.util.UUID.randomUUID().toString(); boolean expected = s.length() % 2 == 0; boolean actual = StringParity.isEvenLength(s); if (expected != actual) System.out.println("input string: " + s); assertEquals(expected, actual); } } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
void testSomething() {assertEquals(true, StringParity.isEvenLength("even"));assertEquals(false, StringParity.isEvenLength("odd"));assertEquals(true, StringParity.isEvenLength(""));assertEquals(false, StringParity.isEvenLength("Instant Noodles"));assertEquals(true, StringParity.isEvenLength("\n\n"));- void simpleTests() {
- String ss[] = {"even", "odd", "", "Instant Noodles", "\n\n"};
- for (String s : ss) {
- boolean expected = s.length() % 2 == 0;
- boolean actual = StringParity.isEvenLength(s);
- if (expected != actual) System.out.println("input string: " + s);
- assertEquals(expected, actual);
- }
- }
- @Test
- void randomTests() {
- for (int i = 0; i < 1000; ++i) {
- String s = java.util.UUID.randomUUID().toString();
- boolean expected = s.length() % 2 == 0;
- boolean actual = StringParity.isEvenLength(s);
- if (expected != actual) System.out.println("input string: " + s);
- assertEquals(expected, actual);
- }
- }
- }