Skip to main content
added 3 characters in body
Source Link
ybeltukov
  • 43.9k
  • 5
  • 109
  • 215

Just to be different

list = {1, 2, 3, 1, 2, 3};
m = Length[list];
n = 2;

Module[{k = 0, b},
 Permutations[ConstantArray[a, n]~Join~ConstantArray[b, m]] /. 
  b :> list[[1 + Mod[k++, m]]]]
{{a, a, 1, 2, 3, 1, 2, 3}, {a, 1, a, 2, 3, 1, 2, 3}, ..., {1, 2, 3, 1, 2, 3, a, a}}

Just to be different

list = {1, 2, 3, 1, 2, 3};
m = Length[list];
n = 2;

Module[{k = 0},
 Permutations[ConstantArray[a, n]~Join~ConstantArray[b, m]] /. 
  b :> list[[1 + Mod[k++, m]]]]
{{a, a, 1, 2, 3, 1, 2, 3}, {a, 1, a, 2, 3, 1, 2, 3}, ..., {1, 2, 3, 1, 2, 3, a, a}}

Just to be different

list = {1, 2, 3, 1, 2, 3};
m = Length[list];
n = 2;

Module[{k = 0, b},
 Permutations[ConstantArray[a, n]~Join~ConstantArray[b, m]] /. 
  b :> list[[1 + Mod[k++, m]]]]
{{a, a, 1, 2, 3, 1, 2, 3}, {a, 1, a, 2, 3, 1, 2, 3}, ..., {1, 2, 3, 1, 2, 3, a, a}}
Source Link
ybeltukov
  • 43.9k
  • 5
  • 109
  • 215

Just to be different

list = {1, 2, 3, 1, 2, 3};
m = Length[list];
n = 2;

Module[{k = 0},
 Permutations[ConstantArray[a, n]~Join~ConstantArray[b, m]] /. 
  b :> list[[1 + Mod[k++, m]]]]
{{a, a, 1, 2, 3, 1, 2, 3}, {a, 1, a, 2, 3, 1, 2, 3}, ..., {1, 2, 3, 1, 2, 3, a, a}}