Hi. I have a kata suggestion.
It goes as followed:
Write a function that takes a parameter n for example. Then declare a variable k = 2 ^ n.
Create an empty array with length k - 1. In each step place redeclare k and put it in the middle of every array/sub-array.
Then place the value of k in the middle of left and right sub-arrays until you get this:
example:
n = 1: k = 2, array length = 1, array : 1;
n = 2: k = 4, array length = 3, array : 1,2,1;
n = 3: k = 8, array length = 7, array : 1,2,1,4,1,2,1;
n = 4: k = 16, array length = 15, array : 1,2,1,4,1,2,1,8,1,2,1,4,1,2,1;
import java.util.Arrays; public class PowerfulArrays { public static int[] powerfulArray(int n) { if (n == 0) { return new int[0]; } else { // if n > 0 int[] subArray = powerfulArray(n-1); // recursively get subArray for n-1 int k = (int) Math.pow(2, n); int[] result = new int[k - 1]; // create new int array of size k-1 System.arraycopy(subArray, 0, result, 0, subArray.length); // copy subArray on left side result[subArray.length] = k / 2; // put k / 2 in middle System.arraycopy(subArray, 0, result, subArray.length + 1, subArray.length); // copy subArray on right side return result; } } }
function powerfulArray(n) {if ( n ) {const t = powerfulArray(n-1);return [ ...t, 2**(n-1), ...t ];} elsereturn [];- import java.util.Arrays;
- public class PowerfulArrays {
- public static int[] powerfulArray(int n) {
- if (n == 0) {
- return new int[0];
- } else {
- // if n > 0
- int[] subArray = powerfulArray(n-1); // recursively get subArray for n-1
- int k = (int) Math.pow(2, n);
- int[] result = new int[k - 1]; // create new int array of size k-1
- System.arraycopy(subArray, 0, result, 0, subArray.length); // copy subArray on left side
- result[subArray.length] = k / 2; // put k / 2 in middle
- System.arraycopy(subArray, 0, result, subArray.length + 1, subArray.length); // copy subArray on right side
- return result;
- }
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertArrayEquals; class SolutionTest { @Test void testPowerfulArray() { assertArrayEquals(new int[0], PowerfulArrays.powerfulArray(0)); assertArrayEquals(new int[]{1}, PowerfulArrays.powerfulArray(1)); assertArrayEquals(new int[]{1, 2, 1}, PowerfulArrays.powerfulArray(2)); assertArrayEquals(new int[]{1, 2, 1, 4, 1, 2, 1}, PowerfulArrays.powerfulArray(3)); assertArrayEquals(new int[]{1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1}, PowerfulArrays.powerfulArray(4)); assertArrayEquals(new int[]{1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1}, PowerfulArrays.powerfulArray(5)); } }
const chai = require("chai");const assert = chai.assert;chai.config.truncateThreshold = 0;- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertArrayEquals;
describe("Solution", function() {it("should test for something", function() {assert.deepEqual(powerfulArray(0), [])assert.deepEqual(powerfulArray(1), [1])assert.deepEqual(powerfulArray(2), [1, 2, 1])assert.deepEqual(powerfulArray(3), [1, 2, 1, 4, 1, 2, 1])assert.deepEqual(powerfulArray(4), [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1])assert.deepEqual(powerfulArray(5), [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1])});});- class SolutionTest {
- @Test
- void testPowerfulArray() {
- assertArrayEquals(new int[0], PowerfulArrays.powerfulArray(0));
- assertArrayEquals(new int[]{1}, PowerfulArrays.powerfulArray(1));
- assertArrayEquals(new int[]{1, 2, 1}, PowerfulArrays.powerfulArray(2));
- assertArrayEquals(new int[]{1, 2, 1, 4, 1, 2, 1}, PowerfulArrays.powerfulArray(3));
- assertArrayEquals(new int[]{1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1}, PowerfulArrays.powerfulArray(4));
- assertArrayEquals(new int[]{1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1}, PowerfulArrays.powerfulArray(5));
- }
- }
Amir Hamedi has suggested a kata on Discord.
Hi. I have a kata suggestion.
It goes as followed:
Write a function that takes a parameter n for example. Then declare a variable k = 2 ^ n.
Create an empty array with length k - 1. In each step place redeclare k and put it in the middle of every array/sub-array.
Then place the value of k in the middle of left and right sub-arrays until you get this:
example:
n = 1: k = 2, array length = 1, array : 1;
n = 2: k = 4, array length = 3, array : 1,2,1;
n = 3: k = 8, array length = 7, array : 1,2,1,4,1,2,1;
n = 4: k = 16, array length = 15, array : 1,2,1,4,1,2,1,8,1,2,1,4,1,2,1;
As you can see this generates a sequence 1,2,1,4,1,2,1...
filled with values which are powers of 2, which could continue growing as we increase the value of n
.
Instead of asking you to generate the array for a given value of n
, this kata is going to ask you what the value will be at position i
assuming you had generated a sequence long enough to include the position.
assert.equal(powerfulValueAt(0), 1)
assert.equal(powerfulValueAt(1), 2)
assert.equal(powerfulValueAt(2), 1)
assert.equal(powerfulValueAt(3), 4)
assert.equal(powerfulValueAt(7), 8)
assert.equal(powerfulValueAt(11), 4)
assert.equal(powerfulValueAt(511), 512)
assert.equal(powerfulValueAt(991), 32)
function powerfulValueAt(i) { let binaryString = (i+1).toString(2); // convert i to binary let binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10 return parseInt(binaryResult, 2); // convert back to base10 and yeild }
// split out previous solution so I could better understand itfunction* powerfulGen(n) {let i = 0;while (++i<n) {let binaryString = i.toString(2); // convert i to binarylet binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10yield parseInt(binaryResult, 2); // convert back to base10 and yeild}}const powerfulArray = n => [...powerfulGen(2**n)];- function powerfulValueAt(i) {
- let binaryString = (i+1).toString(2); // convert i to binary
- let binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10
- return parseInt(binaryResult, 2); // convert back to base10 and yeild
- }
const chai = require("chai"); const assert = chai.assert; chai.config.truncateThreshold = 0; describe("Sample Tests", function() { it("Check example values", function() { assert.equal(powerfulValueAt(0), 1) assert.equal(powerfulValueAt(1), 2) assert.equal(powerfulValueAt(2), 1) assert.equal(powerfulValueAt(3), 4) assert.equal(powerfulValueAt(7), 8) assert.equal(powerfulValueAt(11), 4) assert.equal(powerfulValueAt(511), 512) assert.equal(powerfulValueAt(991), 32) }); }); describe("Fixed Tests", function() { it("Check firt 100 values", function() { let first100 = [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32, 1, 2, 1, 4]; for(let i = 0; i < 100; i++){ assert.equal(powerfulValueAt(i), first100[i]) } }); it("Check millionth value", function() { assert.equal(powerfulValueAt(1_000_000), 1) }); it("Check 917503th value", function() { assert.equal(powerfulValueAt(917503), 131072) }) }); describe("Randomised Tests", function() { const RANDOM_POSITION_LIMIT = 1_000_000; for(let i = 0; i<100; i++) { let position = Math.floor(Math.random() * RANDOM_POSITION_LIMIT); it(`Check ${position}`, function() { assert.equal(powerfulValueAt(position), solution(position)); }) } function solution(i) { let binaryString = (i+1).toString(2); // convert i to binary let binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10 return parseInt(binaryResult, 2); // convert back to base10 and yeild } });
- const chai = require("chai");
- const assert = chai.assert;
- chai.config.truncateThreshold = 0;
describe("Solution", function() {it("should test for something", function() {assert.deepEqual(powerfulArray(0), [])assert.deepEqual(powerfulArray(1), [1])assert.deepEqual(powerfulArray(2), [1, 2, 1])assert.deepEqual(powerfulArray(3), [1, 2, 1, 4, 1, 2, 1])assert.deepEqual(powerfulArray(4), [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1])assert.deepEqual(powerfulArray(5), [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1])- describe("Sample Tests", function() {
- it("Check example values", function() {
- assert.equal(powerfulValueAt(0), 1)
- assert.equal(powerfulValueAt(1), 2)
- assert.equal(powerfulValueAt(2), 1)
- assert.equal(powerfulValueAt(3), 4)
- assert.equal(powerfulValueAt(7), 8)
- assert.equal(powerfulValueAt(11), 4)
- assert.equal(powerfulValueAt(511), 512)
- assert.equal(powerfulValueAt(991), 32)
- });
- });
- describe("Fixed Tests", function() {
- it("Check firt 100 values", function() {
- let first100 = [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 64, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 32, 1, 2, 1, 4];
- for(let i = 0; i < 100; i++){
- assert.equal(powerfulValueAt(i), first100[i])
- }
- });
- it("Check millionth value", function() {
- assert.equal(powerfulValueAt(1_000_000), 1)
- });
- it("Check 917503th value", function() {
- assert.equal(powerfulValueAt(917503), 131072)
- })
- });
- describe("Randomised Tests", function() {
- const RANDOM_POSITION_LIMIT = 1_000_000;
- for(let i = 0; i<100; i++) {
- let position = Math.floor(Math.random() * RANDOM_POSITION_LIMIT);
- it(`Check ${position}`, function() {
- assert.equal(powerfulValueAt(position), solution(position));
- })
- }
- function solution(i) {
- let binaryString = (i+1).toString(2); // convert i to binary
- let binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10
- return parseInt(binaryResult, 2); // convert back to base10 and yeild
- }
- });
// split out previous solution so I could better understand it function* powerfulGen(n) { let i = 0; while (++i<n) { let binaryString = i.toString(2); // convert i to binary let binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10 yield parseInt(binaryResult, 2); // convert back to base10 and yeild } } const powerfulArray = n => [...powerfulGen(2**n)];
- // split out previous solution so I could better understand it
- function* powerfulGen(n) {
- let i = 0;
while (++i<n) yield parseInt(i.toString(2).match(/10*$/), 2);- while (++i<n) {
- let binaryString = i.toString(2); // convert i to binary
- let binaryResult = binaryString.substring(binaryString.lastIndexOf("1")); // Only want from last index of 1, eg 1010 => 10
- yield parseInt(binaryResult, 2); // convert back to base10 and yeild
- }
- }
- const powerfulArray = n => [...powerfulGen(2**n)];
Suggested by grim reaper on Discord.
Hi. I have a kata suggestion. I have not been able to solve it, but it would be an exciting kata.
It goes as followed:
Write a function that takes a parameter n for example. Then declare a variable k = 2 ^ n.
Create an empty array with length k - 1. In each step place redeclare k and put it in the middle of every array/sub-array.
Then place the value of k in the middle of left and right sub-arrays until you get this:
example:
n = 1: k = 2, array length = 1, array : 1;
n = 2: k = 4, array length = 3, array : 1,2,1;
n = 3: k = 8, array length = 7, array : 1,2,1,4,1,2,1;
n = 4: k = 16, array length = 15, array : 1,2,1,4,1,2,1,8,1,2,1,4,1,2,1;
function powerfulArray(n) {
if(n === 0) {
return [];
}
let k = Math.pow(2, n);
let result = Array(k-1).fill(1);
for(let i = 2; i < k; i *= 2) {
for(let j = i-1; j<result.length; j += i) {
result[j] = i;
}
}
return result;
}
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold = 0;
describe("Solution", function() {
it("should test for something", function() {
assert.deepEqual(powerfulArray(0), [])
assert.deepEqual(powerfulArray(1), [1])
assert.deepEqual(powerfulArray(2), [1, 2, 1])
assert.deepEqual(powerfulArray(3), [1, 2, 1, 4, 1, 2, 1])
assert.deepEqual(powerfulArray(4), [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1])
assert.deepEqual(powerfulArray(5), [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1])
});
});