DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #104 - Matrixify

Given an array with 50 indices, write a function that takes the array and a number n as input and builds a matrix with n columns.

Good luck!


This challenge comes from stanciudragosioan here on DEV. Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (6)

Collapse
 
aminnairi profile image
Amin

Haskell

splitEvery :: Int -> [a] -> [[a]]
splitEvery _ [] = []
splitEvery breakpoint list =
    first : splitEvery breakpoint rest
    where
        (first, rest) = splitAt breakpoint list

Playground

Hosted on Repl.it.

Collapse
 
qm3ster profile image
Mihail Malo

What should the output for n other than 1, 2, 5, 10, 25 and 50 be?

Collapse
 
stanciudragosioan profile image
StanciuDragosIoan

It should build n columns. Regardless if there is some remainder or not, the remainder should just be put in a column of itself (which will contain less elements compaired to the other).

Collapse
 
qm3ster profile image
Mihail Malo

You mean a row? A row where only m<n out of n columns are populated?

Thread Thread
 
stanciudragosioan profile image
StanciuDragosIoan

Hi, the function should build columns, say you have only 9 indexes and build 3 columns:

Say array is [0,1,2,3,4,5,6,7,8]

Your columns will be:

0 3 6
1 4 7
2 5 8

That's the output in the console, of course the 'columns' can be simple arrays, but the elements have to be pushed onto them like this.

Also, if instead of 9 items you have say 11 -> [0,1,2,3,4,5,6,7,8,9,10], your output will be similar but the last column will be incomplete:

0 3 6 9
1 4 7 10
2 5 8

Hope this clarifies it a bit.

Thanks for reaching out =).

Collapse
 
erezwanderman profile image
erezwanderman

f = (a, n) => [Array(n)]