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.stream.IntStream; public class Area { public static long[] workOutArea(long[] values){ if (values.length % 2 != 0) { return null; } return IntStream.range(0, values.length / 2).mapToLong(i -> values[i * 2] * values[i * 2 + 1]).toArray(); } }
- import java.util.stream.IntStream;
- public class Area {
- public static long[] workOutArea(long[] values){
- if (values.length % 2 != 0) {
- return null;
- }
- return IntStream.range(0, values.length / 2).mapToLong(i -> values[i * 2] * values[i * 2 + 1]).toArray();
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertArrayEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testNothing() { assertArrayEquals(new long[0], Area.workOutArea(new long[0])); } @Test public void testSomething() { long[] val1 = {4l,4l,5l,5l,6l,6l,7l,7l}; assertArrayEquals(Area.workOutArea(val1), new long[] {16l, 25l, 36l, 49l}); long[] val2 = {4l,4l,5l,5l,6l,6l,7l}; assertEquals(null, Area.workOutArea(val2)); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertArrayEquals;
- import org.junit.runners.JUnit4;
- public class SolutionTest {
- @Test
- public void testNothing() {
- assertArrayEquals(new long[0], Area.workOutArea(new long[0]));
- }
- @Test
- public void testSomething() {
- long[] val1 = {4l,4l,5l,5l,6l,6l,7l,7l};
Assert.assertArrayEquals(area.workOutArea(val1), new long[] {16l, 25l, 36l, 49l});- assertArrayEquals(Area.workOutArea(val1), new long[] {16l, 25l, 36l, 49l});
- long[] val2 = {4l,4l,5l,5l,6l,6l,7l};
assertEquals(null, area.workOutArea(val2));- assertEquals(null, Area.workOutArea(val2));
- }
- }
function wordCount(str) { return str.replace(/\s/g, '').length ? str.trim().split(/\s+/).length : 0; }
- function wordCount(str) {
var words = 1;for(var i = 1; i < str.length; i++) {if(str[i] == " " && str[i - 1] !== " "&& str[i + 1] !== " ") {words++;}}return words;}- return str.replace(/\s/g, '').length ? str.trim().split(/\s+/).length : 0;
- }
package com.mystuff.juststuff; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.Arrays; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Palindrome { private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy"); public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) { final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate)); return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear()) .mapToObj(Palindrome::createPalindrome) .filter(isDateInRange(sortedDates.first(), sortedDates.last())) .collect(Collectors.toCollection(TreeSet::new)); } private static LocalDate createPalindrome(final int year) { final String yearStr = String.valueOf(year); final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString(); try { return LocalDate.parse(datePalindrome, formatter); } catch (final DateTimeParseException e) {} return null; } private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) { return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate)); } }
- package com.mystuff.juststuff;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.time.format.DateTimeParseException;
- import java.util.Arrays;
- import java.util.Set;
- import java.util.SortedSet;
- import java.util.TreeSet;
- import java.util.function.Predicate;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- public class Palindrome {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");- private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMddyyyy");
public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {return IntStream.range(startDate.getYear(), endDate.getYear() + 1).mapToObj(Palindrome::createPalindrome).filter(date -> isValidDate(date, startDate, endDate)).collect(Collectors.toCollection(TreeSet::new));}private static LocalDate createPalindrome(int year) {String yearStr = String.valueOf(year);String datePalindrome = new StringBuilder().append(yearStr).reverse().append(yearStr).toString();try {return LocalDate.parse(datePalindrome, formatter);} catch (DateTimeParseException e) {return null;}}private static boolean isValidDate(final LocalDate date, LocalDate startDate, LocalDate endDate) {if (date == null)return false;if (startDate.isEqual(date) || endDate.isEqual(date))return true;return startDate.isBefore(date) && endDate.isAfter(date) ||endDate.isBefore(date) && startDate.isAfter(date);}- public Set<LocalDate> countDatePalindromes(final LocalDate startDate, final LocalDate endDate) {
- final SortedSet<LocalDate> sortedDates = new TreeSet<>(Arrays.asList(startDate, endDate));
- return IntStream.rangeClosed(sortedDates.first().getYear(), sortedDates.last().getYear())
- .mapToObj(Palindrome::createPalindrome)
- .filter(isDateInRange(sortedDates.first(), sortedDates.last()))
- .collect(Collectors.toCollection(TreeSet::new));
- }
- private static LocalDate createPalindrome(final int year) {
- final String yearStr = String.valueOf(year);
- final String datePalindrome = new StringBuilder(yearStr).reverse().append(yearStr).toString();
- try {
- return LocalDate.parse(datePalindrome, formatter);
- } catch (final DateTimeParseException e) {}
- return null;
- }
- private static Predicate<LocalDate> isDateInRange(final LocalDate startDate, final LocalDate endDate) {
- return (date) -> !(date == null || date.isBefore(startDate) || date.isAfter(endDate));
- }
- }
package com.mystuff.juststuff; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.util.Set; import org.junit.Test; public class PalindromeTest { @Test public void testCountDatePalindromesEndDateBeforeStartDate() { final LocalDate startDate = LocalDate.of(2001, 12, 1); final LocalDate endDate = LocalDate.of(2001, 1, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertTrue("Set size should be greater than zero", palSet.size() > 0); } @Test public void testCountDatePalindromesStartDateBeforeEndDate() { final LocalDate startDate = LocalDate.of(2001, 1, 1); final LocalDate endDate = LocalDate.of(2001, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertTrue("Set size should be greater than zero", palSet.size() > 0); } @Test public void testCountDatePalindromesStartDateEqualsEndDateNoPalindromes() { final LocalDate startDate = LocalDate.of(2001, 12, 31); // Dec 31, 2001 is not a palindrome date final LocalDate endDate = LocalDate.of(2001, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("Set size should be zero", 0, palSet.size()); } @Test public void testCountDatePalindromesStartDateEqualsEndDateOnePalindrome() { final LocalDate startDate = LocalDate.of(2001, 10, 2); // Oct 2, 2001 is a palindrome date final LocalDate endDate = LocalDate.of(2001, 10, 2); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("Set size should be one", 1, palSet.size()); } @Test public void testCountDatePalindromesHugeDateRange() { final LocalDate startDate = LocalDate.of(1900, 1, 1); final LocalDate endDate = LocalDate.of(2001, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("There is 1 palindrom date between 1/1/1900 12/31/2001", 1, palSet.size()); } @Test public void testCountDatePalindromesMultipleValues() { final LocalDate startDate = LocalDate.of(1900, 1, 1); final LocalDate endDate = LocalDate.of(2010, 12, 31); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("There are 2 palindrom date between 1/1/1900 12/31/2010", 2, palSet.size()); } @Test public void testCountDatePalindromesMultipleValuesEndDateBeforeStartDate() { final LocalDate startDate = LocalDate.of(2010, 12, 31); final LocalDate endDate = LocalDate.of(1900, 1, 1); final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate); assertEquals("There are 2 palindrom date between 1/1/1900 12/31/2010", 2, palSet.size()); } }
- package com.mystuff.juststuff;
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertTrue;
- import java.time.LocalDate;
- import java.util.Set;
- import org.junit.Test;
- public class PalindromeTest {
- @Test
- public void testCountDatePalindromesEndDateBeforeStartDate() {
- final LocalDate startDate = LocalDate.of(2001, 12, 1);
- final LocalDate endDate = LocalDate.of(2001, 1, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertTrue("Set size should be greater than zero", palSet.size() > 0);
- }
- @Test
- public void testCountDatePalindromesStartDateBeforeEndDate() {
- final LocalDate startDate = LocalDate.of(2001, 1, 1);
- final LocalDate endDate = LocalDate.of(2001, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertTrue("Set size should be greater than zero", palSet.size() > 0);
- }
- @Test
- public void testCountDatePalindromesStartDateEqualsEndDateNoPalindromes() {
- final LocalDate startDate = LocalDate.of(2001, 12, 31); // Dec 31, 2001 is not a palindrome date
- final LocalDate endDate = LocalDate.of(2001, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertEquals("Set size should be zero", 0, palSet.size());
- }
- @Test
- public void testCountDatePalindromesStartDateEqualsEndDateOnePalindrome() {
- final LocalDate startDate = LocalDate.of(2001, 10, 2); // Oct 2, 2001 is a palindrome date
- final LocalDate endDate = LocalDate.of(2001, 10, 2);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertEquals("Set size should be one", 1, palSet.size());
- }
- @Test
- public void testCountDatePalindromesHugeDateRange() {
- final LocalDate startDate = LocalDate.of(1900, 1, 1);
- final LocalDate endDate = LocalDate.of(2001, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertEquals("There is 1 palindrom date between 1/1/1900 12/31/2001", 1, palSet.size());
- }
- @Test
- public void testCountDatePalindromesMultipleValues() {
- final LocalDate startDate = LocalDate.of(1900, 1, 1);
- final LocalDate endDate = LocalDate.of(2010, 12, 31);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertEquals("There are 2 palindrom date between 1/1/1900 12/31/2010", 2, palSet.size());
- }
- @Test
- public void testCountDatePalindromesMultipleValuesEndDateBeforeStartDate() {
- final LocalDate startDate = LocalDate.of(2010, 12, 31);
- final LocalDate endDate = LocalDate.of(1900, 1, 1);
- final Set<LocalDate> palSet = new Palindrome().countDatePalindromes(startDate, endDate);
- assertEquals("There are 2 palindrom date between 1/1/1900 12/31/2010", 2, palSet.size());
- }
- }
function nextGeneration(grid) { return grid.map((row, rowIndex) => { return row.map((cell, colIndex) => { if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) { let neighboursCount = 0; if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++; if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++; if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++; if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++; if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++; if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++; if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++; if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++; if (cell === 1) { if (neighboursCount === 2 || neighboursCount === 3 ) { return 1; } } else { if (neighboursCount === 3 ) { return 1; } } return 0; } return 0; }); }); }
- function nextGeneration(grid) {
return grid;- return grid.map((row, rowIndex) => {
- return row.map((cell, colIndex) => {
- if (rowIndex !== 0 && colIndex !== 0 && rowIndex < grid.length - 1 && colIndex < row.length - 1) {
- let neighboursCount = 0;
- if (grid[rowIndex][colIndex + 1] === 1) neighboursCount++;
- if (grid[rowIndex][colIndex - 1] === 1) neighboursCount++;
- if (grid[rowIndex + 1][colIndex] === 1) neighboursCount++;
- if (grid[rowIndex - 1][colIndex] === 1) neighboursCount++;
- if (grid[rowIndex + 1][colIndex + 1] === 1) neighboursCount++;
- if (grid[rowIndex + 1][colIndex - 1] === 1) neighboursCount++;
- if (grid[rowIndex - 1][colIndex + 1] === 1) neighboursCount++;
- if (grid[rowIndex - 1][colIndex - 1] === 1) neighboursCount++;
- if (cell === 1) {
- if (neighboursCount === 2 || neighboursCount === 3 ) {
- return 1;
- }
- } else {
- if (neighboursCount === 3 ) {
- return 1;
- }
- }
- return 0;
- }
- return 0;
- });
- });
- }
describe("Given empty grid", () => { it("when next generation, should return empty", () => { Test.assertDeepEquals(nextGeneration([]), []); }); }); describe("Given a single cell", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0], [0,1,0], [0,0,0], ]), [ [0,0,0], [0,0,0], [0,0,0], ]); }); }); describe("Given a cell with 1 neighbour at rows", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0], [0,1,1,0], [0,0,0,0], ]), [ [0,0,0,0], [0,0,0,0], [0,0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at rows", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at cols", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0], [0,1,0], [0,1,0], [0,1,0], [0,0,0], ]), [ [0,0,0], [0,0,0], [0,1,0], [0,0,0], [0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at \ (diagonal)", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,0,0], [0,0,1,0,0], [0,0,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 2 neighbours at / (diagonal)", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,0,0,1,0], [0,0,1,0,0], [0,1,0,0,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 4 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,1,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,1,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 5 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,0,1,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,1,1,1,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 6 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 7 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,1,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], ]); }); }); describe("Given a cell with 8 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,1,1,0], [0,1,1,1,0], [0,1,1,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], ]); }); }); describe("Given a die cell with 2 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a die cell with 3 neighbours", () => { it("when next generation, should live", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); }); describe("Given a die cell with 4 neighbours", () => { it("when next generation, should die", () => { Test.assertDeepEquals(nextGeneration([ [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], [0,1,0,1,0], [0,0,0,0,0], ]), [ [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], ]); }); });
- describe("Given empty grid", () => {
- it("when next generation, should return empty", () => {
assert.deepEqual(nextGeneration([]), []);- Test.assertDeepEquals(nextGeneration([]), []);
- });
- });
- describe("Given a single cell", () => {
- it("when next generation, should die", () => {
assert.deepEqual(nextGeneration([0,0,0,0,1,0,0,0,0,]), [0,0,0,0,0,0,0,0,0,- Test.assertDeepEquals(nextGeneration([
- [0,0,0],
- [0,1,0],
- [0,0,0],
- ]), [
- [0,0,0],
- [0,0,0],
- [0,0,0],
- ]);
- });
- });
- describe("Given a cell with 1 neighbour at rows", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0],
- [0,1,1,0],
- [0,0,0,0],
- ]), [
- [0,0,0,0],
- [0,0,0,0],
- [0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at rows", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at cols", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0],
- [0,1,0],
- [0,1,0],
- [0,1,0],
- [0,0,0],
- ]), [
- [0,0,0],
- [0,0,0],
- [0,1,0],
- [0,0,0],
- [0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at \ (diagonal)", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,1,0,0],
- [0,0,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 2 neighbours at / (diagonal)", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,0,0,1,0],
- [0,0,1,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 4 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,1,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,1,0,1,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 5 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,0,1,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 6 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,1,1,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 7 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,1,1,1,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a cell with 8 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,1,1,0],
- [0,1,1,1,0],
- [0,1,1,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a die cell with 2 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a die cell with 3 neighbours", () => {
- it("when next generation, should live", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,0,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,1,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
- describe("Given a die cell with 4 neighbours", () => {
- it("when next generation, should die", () => {
- Test.assertDeepEquals(nextGeneration([
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- [0,1,0,1,0],
- [0,0,0,0,0],
- ]), [
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- ]);
- });
- });
import java.util.*; class Solution { private int[] getNumDigits(int number) { int[] digits = new int[10]; while (number != 0) { digits[number % 10]++; number = number / 10; } return digits; } public int retSmallestPositiveInteger() { for (int num = 100; num < Integer.MAX_VALUE; num++) { boolean isEquals = true; int[] digits = getNumDigits(num); for (int i = 2; (i <= 6) && isEquals; i++) { isEquals = isEquals && Arrays.equals(digits, getNumDigits(num * i)); } if (isEquals) return num; } return 0; } }
- import java.util.*;
- class Solution {
public static int retSmallestPositiveInteger() {for(int i=1; ; i++) {if(hasSameDigits(i, i*2) && hasSameDigits(i, i*3) && hasSameDigits(i, i*4) && hasSameDigits(i, i*5) && hasSameDigits(i, i*6))return i;}}private static boolean hasSameDigits(int x, int y) {char[] xdigits = Integer.toString(x).toCharArray();char[] ydigits = Integer.toString(y).toCharArray();Arrays.sort(xdigits);Arrays.sort(ydigits);return Arrays.equals(xdigits, ydigits);}- private int[] getNumDigits(int number) {
- int[] digits = new int[10];
- while (number != 0) {
- digits[number % 10]++;
- number = number / 10;
- }
- return digits;
- }
- public int retSmallestPositiveInteger() {
- for (int num = 100; num < Integer.MAX_VALUE; num++) {
- boolean isEquals = true;
- int[] digits = getNumDigits(num);
- for (int i = 2; (i <= 6) && isEquals; i++) {
- isEquals = isEquals && Arrays.equals(digits, getNumDigits(num * i));
- }
- if (isEquals)
- return num;
- }
- return 0;
- }
- }
Find number of digits with an unrolled div loop.
// Unrolled div loop fn digits(mut n: u64) -> usize { let mut l = 1; loop { if n < 10 { return l; } if n < 100 { return l + 1; } if n < 1000 { return l + 2; } if n < 10000 { return l + 3; } n /= 10000; l += 4; } }
- // Unrolled div loop
- fn digits(mut n: u64) -> usize {
let mut l = 1;while n >= 10 {n /= 10;l += 1;- let mut l = 1;
- loop {
- if n < 10 {
- return l;
- }
l- if n < 100 {
- return l + 1;
- }
- if n < 1000 {
- return l + 2;
- }
- if n < 10000 {
- return l + 3;
- }
- n /= 10000;
- l += 4;
- }
- }
#[test] fn pow10() { assert_eq!(digits(0), 1); let mut n = 1; assert_eq!(digits(1), 1); for i in 1..20 { n*=10; assert_eq!(digits(n), i+1); } assert_eq!(digits(std::u64::MAX), 20); } #[test] fn pow10_minus_1() { let mut n = 1; for i in 1..20 { n*=10; assert_eq!(digits(n - 1), i); } } #[test] fn pow10_half() { let mut n = 1; for i in 1..20 { n*=10; assert_eq!(digits(n / 2), i); } }
- #[test]
- fn pow10() {
- assert_eq!(digits(0), 1);
for i in 0..20 {assert_eq!(digits(POW10[i]), i+1);- let mut n = 1;
- assert_eq!(digits(1), 1);
- for i in 1..20 {
- n*=10;
- assert_eq!(digits(n), i+1);
- }
- assert_eq!(digits(std::u64::MAX), 20);
- }
- #[test]
- fn pow10_minus_1() {
- let mut n = 1;
- for i in 1..20 {
assert_eq!(digits(POW10[i] - 1), i);- n*=10;
- assert_eq!(digits(n - 1), i);
- }
- }
- #[test]
- fn pow10_half() {
- let mut n = 1;
- for i in 1..20 {
assert_eq!(digits(POW10[i] / 2), i);- n*=10;
- assert_eq!(digits(n / 2), i);
- }
- }
#include <iostream> #include <string> using namespace std ; int helloCplusplus(){ string str = "Hello, C++!"; cout << str << '\n'; return 0; }
- #include <iostream>
- #include <string>
- using namespace std ;
- int helloCplusplus(){
std::string str = "Hello, C++!";std::cout << str << '';- string str = "Hello, C++!";
- cout << str << '
- ';
- return 0;
- }
# write a add function that doesn't use the plus symbol def add(a, b) a + b end # Make sure to view the test cases to see how this test fails
// write a add function that doesn't use the plus symbolfunction add(a, b){return a + b;}- # write a add function that doesn't use the plus symbol
- def add(a, b)
- a + b
- end
// Make sure to view the test cases to see how this test fails- # Make sure to view the test cases to see how this test fails
solution = File.open('/home/codewarrior/solution.txt', 'r').read describe "Check Solution" do it "should prevent the '+' symbol from being used anywhere in the code" do Test.expect(solution.exclude?('+'), "Your code isn't allowed to include the + symbol!") end end
const fs = require('fs');const solution = fs.readFileSync('/home/codewarrior/solution.txt', 'utf8');- solution = File.open('/home/codewarrior/solution.txt', 'r').read
describe("Check Solution", function(){it("should prevent the '+' symbol from being used anywhere in the code", function(){Test.expect(solution.indexOf('+') == -1, "Your code isn't allowed to include the + symbol!");});});- describe "Check Solution" do
- it "should prevent the '+' symbol from being used anywhere in the code" do
- Test.expect(solution.exclude?('+'), "Your code isn't allowed to include the + symbol!")
- end
- end
var getMin = function (list){ var list = (!list) ? 0 : list.filter(function(v){ return (v > 0 && v < Number.MAX_VALUE); }); return Math.min.apply(null,list) | 0; }
- var getMin = function (list){
var min = Number.MAX_VALUE;for (var i = 0; i < list.length; i++) {if (+list[i] <= 0) {continue;}min = Math.min(min, +list[i]);}return min = min === Number.MAX_VALUE ? 0 : min;- var list = (!list) ? 0 : list.filter(function(v){ return (v > 0 && v < Number.MAX_VALUE); });
- return Math.min.apply(null,list) | 0;
- }
describe("Solution", function(){ it("test1", function(){ var list = [0, 1, 1, 3]; Test.assertEquals(getMin(list), 1, "test fails"); }); it("test2", function(){ var list = [7, 2, null, 3, 0, 10]; Test.assertEquals(getMin(list), 2, "test fails"); }); it("test3", function(){ var list = [null, -2]; Test.assertEquals(getMin(list), 0, "test fails"); }); it("test4", function(){ var list = [5,null, -2, {a:"5"}, [1,2,3],3]; Test.assertEquals(getMin(list), 3, "test fails"); }); });
- describe("Solution", function(){
- it("test1", function(){
- var list = [0, 1, 1, 3];
- Test.assertEquals(getMin(list), 1, "test fails");
- });
- it("test2", function(){
- var list = [7, 2, null, 3, 0, 10];
- Test.assertEquals(getMin(list), 2, "test fails");
- });
- it("test3", function(){
- var list = [null, -2];
- Test.assertEquals(getMin(list), 0, "test fails");
- });
- it("test4", function(){
- var list = [5,null, -2, {a:"5"}, [1,2,3],3];
- Test.assertEquals(getMin(list), 3, "test fails");
- });
- });