MathGroup Archive 2009

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

Search the Archive

Re: Pattern matching / subsequence matching, question on

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101459] Re: [mg101419] Pattern matching / subsequence matching, question on
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Wed, 8 Jul 2009 07:08:10 -0400 (EDT)
  • References: <200907070904.FAA21966@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

I think you have a point, since the following codes work equally well:

Clear[find1, find2, findSubsequence]
find1[lis_List, {subseq__}] :=
  Position[Partition[lis, Length@{subseq}, 1], {subseq}]
find2[lis_List, subseq_List] :=
  Position[Partition[lis, Length@subseq, 1], subseq]
findSubsequence[lis_List, subseq_List] :=
  Module[{p}, p = Partition[lis, Length[subseq], 1];
   Position[p, Flatten[{___, subseq, ___}]]]

test = RandomInteger[{0, 1}, 100];
findSubsequence[test, {0, 0, 0}]
find1[test, {0, 0, 0}]
find2[test, {0, 0, 0}]

{{1}, {2}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {46}, {72}}

{{1}, {2}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {46}, {72}}

{{1}, {2}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {46}, {72}}

Bobby

On Tue, 07 Jul 2009 04:04:00 -0500, heycarnut <heycarnut at gmail.com> wrote:

> On pp 154-156 of the book, the example to search a sequence of digits
> for a given subsequence is shown as
>
> FindSubsequence[lis_List, subseq_List] := Module[{p}, p =
> Partition[lis, Length[subseq], 1]; Position[p, Flatten[{___,subseq,
> ___}]]]
>
> I'm puzzled at the use of Flatten[{___,subseq, ___}] for the pattern,
> instead of just subseq, and can't seem to see what, if anything, it
> adds to the functionality of the example. Can someone thaw my brain
> freeze?
>
> Thanks
> Rob
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: logical simplification problem
  • Next by Date: Manipulate not working
  • Previous by thread: Pattern matching / subsequence matching, question on example in
  • Next by thread: Re: Pattern matching / subsequence matching, question