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.
import java.util.Map; import java.util.HashMap; import java.util.Set; import java.util.HashSet; public class DependencyTracker { Map<String, Set<String>> theDependencies = new HashMap<>(); public Set<String> getDependencies(String dependencyName) { Set<String> allDependencies = new HashSet<>(); Set<String> directDependencies = getDirectDependencies(dependencyName); allDependencies.addAll(directDependencies); for (String subDependency : directDependencies) { allDependencies.addAll(getDependencies(subDependency)); } return allDependencies; } public void registerDependency(String dependent, String dependedUpon) { Set<String> currentDependencies = getDirectDependencies(dependent); currentDependencies.add(dependedUpon); theDependencies.put(dependent, currentDependencies); } private Set<String> getDirectDependencies(String dependencyName) { return theDependencies.getOrDefault(dependencyName, new HashSet<>()); } }
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.HashSet;
- public class DependencyTracker {
- Map<String, Set<String>> theDependencies = new HashMap<>();
- public Set<String> getDependencies(String dependencyName) {
return getDirectDependencies(dependencyName);- Set<String> allDependencies = new HashSet<>();
- Set<String> directDependencies = getDirectDependencies(dependencyName);
- allDependencies.addAll(directDependencies);
- for (String subDependency : directDependencies) {
- allDependencies.addAll(getDependencies(subDependency));
- }
- return allDependencies;
- }
// If "a" depends on "b," "a" is `dependent` and "b" is dependendUpon- public void registerDependency(String dependent, String dependedUpon) {
- Set<String> currentDependencies = getDirectDependencies(dependent);
- currentDependencies.add(dependedUpon);
- theDependencies.put(dependent, currentDependencies);
- }
- private Set<String> getDirectDependencies(String dependencyName) {
- return theDependencies.getOrDefault(dependencyName, new HashSet<>());
- }
- }
import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; import static org.junit.jupiter.api.Assertions.assertEquals; class DependencyTrackerTest { private DependencyTracker tracker; @BeforeEach void setUp() { tracker = new DependencyTracker(); } @Test void noDependencies() { Set<String> dependencies = tracker.getDependencies("H"); assertEquals(Set.of(), dependencies); } @Test void oneDependency() { tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("G"); assertEquals(Set.of("H"), dependencies); } @Test void noDependenciesWhenOtherDependency() { tracker.registerDependency("G","H"); Set<String> dependencies = tracker.getDependencies("K"); assertEquals(Set.of(), dependencies); } @Test void twoDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("A","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } @Test void oneTransitiveDependency() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C"), dependencies); } @Test void twoTransitiveDependencies() { tracker.registerDependency("A","B"); tracker.registerDependency("B","C"); tracker.registerDependency("C","D"); Set<String> dependencies = tracker.getDependencies("A"); assertEquals(Set.of("B", "C", "D"), dependencies); } }
- import java.util.Set;
- import org.junit.jupiter.api.Test;
- import org.junit.jupiter.api.BeforeEach;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class DependencyTrackerTest {
- private DependencyTracker tracker;
- @BeforeEach
- void setUp() {
- tracker = new DependencyTracker();
- }
- @Test
- void noDependencies() {
- Set<String> dependencies = tracker.getDependencies("H");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void oneDependency() {
- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("G");
- assertEquals(Set.of("H"), dependencies);
- }
- @Test
- void noDependenciesWhenOtherDependency() {
- tracker.registerDependency("G","H");
- Set<String> dependencies = tracker.getDependencies("K");
- assertEquals(Set.of(), dependencies);
- }
- @Test
- void twoDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("A","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- @Test
- void oneTransitiveDependency() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C"), dependencies);
- }
- @Test
- void twoTransitiveDependencies() {
- tracker.registerDependency("A","B");
- tracker.registerDependency("B","C");
- tracker.registerDependency("C","D");
- Set<String> dependencies = tracker.getDependencies("A");
- assertEquals(Set.of("B", "C", "D"), dependencies);
- }
- }
fn find_the_magick_mushroom(forest: [[char;10];10]) -> Option<(usize, usize)> { for (i, row) in forest.iter().enumerate() { for (j, &plant) in row.iter().enumerate() { if plant == 'π' { return Some((i, j)) } } } None }
from typing import Anydef find_the_magick_mushroom(forest: list) -> Any:for t, tree in enumerate(forest):for m, mushroom in enumerate(tree):if mushroom == 'π':return t, mreturn -1- fn find_the_magick_mushroom(forest: [[char;10];10]) -> Option<(usize, usize)> {
- for (i, row) in forest.iter().enumerate() {
- for (j, &plant) in row.iter().enumerate() {
- if plant == 'π' {
- return Some((i, j))
- }
- }
- }
- None
- }
#[cfg(test)] mod tests { use super::*; use rand::prelude::*; #[test] fn test_some() { let mut rng = thread_rng(); for _ in 0..100 { let mut forest: [[char;10];10] = [['π²';10];10]; let i = rng.gen_range(0..10); let j = rng.gen_range(0..10); forest[i][j] = 'π'; assert_eq!(find_the_magick_mushroom(forest), Some((i, j))); } } #[test] fn test_none() { let forest: [[char;10];10] = [['π²';10];10]; assert_eq!(find_the_magick_mushroom(forest), None); } }
import codewars_test as testfrom solution import find_the_magick_mushroomimport random@test.describe("Example")def test_group():@test.it("test case 1: find mushroom coordinates")def test_case():for n in range(101):forest = [['π²' for i in range(10)] for j in range(10)]i = random.randint(0,9)j = random.randint(0,9)forest[i][j] = 'π'test.assert_equals(find_the_magick_mushroom(forest), (i, j))@test.it("test case 2: No mushroom found")def test_case():forest = [['π²' for i in range(10)] for j in range(10)]test.assert_equals(find_the_magick_mushroom(forest), (-1))- #[cfg(test)]
- mod tests {
- use super::*;
- use rand::prelude::*;
- #[test]
- fn test_some() {
- let mut rng = thread_rng();
- for _ in 0..100 {
- let mut forest: [[char;10];10] = [['π²';10];10];
- let i = rng.gen_range(0..10);
- let j = rng.gen_range(0..10);
- forest[i][j] = 'π';
- assert_eq!(find_the_magick_mushroom(forest), Some((i, j)));
- }
- }
- #[test]
- fn test_none() {
- let forest: [[char;10];10] = [['π²';10];10];
- assert_eq!(find_the_magick_mushroom(forest), None);
- }
- }
public static class Kata { public static int SameCase(char a, char b) { if (!IsLetter(a) || !IsLetter(b)) { return -1; } return IsUpper(a) == IsUpper(b) ? 1 : 0; } private static bool IsLetter(char c) { return (uint)(c - 'A') <= ('Z' - 'A') || (uint)(c - 'a') <= ('z' - 'a'); } private static bool IsUpper(char c) { return (uint)(c - 'A') <= ('Z' - 'A'); } }
public static class Kata- public static class Kata
- {
public static int SameCase(char a, char b) =>!char.IsLetter(a) || !char.IsLetter(b)? -1: char.IsUpper(a) == char.IsUpper(b)? 1 : 0;}- public static int SameCase(char a, char b)
- {
- if (!IsLetter(a) || !IsLetter(b))
- {
- return -1;
- }
- return IsUpper(a) == IsUpper(b) ? 1 : 0;
- }
- private static bool IsLetter(char c)
- {
- return (uint)(c - 'A') <= ('Z' - 'A') || (uint)(c - 'a') <= ('z' - 'a');
- }
- private static bool IsUpper(char c)
- {
- return (uint)(c - 'A') <= ('Z' - 'A');
- }
- }
fn balanced_parenthesis(s: &str) -> String { if nesting_level(s) == Some(0) { "Balanced" } else { "Unbalanced" }.into() } fn nesting_level(s: &str) -> Option<u32> { let mut level: u32 = 0; for c in s.chars() { level = if c == '(' { level + 1 } else { level.checked_sub(1)? } } Some(level) }
fn balanced_parenthesis(s: &str) -> String {let balanced = s.chars().fold(Some(0u32),|acc, c|match c {'(' => acc.map(|n| n + 1),')' => acc.and_then(|n| n.checked_sub(1)),_ => acc}) == Some(0);if balanced {- fn balanced_parenthesis(s: &str) -> String {
- if nesting_level(s) == Some(0) {
- "Balanced"
- } else {
- "Unbalanced"
- }.into()
- }
- fn nesting_level(s: &str) -> Option<u32> {
- let mut level: u32 = 0;
- for c in s.chars() {
- level = if c == '(' {
- level + 1
- } else {
- level.checked_sub(1)?
- }
- }
- Some(level)
- }
The function calendar_func() takes input of two people's schedules of the day in the form of range slices. (Eg. &[0900..1300, 1440..1540, 1820..1910] denoting the person is available from 9am-1pm, 2:40-3:40pm, 6:20-7:10pm) It finds the overlaps in their schedules and outputs at most three blocks of free time that they both have in common.
Assume given ranges are valid times (Ie. no 0990 or 3600).
use std::ops::Range; fn calendar_func<Idx: Ord + Copy>(person_a: &[Range<Idx>], person_b: &[Range<Idx>]) -> Vec<Range<Idx>> { let mut iter_a = person_a.iter(); let mut iter_b = person_b.iter(); let mut overlaps = Vec::new(); let mut item_a = iter_a.next(); let mut item_b = iter_b.next(); while let (Some(a), Some(b)) = (item_a, item_b) { match range_cmp(a, b) { RO::Less => item_a = iter_a.next(), RO::Greater => item_b = iter_b.next(), RO::Overlap(overlap) => { overlaps.push(overlap); if overlaps.len() == 3 { break; } item_a = iter_a.next(); item_b = iter_b.next(); } } } overlaps } fn range_cmp<Idx: Ord + Copy>(a: &Range<Idx>, b: &Range<Idx>) -> RangeOrdering<Idx> { if a.end <= b.start { RO::Less } else if b.end <= a.start { RO::Greater } else { RO::Overlap( (a.start.max(b.start))..(a.end.min(b.end)) ) } } use RangeOrdering as RO; enum RangeOrdering<Idx> { Less, Greater, Overlap(Range<Idx>) }
import java.util.*;public class Main<Integer> implements CalendarFunc<Integer> {ArrayList<Integer> PersonA = new ArrayList<Integer>();ArrayList<Integer> PersonB = new ArrayList<Integer>();ArrayList<Integer> BusyTime = new ArrayList<Integer>();ArrayList<Integer> FreeTime = new ArrayList<Integer>();public Main(ArrayList<Integer> PersonA, ArrayList<Integer> PersonB) {this.PersonA = PersonA;this.PersonB = PersonB;}@Overridepublic void removeSleepTime() {// TODO removes sleeping blocks in FreeTime ArrayList}@Overridepublic void findUnion() {// TODO return BusyTime, which is a union of all busy blocks}}- use std::ops::Range;
- fn calendar_func<Idx: Ord + Copy>(person_a: &[Range<Idx>], person_b: &[Range<Idx>]) -> Vec<Range<Idx>> {
- let mut iter_a = person_a.iter();
- let mut iter_b = person_b.iter();
- let mut overlaps = Vec::new();
- let mut item_a = iter_a.next();
- let mut item_b = iter_b.next();
- while let (Some(a), Some(b)) = (item_a, item_b) {
- match range_cmp(a, b) {
- RO::Less => item_a = iter_a.next(),
- RO::Greater => item_b = iter_b.next(),
- RO::Overlap(overlap) => {
- overlaps.push(overlap);
- if overlaps.len() == 3 {
- break;
- }
- item_a = iter_a.next();
- item_b = iter_b.next();
- }
- }
- }
- overlaps
- }
- fn range_cmp<Idx: Ord + Copy>(a: &Range<Idx>, b: &Range<Idx>) -> RangeOrdering<Idx> {
- if a.end <= b.start {
- RO::Less
- } else if b.end <= a.start {
- RO::Greater
- } else {
- RO::Overlap(
- (a.start.max(b.start))..(a.end.min(b.end))
- )
- }
- }
- use RangeOrdering as RO;
- enum RangeOrdering<Idx> {
- Less,
- Greater,
- Overlap(Range<Idx>)
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_surplus() { assert_eq!( calendar_func( &[0900..1300, 1315..1350, 1440..1540, 1600..1630, 1820..1910], &[0900..1300, 1350..1410, 1440..1540, 1600..1630, 1820..1910], ), &[0900..1300, 1440..1540, 1600..1630], ); } #[test] fn test_non_surplus() { assert_eq!( calendar_func( &[0900..1300, 1615..1630, 1820..1910], &[0900..1300, 1600..1615, 1820..1910], ), &[0900..1300, 1820..1910], ); } #[test] fn test_none() { assert_eq!( calendar_func( &[0900..1300, 1615..1630, 1820..1910], &[1300..1400, 1600..1615, 2000..2130], ), &[], ); } }
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;// TODO: Replace examples and use TDD development by writing your own testspublic class SolutionTest {@Test@SuppressWarnings("unchecked")public static<Integer> void main(String[] args) {//TODO takes in schedule blocks of Person A and B, make them into two arrayListsScanner s = new Scanner(System.in);ArrayList<Integer> PerA = new ArrayList<Integer>();ArrayList<Integer> PerB = new ArrayList<Integer>();int count1 = 1;int count2 = 1;while(s.hasNext()){System.out.println("Enter first person's schedule.");System.out.println("Busy Block #"+ count1);System.out.println("Start time "+ "(Use military time, eg.0930): ");Integer a1 = (Integer) s.nextLine();System.out.println(a1);System.out.println("End time "+ "(Use military time, eg.1330): ");Integer a2 = (Integer) s.nextLine();System.out.println(a2);PerA.add(a1);PerA.add(a2);count1++;}while(s.hasNext()){System.out.println("Enter second person's schedule.");System.out.println("Busy Block #"+ count2);System.out.println("Start time "+ "(Use military time, eg.0930): ");Integer b1 = (Integer) s.nextLine();System.out.println(b1);System.out.println("End time "+ "(Use military time, eg.1330): ");Integer b2 = (Integer) s.nextLine();System.out.println(b2);PerB.add(b1);PerB.add(b2);count2++;}Main<Integer> c = new Main<Integer>(PerA, PerB);c.findUnion();c.removeSleepTime();System.out.println(c.FreeTime);}- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn test_surplus() {
- assert_eq!(
- calendar_func(
- &[0900..1300, 1315..1350, 1440..1540, 1600..1630, 1820..1910],
- &[0900..1300, 1350..1410, 1440..1540, 1600..1630, 1820..1910],
- ),
- &[0900..1300, 1440..1540, 1600..1630],
- );
- }
- #[test]
- fn test_non_surplus() {
- assert_eq!(
- calendar_func(
- &[0900..1300, 1615..1630, 1820..1910],
- &[0900..1300, 1600..1615, 1820..1910],
- ),
- &[0900..1300, 1820..1910],
- );
- }
- #[test]
- fn test_none() {
- assert_eq!(
- calendar_func(
- &[0900..1300, 1615..1630, 1820..1910],
- &[1300..1400, 1600..1615, 2000..2130],
- ),
- &[],
- );
- }
- }
For provided string return the reversed string.
Examples:
1)
Input:
abc
Output:
cba
Input:
123
Output:
321
Input:
Hello World!
Output:
!dlroW olleH
const reverseStr = s => [...s].reverse().join("") console.log(reverseStr("Lorem ipsum dolor sit amet!"))
const reverseStr = str => Array.from(str).reverse().join("")- const reverseStr = s => [...s].reverse().join("")
console.log(reverseStr('hello world'));- console.log(reverseStr("Lorem ipsum dolor sit amet!"))
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("should test for something", function() { assert.strictEqual(reverseStr('abc'), 'cba'); assert.strictEqual(reverseStr('123'), '321'); assert.strictEqual(reverseStr('a1b2c3'), '3c2b1a'); assert.strictEqual(reverseStr('Hello World!'), '!dlroW olleH'); assert.strictEqual(reverseStr('Lorem ipsum dolor sit amet!'), '!tema tis rolod muspi meroL'); }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("should test for something", function() {
- assert.strictEqual(reverseStr('abc'), 'cba');
- assert.strictEqual(reverseStr('123'), '321');
- assert.strictEqual(reverseStr('a1b2c3'), '3c2b1a');
- assert.strictEqual(reverseStr('Hello World!'), '!dlroW olleH');
- assert.strictEqual(reverseStr('Lorem ipsum dolor sit amet!'), '!tema tis rolod muspi meroL');
- });
- });