Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have created a simple way to create rotational-symmetric (180 degrees) rectangles, but my stuffit function bothers me. No matter what I tried, I couldn't get it to work within the recursive function. Can someone show me the way?

stuffit[n_List, k_] := 
Block[{j = n}, For[m = k, m <= Length[j], m += k, j[[m]] = k]; 
Return[j]]  

f[1] := 1
f[k_] := stuffit[Flatten[Table[f[k - 1], {n, (Cyclotomic[k, 1])}]], k]
f[k_, p_] := Partition[f[k], p]

f[6, 6] // MatrixForm  

Edit Replaced stuffit with MapAt from kguler's answer below. I used it within the recursive routine. Also, added parameter to the partitioning function to subtract the 3rd parameter from p to take the smaller rectangle: f[6,6,1] skips the rightmost column.

f[1] := 1  
f[k_] := MapAt[k &, Flatten[Table[f[k - 1],
  {n, (Cyclotomic[k, 1])}]], {k ;;  ;; k}]
f[k_, p_, x_] := Partition[f[k], p][[All, 1 ;; p - x]]

f[6, 6, 0] // MatrixForm (*below*)

$$\left( \begin{array}{cccccc} 1 & 2 & 3 & 4 & 5 & 6 \\ 1 & 4 & 3 & 5 & 1 & 6 \\ 1 & 2 & 5 & 4 & 1 & 6 \\ 1 & 5 & 3 & 2 & 1 & 6 \\ 5 & 2 & 3 & 4 & 1 & 6 \\ 1 & 4 & 3 & 2 & 5 & 6 \\ 1 & 2 & 3 & 5 & 1 & 6 \\ 1 & 4 & 5 & 2 & 1 & 6 \\ 1 & 5 & 3 & 4 & 1 & 6 \\ 5 & 4 & 3 & 2 & 1 & 6 \\ \end{array} \right)$$

The rotational symmetry is 180 degrees using the $k-1$ rectangle (omitting the rightmost column, including the ends of the rows.) I think this is the coolest thing I have ever created because it partitions down and keeps the symmetry. Try 6,4,0 and 6,5,0 and 6,4,1 and 6,5,1

share|improve this question

1 Answer 1

up vote 2 down vote accepted
ClearAll[stuffit2,stuffit3]; 
stuffit2[n_List, k_] := MapAt[k &, n, {k ;; ;; k}] (* thanks: Mr.Wizard *)
stuffit3[n_List, k_] := ReplacePart[n, List /@ Range[k, Length[n], k] -> k]
(stuffit2[Range[6], #] & /@ Range[6]) ==
    (stuffit3[Range[6], #] & /@ Range[6]) ==  (stuffit[Range[6], #] & /@ Range[6]) 
(* True *)

Used in the recursive functions:

f2[1] := 1
f2[k_] := stuffit2[Flatten[Table[f2[k - 1], {n, (Cyclotomic[k, 1])}]], k]
f2[k_, p_] := Partition[f2[k], p]

f3[1] := 1
f3[k_] := stuffit3[Flatten[Table[f3[k - 1], {n, (Cyclotomic[k, 1])}]], k]
f3[k_, p_] := Partition[f3[k], p]

f2[6, 6] == f3[6, 6] == f[6, 6]
(* True *)
share|improve this answer
    
I expect that you can use k ;; ;; k in place of k ;; Length[n] ;; k. –  Mr.Wizard May 28 at 6:29
    
Yes ... thank you @Mr.W. –  kguler May 28 at 9:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.