Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16006

Re: Pattern matching in lists

From Achilleas Lazarides <achilleas.lazarides@gmx.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Pattern matching in lists
Date 2011-02-03 10:27 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iidvva$bhv$1@smc.vnet.net> (permalink)

Show all headers | View raw


If we define
lst = RandomInteger[{0, 10}, 100000];
str = ToString@FromDigits[lst];

One can simply partition the list and test:

StringMatchQ[str, ___ ~~ "1554367876" ~~ ___]
MemberQ[Partition[lst, Length@#, 1], #] &@
 {1, 5, 5, 4, 3, 6, 7, 8, 7, 6}

It's slower though. This is faster
randomname = Compile[{{lst, _Integer, 1}, {sublst, _Integer, 1}},
  	MemberQ[Partition[lst, Length@sublst, 1], sublst]]
but still slower than doing it with strings.

Surely there is a faster way though.


On Feb2, 2011, at 12:07 PM, Harvey P. Dale wrote:

> MemberQ easily tests whether a single integer appears in a list
> of integers.  Suppose, however, that I want to test not for a single
> integer but for two or more consecutive integers, e.g., to see whether
> {1,4,6} is a member of {1,3,2,6,5,1,4,6,3,9}.  I can do this by
> converting both lists into strings and then using string-matching
> functions, but is there some way of doing it directly without that
> conversion?
> 
> Thanks.
> 
> Harvey
> 
> Harvey P. Dale
> University Professor of Philanthropy and the Law
> Director, National Center on Philanthropy and the Law
> 139 MacDougal Street
> New York, N.Y. 10012-1076
> Tel: 212-998-6161
> Fax: 212-995-3149 
> =09
> 
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
> 

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Pattern matching in lists Achilleas Lazarides <achilleas.lazarides@gmx.com> - 2011-02-03 10:27 +0000

csiph-web