Ad

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;

Code
Diff
  • 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 ];
    • } else
    • return [];
    • 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;
    • }
    • }
    • }

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)
Code
Diff
  • 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 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)];
    • 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
    • }
Code
Diff
  • // 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;
}