1

Apologies, I am a newbie and I'm having trouble wording my question enough so I can search for it here, I tried but I couldn't find the answer using my search terms.

Given 1. a number length of array and 2. an array of different numbers how can I have combination like this through recursion?

const number_of_combinations = 4; // the arrays must be 4 in length
const combinations = [1,2,3];

// answer 
// [1,1,1,1]
// [1,1,1,2]
// [1,1,1,3]
// [1,1,2,1]
// [1,1,2,2]
// [1,1,2,3]
// etc....

p.s. If you can point me to a duplicate or answer I will delete this question right away, I know this must be something that's already been answered here.

Marco Cab
  • 11
  • 1
  • Iterate from 0 to 4, push a new item to an empty array containing one of the combination items. – Chloe Anderson Apr 09 '21 at 22:09
  • combinations or permutations? – Jonas Wilms Apr 09 '21 at 22:10
  • Is it supposed to be a recursive solution? – whiterook6 Apr 09 '21 at 22:12
  • @JonasWilms sorry, what is the difference? the resulting array has to be all different – Marco Cab Apr 09 '21 at 22:12
  • @whiterook6 yes, is it easier if it's not? – Marco Cab Apr 09 '21 at 22:13
  • When doing recursion, figure out the solution for the base case N=1. Then figure out how you combine a case with the next simpler case. – Barmar Apr 09 '21 at 22:13
  • This is a duplication of: https://stackoverflow.com/questions/32543936/combination-with-repetition – Rezart Qelibari Apr 09 '21 at 22:13
  • @MarcoCab: Even if this post is a duplicate, there's no need to delete it! Usually duplicates (after mod approval) will close the post and show a "This post already has answers here: ..." for others when they stumble apon the question. – Logan Devine Apr 09 '21 at 22:13
  • 1
    Does this answer your question? [Combination with repetition](https://stackoverflow.com/questions/32543936/combination-with-repetition) – Logan Devine Apr 09 '21 at 22:13
  • Ah, actually it's already in the question, you want permutation as the order matters ([1,1,1,2] and [1,1,2,1]) – Jonas Wilms Apr 09 '21 at 22:13
  • @Marco Cab easier or not depends but that's fine. To solve a recursive problem like this, you have to first think of how to break the problem into smaller parts then assemble them. For example, what are the solutions for this problem when you need only one item? I'd guess the array would look like [1, 2, 3]. Then figure out how to use that base case to build the solutions for one more: something + [1, 2, 3]. – whiterook6 Apr 09 '21 at 22:17
  • Thanks, everyone. It did answer the question, it's ok to leave this here or should I delete this question? – Marco Cab Apr 09 '21 at 22:38
  • It's okay to leave it, might help future googler's to find this :) – Jonas Wilms Apr 09 '21 at 22:54

1 Answers1

2

This is a great usecase for a recursive generator function:

  function* combine(source, length, prev = []) {
    if(prev.length >= length) {
       yield prev;
       return;
    }

    for(const el of source) {
       yield* combine(source, length, [...prev, el]);
    }
 }

 const result = [...combine([1, 2, 3], 4)];
Jonas Wilms
  • 106,571
  • 13
  • 98
  • 120
  • I always get confused seeing `function *` (coming from a C++ background) and I think "Returns a pointer to a function? *What?* oh wait it's a yield thingy" – Logan Devine Apr 09 '21 at 22:15