10
1
When given a a list of values and a positive integer n
, your code should output the cartesian product of the list with itself n
times.
For example, in pseudocode your function could be similar to:
for x1 in list:
for x2 in list:
for x3 in list:
...
for xn in list:
print x1, x2, x3, ... , xn
Example:
repeated_cart([1,2,3], 3)
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
Built in functions (or functions from imported libraries) that compute the Cartesian product (or power) are not allowed due to the resulting code being somewhat boring.
Inputs and outputs should be delimited but can be taken in any reasonable method.
the order the output is given does not matter but duplicates are not allowed.
This is my first time posting a question, so if I did anything horribly wrong, please tell me.
5
Welcome to PPCG! Nothing horribly wrong, but take some time to look at this meta post and answers.Things to avoid when writing challenges
– JayCe – 2018-05-29T18:37:21.0134
and to follow on @JayCe 's point, you could (should) post in The Sandbox to get feedback before posting a question :-)
– Giuseppe – 2018-05-29T18:40:53.833@Giuseppe Ok, I'll do that from now on, thanks :) – JoshM – 2018-05-29T18:47:37.627
1
Borderline dupe of https://codegolf.stackexchange.com/q/125104/194
– Peter Taylor – 2018-05-29T19:14:26.400@JoshuaMartin can we use builtins that return all n-tuples over 0...m-1? – ngn – 2018-05-29T19:18:53.263
@ngn sure but you'll need to make sure the output is in the right order – JoshM – 2018-05-29T19:25:27.150
The Cartesian product is an operation on sets. Do you allow sets for I/O rather than lists? – Jakob – 2018-05-29T23:47:50.010
1@Jakob sets should be fine – JoshM – 2018-05-30T01:24:34.680
My R program has the output in a different order than your example. You say "make sure the output is in the right order" but what does that mean? Could you put that in the question body so it's clear for future answers? – Giuseppe – 2018-05-30T17:17:14.490
@Giuseppe sorry, I realise now that order does not matter – JoshM – 2018-05-30T22:26:59.197