Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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
share|improve this question
3  
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. –  ProgrammerDan Apr 4 at 14:23
4  
This feels homework-y, and even if I'm wrong it's not a particularly interesting challenge. –  undergroundmonorail Apr 4 at 14:23
3  
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. –  m.buettner Apr 4 at 14:35
2  
@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. –  ProgrammerDan Apr 4 at 14:39
3  
"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. –  Geobits Apr 4 at 14:45
show 7 more comments

5 Answers

GolfScript

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

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

Online demo

share|improve this answer
2  
@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. –  Peter Taylor Apr 4 at 14:53
    
@ProgramFOX You can even see in this short example that the @ is coloured differently in two places as is the A. –  Howard Apr 4 at 14:57
    
Ok, I'll keep it in mind. –  ProgramFOX Apr 4 at 14:57
add comment

GolfScript, 17 characters

A non-recursive approach in GolfScript.

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

Online demo

share|improve this answer
add comment

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);
}
share|improve this answer
add comment

Mathematica, 32 bytes

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

f=ConstantArray[#3,#+0Range@#2]&
share|improve this answer
add comment

TI-BASIC, 35

Pretty trivial. 21 bytes when compiled.

Input A,B,C:{A,B→dim([A]:Fill(C,[A]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.