3
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.
5what does "without recursion" mean in this context? can you use a while loop? for loop? context would be helpful here... – Jeff – 2012-06-16T07:45:29.930
If I look at your third test case, it seems that you care about the order. Is
cdeabc
a valid combination? – Howard – 2012-06-16T08:45:39.160Yes it is. Why not? – beary605 – 2012-06-16T09:27:25.050
2@beary605 Because your test case says it is not. – Howard – 2012-06-16T09:55:21.620
1Are we permitted to assume that none of the input strings are empty or contain newlines? – Peter Taylor – 2012-06-16T10:01:12.423
Yep. Hehe, how would you input newlines? – beary605 – 2012-06-16T14:08:29.970
@rhymingorange Ehehe, recursion is when you call a function in itself. – beary605 – 2012-06-17T01:34:06.563
@beary605 I understand what recursion is. it's just that you can do the equivalent of recursion using a while loop with very little difficulty. Just trying to tease out the "spirit" of the problem. – Jeff – 2012-06-17T05:13:14.603
@rhymingorange It depends on the recursive algorithm, but yeah, that's true. – beary605 – 2012-06-17T05:41:43.123
To howards question: Shall we ignore the order of elements? So for example 3: abc-bcd = bcd-abc? – user unknown – 2012-06-18T04:24:45.653
Yep, whatever goes. As long as you have them all in there. – beary605 – 2012-06-18T04:27:05.990
Not all languages have to offer looping constructs other than explicit recursion. – dfeuer – 2019-03-06T04:51:14.440