DEV Community

Discussion on: Daily Challenge #72 - Matrix Shift

Collapse
 
mbaas2 profile image
Michael Baas

I think the 2nd example is wrong...

And the solution in APL (Dyalog APL) is:

shift←{(⍴⍵)⍴(-⍺)⌽,⍵}

The left argument is the number of characters to shift and the right argument is the matrix. The approach (read from right to left) is as follows:

  • ravel (flatten) the argument (using monadic ,)
  • rotate the resulting vector by ⍺ characters
  • reshape the vector to the original shape of the argument

Test cases:

      1 shift 2 2⍴'hiok'
kh
io


      2 shift 3 4⍴'dudeimcoding'
ngdu
deim
codi

      1 shift 4 4⍴'abcd1234codeblah'
habc
d123
4cod
ebla
Collapse
 
aminnairi profile image
Amin

Yes indeed, the second example is wrong, I just noticed that when doing the unit tests!

Collapse
 
not_jffrydsr profile image
@nobody

pretty esoteric but interesting! 🥴🥴🥴