Re: Partition List
- To: mathgroup at smc.vnet.net
- Subject: [mg8430] Re: Partition List
- From: tburton at cts.com (Tom Burton)
- Date: Sat, 30 Aug 1997 00:42:39 -0400
- Organization: Brahea Consulting
- Sender: owner-wri-mathgroup at wolfram.com
On 26 Aug 1997 04:27:39 -0400, in comp.soft-sys.math.mathematica you
wrote:
>A question on programing.
>I want to partition a list, using special values in the list as boundary
>indicators. For example, myList=3D{3,7,10,6,1,1,6,1,7,10,3,4,6} and my =
special
>value for boundary is 10, then my desired result would be
>result=3D{{3,7},{10,6,1,1,6,1,7},{10,3,4,6}}.
>
>How to do this using structure manipulating operators, and how to do it =
with
>pattern matching? Thanks.
>
Here's clumsy, slow method using pattern matching:
{myList//.{a__,10,b___}:>Sequence[{a},{10,b}]}
Run-time appears to be quadratic in the length of the list. I considered
tried to speed it up using nested lists and level-specific matching, but
then I timed my first attempt at structured programming:
Take[myList,#/.{{a_},{b_}}:>{a,b-1}]&/@
Partition[Join[{{1}},
Position[myList,10],
{{Length[myList]+1}}],
2,1]
This latter method is sooo much faster that I abandoned the first
approach. The latter method takes about 20 seconds on a list of one
million random integers (test case Table[Random[Integer,20],{1000000}]).
Run-time appears to be linear in the length of the list. Cool.
PS: Not surprisingly, Extract seems to perform about as well as Take;
just leave in the second level of braces.
Tom Burton