Message Boards Message Boards

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

Working with For loop results in Mathematica

Posted 9 years ago

I am currently looking for a way to identify a palindromic sequence (same forwards and backwards) from a long string of numbers. This is the sequence I've been using:

sequence={6,3,2,4,**1,2,3,4,5,4,3,2,1**,3}

Note that positions 5 through 13 are the palindrome; 1,2,3,4,5,4,3,2,1. The way I have it set up, I am looking for palindromes of length 9 (query length; ql), such as this one, and the loop length is half of the palindrome length (loop length; LL), excluding the spacer which is number 5 in this case.

sl=Length[sequence] = 14

ql= 9 (I happen to be looking for palindromes of length 9 in this case)

LL=Round[ql/2-0.1] = 4

*note: I capitalized LL for increased visibility here in this post, I use lowercase letters for actual the actual inputs.

Recently, using a For loop function I have been able to list out every frame of 4 numbers in a row, starting with the first four, then on to the second four, and so on until I list the final four. Here is what I mean:

sequence={6,3,2,4,1,2,3,4,5,4,3,2,1,3}
input: For[n=1.n<=sl-LL-1,n++,Print[Part[sequence,n;;n+LL-1]]]

output:

{6,3,2,4}

{3,2,4,1}

{2,4,1,2}

{4,1,2,3}

{1,2,3,4}

{2,3,4,5}

{3,4,5,4}

{4,5,4,3}

{5,4,3,2}

{4,3,2,1}

{3,2,1,3}

Now here is my question:

Is there any way I can compare results specifically for palindromes of length 9? For example, we can see that the 5th result {1,2,3,4} is equivalent to the 10th result {4,3,2,1} if you were to reverse it. What I'm looking for is a way I can input the entire 14 number-long sequence, and have the output give a table or a matrix of some sort that shows when sequences of four match up. Another acceptable way would be some order of operations that would allow me to perform further functions on the results of the For loop (For loop within a For loop?).

I suspect functions such as NestWhile, Nest, Do, and If may be useful, but am struggling to find the appropriate combination of functions that will assist me. Also I suspect it might be more useful to list out results of length 9 (the query length) instead of length 4, but I still run into the problem of being able to further perform operations on the individual results afterwards.

Also, if anyone has any hints or advice on matrix math, or matrix functions that can help me, please do share! I have yet to learn basic matrix operations, but learning them is on my agenda for the near future.

Thank you for any and all help!

POSTED BY: Erik Gentzel
3 Replies
POSTED BY: Vitaliy Kaurov
Posted 9 years ago
POSTED BY: Paul Cleary
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