MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

[newbie] from right to left

  • To: mathgroup at
  • Subject: [mg48413] [newbie] from right to left
  • From: János <janos.lobb at>
  • Date: Fri, 28 May 2004 00:51:00 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


Let's say I have a list of letter like symbols:

lst = {a, b, a, j, a, n, o, s, c, a, d,a, b, a, j, a, n, o, s, c, a, d}

and have a pattern

pat = {j, a, n, o, s}

If I want to match by going from left to right

Position[Partition[lst, Length[pat], 1], pat]

gives me positions {{4}, {15}}.  So far so good.  Now the genie came 
out from the bottle and reversed the list, so now list reads like:

revlist = Reverse[lst]

revlist = {d, a, c, s, o, n, a, j, a, b, a, d, a, c, s, o, n, a, j, a, 
b, a}

and the genie say that now I have to read it from right to left and 
find the pattern that way.  I could now reverse the pattern

revpat = Reverse[pat]

revpat = {s, o, n, a, j}

and match it up to the reversed list like:

Position[Partition[revlst, Length[revpat], 1], revpat]

{{4}, {15}}

again, but that would not do me any good, because what I want is to 
match it from right to left, so the position should be:


that is, where "j" is found in revlist by reading from right to left.  
Adding Length[pat] to the undesired result of course will give me the 
"good" positions, but that looks somewhat "artificial".

I can program it out procedurally, but that is not "nice" and slow.

So, in summary, at odd occasions I read lst in western style and I can 
find the pattern easily, but at even occasions, when my eyes are 
reading the reversed lst from right to left, I have read it in arabic 
style and I want to find the pattern that way too.

Any good hint ?

Thanks ahead

clear perl code is better than unclear awk code; but NOTHING comes 
close to unclear perl code

  • Prev by Date: RE: removing letters from alphanumeric data
  • Next by Date: Re: style sheet for typesetting?
  • Previous by thread: Manipulating notebooks. Request for examples
  • Next by thread: Re: [newbie] from right to left