Skip to main content
added 522 characters in body
Source Link
Bubbler
  • 77.5k
  • 5
  • 149
  • 465

K (ngn/k), 16 bytes

{(&*/'2\'!#x)_x}

Try it online!

A near-backport from my own J solution. Instead of checking each 1-based index has exactly one 1 bit, this checks if each 0-based index is all ones in binary. This works in K (2\0 is an empty array and its product is 1) but not in J (#:0 is 0 instead of an empty array).

K (ngn/k), 17 bytes

{(|1_(-2!)\#x)_x}

Try it online!

Similar to Jelly (x_y cuts y at the indices specified in x), but had to be careful to avoid generating large indices or indices in non-increasing order (which give domain error).

My initial approach used 2\ (integer into binary vector) and 2/ (binary vector into integer), but I realized that simply repeatedly dividing the length by 2 gives the correct cut indices.

{(|1_(-2!)\#x)_x}    Input: a string of length 2^n-1 (example: "abcdefg")
 (         #x)       length of x = 7
     (-2!)\          repeatedly floor-divide by 2 until convergence and collect:
                     (7 3 1 0)
   1_                remove head which is too large (3 1 0)
  |                  reverse (0 1 3)
              _x     cut x with those indices ("a"; "bc"; "defg")

K (ngn/k), 17 bytes

{(|1_(-2!)\#x)_x}

Try it online!

Similar to Jelly (x_y cuts y at the indices specified in x), but had to be careful to avoid generating large indices or indices in non-increasing order (which give domain error).

My initial approach used 2\ (integer into binary vector) and 2/ (binary vector into integer), but I realized that simply repeatedly dividing the length by 2 gives the correct cut indices.

{(|1_(-2!)\#x)_x}    Input: a string of length 2^n-1 (example: "abcdefg")
 (         #x)       length of x = 7
     (-2!)\          repeatedly floor-divide by 2 until convergence and collect:
                     (7 3 1 0)
   1_                remove head which is too large (3 1 0)
  |                  reverse (0 1 3)
              _x     cut x with those indices ("a"; "bc"; "defg")

K (ngn/k), 16 bytes

{(&*/'2\'!#x)_x}

Try it online!

A near-backport from my own J solution. Instead of checking each 1-based index has exactly one 1 bit, this checks if each 0-based index is all ones in binary. This works in K (2\0 is an empty array and its product is 1) but not in J (#:0 is 0 instead of an empty array).

K (ngn/k), 17 bytes

{(|1_(-2!)\#x)_x}

Try it online!

Similar to Jelly (x_y cuts y at the indices specified in x), but had to be careful to avoid generating large indices or indices in non-increasing order (which give domain error).

My initial approach used 2\ (integer into binary vector) and 2/ (binary vector into integer), but I realized that simply repeatedly dividing the length by 2 gives the correct cut indices.

{(|1_(-2!)\#x)_x}    Input: a string of length 2^n-1 (example: "abcdefg")
 (         #x)       length of x = 7
     (-2!)\          repeatedly floor-divide by 2 until convergence and collect:
                     (7 3 1 0)
   1_                remove head which is too large (3 1 0)
  |                  reverse (0 1 3)
              _x     cut x with those indices ("a"; "bc"; "defg")
Source Link
Bubbler
  • 77.5k
  • 5
  • 149
  • 465

K (ngn/k), 17 bytes

{(|1_(-2!)\#x)_x}

Try it online!

Similar to Jelly (x_y cuts y at the indices specified in x), but had to be careful to avoid generating large indices or indices in non-increasing order (which give domain error).

My initial approach used 2\ (integer into binary vector) and 2/ (binary vector into integer), but I realized that simply repeatedly dividing the length by 2 gives the correct cut indices.

{(|1_(-2!)\#x)_x}    Input: a string of length 2^n-1 (example: "abcdefg")
 (         #x)       length of x = 7
     (-2!)\          repeatedly floor-divide by 2 until convergence and collect:
                     (7 3 1 0)
   1_                remove head which is too large (3 1 0)
  |                  reverse (0 1 3)
              _x     cut x with those indices ("a"; "bc"; "defg")