In working through Programming Paradigms Via Mathematica (A First Course) I have come across this two part question:
A permutation of length "n" is a list of the first n positive integers in some order. Example: {6,1,5,3,4,2} is a permutation of length 6. A permutation is called balanced if the sum of each consecutive pair is equal to the sum of the consecutive pair furthest away from the original pair, if we imagine the permutation written in a circle. For instance, (6,1) and (3,4) are pairs furthest away in the example above, because they are both 1 unit away from the original pair. Similarly, (2,6) and (5,3) are furthest apart. Our example is balanced because pairs furthest apart have the same sum: 6+1=3+4; 1+5 =4+2; 5+3=2+6; 3+4=6+1; 4+2=1+5; 2+6=5+3. Another way to see this is to write the consecutive sums in order (7,6,8,7,6,8) and notice that the corresponding sums are the furthest apart. Note that all of this makes sense only if the length of the permutation (n) is even.
(a) Write a function "balancedQ[ ]" which takes one argument which is a list of even length and returns True if it is balanced, False otherwise.
My answer is:
Clear[balancedQ];
balancedQ[list_List] := Module[{list2, list3, list4, list5},
d = 1;
If [EvenQ[Length[list]],
list2 :=
Append[Partition[list, 2, 1], {First[list], Last[list]}], {d = 2,
Goto[End]}];
addPairs[{m_, n_}] := m + n;
list3 := addPairs /@ list2;
list4 := First[Partition[list3, Length[list3]/2]];
list5 := Last[Partition[list3, Length[list3]/2]];
While[Length[list4] > 0,
If[First[list4] == First[list5], {list4 = Rest[list4],
list5 = Rest[list5]}, {d = 2, Goto[End]}]];
Label[End];
If[d == 1, Print["True"], Print["False"]]];
While I am sure that it can be improved upon, it does work returning true for balancedQ[{6,1,5,3,4,2}]
and false for balancedQ[{6,1,5,3,4}]
(odd # of elements) and balancedQ[{6,1,5,3,2,4}]
(unbalanced). The second part of the question is where I have trouble:
(b) The Mathematica function "Permutations[]" returns all possible permutations of the elements of a list. Use "Permutations[]" and your function "balancedQ[]" to find all balanced permutations of the numbers {1,2,3,4,5,6}.
In looking through the previous lessons as well as the documentation center it seems to me that Select
is the correct command, and I used it as follows:
Select[Permutations[{1, 2, 3, 4, 5, 6}], balancedQ]
To my chagrin what I got in return was a column of 720 Falses (mainly) and Trues (a few).
Therefore my question is, should I be looking at a different command, or is there an option that I am not seeing relative to Select
?
Print
output, but instead return expressions. ReplaceIf[d == 1, Print["True"], Print["False"]]
withd === 1
and everything will work. If you want the rest of your code reviewed, please add thecode-review
tag. If not I shall close this question. $\endgroup$===
$\endgroup$balancedQ
, so I will do as you suggested and addcode-review
$\endgroup$balancedQ[lst_] := If[EvenQ[Length[lst]], Length[Union[Total /@ Partition[lst, 2]]] == 1,False]
$\endgroup$balancedQ[{6,1,5,3,4,2}]
$\endgroup$