Message Boards Message Boards

0
|
5025 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Working with For loop results in Mathematica

Posted 9 years ago
POSTED BY: Erik Gentzel
3 Replies
POSTED BY: Vitaliy Kaurov
Posted 9 years ago

Is this something like what you need?

sequence = {6, 3, 2, 4, 1, 2, 3, 4, 5, 4, 3, 2, 1, 3}; Do[
 s = sequence[[a ;; b]]; 
 If[s == Reverse[s], Print[s]], {a, 1, Length[sequence] - 1}, {b, 
  a + 1, Length[sequence]}]


{1,2,3,4,5,4,3,2,1}

{2,3,4,5,4,3,2}

{3,4,5,4,3}

{4,5,4}
POSTED BY: Paul Cleary

Here is one way of approaching this I think would be effective:

If I list out results of length 9, I obtain the following:

{6,3,2,4,1,2,3,4,5}

{3,2,4,1,2,3,4,5,4}

{2,4,1,2,3,4,5,4,3}

{4,1,2,3,4,5,4,3,2}

{1,2,3,4,5,4,3,2,1}

{2,3,4,5,4,3,2,1,3}

Is there any way I could write the For loop function (For loop within a For loop) such that I look at each result, and query if position 1 is equal to position 9 and if 2 is equal to 8 and so on; such as:

Table[If[Part[result,n]==Part[result,ql+1-n],true,false,{n,1,LL}]  

and if I obtain any falses, I discard the result?

This would discard the first four results as they are not palindromes, but it would list out the 5th result, {1,2,3,4,5,4,3,2,1}, and would also discard the 6th result.

POSTED BY: Erik Gentzel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract