-7
\$\begingroup\$

This is a code-golf, so keep your code small!

The objective is to create a function that receives 3 arguments:

  • Rows(int): The max number of an item in each array;

  • Cols(int): The max number of dimensions (sub-arrays);

  • Val(*): The starting value in each item;

This function should return a multidimensional array with the proper size and with the items containing the value specified.

It should be able to return any number of dimensions.

Example:

var multiArray = createMultiArray(2, 4,'empty');

multiArray[0][0][0][0] === 'empty' //true
multiArray[0][0][0][1] === 'empty' //true
multiArray[0][0][0][2] === 'empty' //true
multiArray[0][0][1][0] === 'empty' //true
...
multiArray[2][2][2][2] === 'empty' //true
\$\endgroup\$
12
  • 3
    \$\begingroup\$ I'd recommend at the very least a different challenge type than a popularity-contest. I'm having a tough time thinking what would make a recursive array initialization exercise popular. code-golf would be better. \$\endgroup\$ Apr 4, 2014 at 14:23
  • 4
    \$\begingroup\$ This feels homework-y, and even if I'm wrong it's not a particularly interesting challenge. \$\endgroup\$ Apr 4, 2014 at 14:23
  • 3
    \$\begingroup\$ No reason to freak out. The point they are making is that this is quite a simple question which doesn't leave a lot of flexibility in the solutions (especially if you are prescribing recursive approachs), and hence it not very well suited for popularity contests. Also even if you leave it as a popularity contest, there is no incentive to keep the code small. So you may want to think about changing the winning criterion or lifting the recursion constraint to allow for creative approaches. \$\endgroup\$ Apr 4, 2014 at 14:35
  • 2
    \$\begingroup\$ @rafaelcastrocouto Please don't take it personally. Just look at these comments as recommendations for asking a better question, they are not personal attacks. I see this is your first question attempt -- that makes you braver than I, and kudos for that, but there's no shame in taking the suggestions you have been giving, acting on them, and coming up with a better question. \$\endgroup\$ Apr 4, 2014 at 14:39
  • 3
    \$\begingroup\$ "Informative" is subjective. If you want useful, clearly written ways to do things, a general QA site might be more appropriate than a puzzle site. \$\endgroup\$
    – Geobits
    Apr 4, 2014 at 14:45

7 Answers 7

8
\$\begingroup\$

GolfScript

This is pretty trivial, so there's no need to over-complicate it:

{[1$({2$@(@A}{\;}if]*}:A;

Online demo

\$\endgroup\$
5
  • 2
    \$\begingroup\$ @ProgramFOX, until they add syntax highlighting for GolfScript, I think it's best to leave it unhighlit. I don't know any other language with the same tokens. \$\endgroup\$ Apr 4, 2014 at 14:53
  • \$\begingroup\$ @ProgramFOX You can even see in this short example that the @ is coloured differently in two places as is the A. \$\endgroup\$
    – Howard
    Apr 4, 2014 at 14:57
  • \$\begingroup\$ Ok, I'll keep it in mind. \$\endgroup\$
    – ProgramFOX
    Apr 4, 2014 at 14:57
  • \$\begingroup\$ Where is the byte count? \$\endgroup\$
    – Adalynn
    Jul 31, 2017 at 22:43
  • \$\begingroup\$ @Zacharý, when I wrote the answer it wasn't golf. \$\endgroup\$ Aug 1, 2017 at 7:22
6
\$\begingroup\$

GolfScript, 17 characters

A non-recursive approach in GolfScript.

{\{\[.;]*}+@*}:A;

Online demo

\$\endgroup\$
4
\$\begingroup\$

JavaScript, 132 characters

function m(e,t,n){var r=0,i=[];var s=function(i){r++;for(var o=0;o<=e;o++){if(r==t)i[o]=n;else i[o]=s([])}r--;return i};return s(i)} 
UnGolfed:
function multiArray(maxRows, maxCols, val){
  var c = 0, farray = [];
  var recursive = function(array){
    c++;
    for(var r = 0; r <= maxRows; r++){ 
      if(c == maxCols) array[r] = val;
      else array[r] = recursive([]);
    }
    c--;
    return array;
  };
  return recursive(farray);
}
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 32 bytes

Within reach of GolfScript for once! Scrap that, I didn't see Howard's answer.

f=ConstantArray[#3,#+0Range@#2]&
\$\endgroup\$
0
\$\begingroup\$

APL, 19 bytes

{(¯1↑⍵)⍴⍨⊃(//)⌽2↑⍵}

Sample:

    {(¯1↑⍵)⍴⍨⊃(//)⌽2↑⍵} 2 4 'empty'
┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘
┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘

┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘
┌─────┬─────┐
│empty│empty│
├─────┼─────┤
│empty│empty│
└─────┴─────┘
\$\endgroup\$
3
  • \$\begingroup\$ Unneeded commute, you can change it to{(⊃(//)⌽2↑⍵)⍴¯1↑⍵}, and maybe even {(⊃(//)⌽2↑⍵)⍴⊃⌽⍵} \$\endgroup\$
    – Adalynn
    Jul 31, 2017 at 22:42
  • \$\begingroup\$ The commute is unneeded (and its absence would save us one character), but the second suggestion is wrong: "minus one take" returns a one element vector, whereas "first reverse" opens the box. Try it online. \$\endgroup\$
    – lstefano
    Aug 2, 2017 at 9:21
  • \$\begingroup\$ Yeah, I don't do well with stuff like that in APL (which is probably bad). \$\endgroup\$
    – Adalynn
    Aug 2, 2017 at 23:54
0
\$\begingroup\$

Python 3, 36 bytes

lambda a,b,x:eval("["*b+"x"+"]*a"*b)

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

J, 8 bytes

$/@]$<@[

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy