# Generating combinations without recursion [closed]

Given a list of strings and a length, give all combinations of that list with the given length. The problem is: your code must not be recursive. Yes, it can be done. I have done it myself, when I had no clue what this "recursion" was.

Input: A list of strings of an undefined length, and an integer defining the length of the combination. The strings will be at least one character long, and will only contain letters or numbers. The way of input will be specified by you, the programmer.

Output: A list of possible combinations as strings, not necessarily distinct (although you can make it that way, if you like). You do not have to sort the list.

Test Cases: As said before, the method of input will be specified by you.
[a,b] 3 -> Nothing
[1,0,0] 2 -> 10 10 00, or 00 10 if distinct.
[abc,bcd,cde] 2 -> abcbcd abccde bcdcde

If you even manage to do this, I will commend you. As always, shortest code wins. Good luck.

## closed as unclear what you're asking by Sriotchilism O'Zaic, Rɪᴋᴇʀ, mbomb007, Laikoni, StephenJan 5 at 15:01

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• what does "without recursion" mean in this context? can you use a while loop? for loop? context would be helpful here... – Jeff Jun 16 '12 at 7:45
• If I look at your third test case, it seems that you care about the order. Is cdeabc a valid combination? – Howard Jun 16 '12 at 8:45
• Yes it is. Why not? – beary605 Jun 16 '12 at 9:27
• @beary605 Because your test case says it is not. – Howard Jun 16 '12 at 9:55
• Are we permitted to assume that none of the input strings are empty or contain newlines? – Peter Taylor Jun 16 '12 at 10:01

## GolfScript (44 42 32 chars)

n%){\,=}+[[]]@1/{{1$+}+%}%;\,n* This takes input on stdin as a list of newline-separated strings followed by a line containing the desired subset size. It assumes that none of the input strings is empty. It takes some inspiration from Howard's solution, and can be shortened by two chars if using his input format: ~{\,=}+[[]]@1/{{1$+}+%}%;\,n*

Output is a newline separated list of concatenations.

E.g.

ABC
ABD
ACD
BCD