Re: Re: [Q] {a,b,c}->{{a,b},{b,c}}
- To: mathgroup at smc.vnet.net
- Subject: [mg2632] Re: [mg2600] Re: [Q] {a,b,c}->{{a,b},{b,c}}
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Thu, 30 Nov 1995 21:00:55 -0500
In [mg2600] Re: [Q] {a,b,c}->{{a,b},{b,c}} "Bart van der Zwet" <B.W.v.d.Zwet at stud.tue.nl> gave a list of responses and timings (attached). Here are two variants that are slightly quicker than the fastest he reports.The main improvement comes from using Thread instead of Transpose. lst = Range[10^4]; Timing[Do[Transpose[{Drop[lst,-1],Drop[lst,1]}],{10}]][[1]] 7.8 Second Timing[Do[Thread[{Drop[lst,-1],Drop[lst,1]}],{10}]][[1]] 6.53333 Second Timing[Do[Thread[{Drop[lst,-1],Rest[lst]}],{10}]][[1]] 6.5 Second Allan Hayes hay at haystack.demon.co.uk Begin forwarded message: >From: "Bart van der Zwet" <B.W.v.d.Zwet at stud.tue.nl> >Subject: [mg2600] Re: [Q] {a,b,c}->{{a,b},{b,c}} Hello Mma-users, I while ago I posted a question here. I wanted to know how I could make {{a,b},{b,c}} out of {a,b,c} for instance, but then for any list. I received a number of different solutions that I shall mention in this mail. I've let them all solve the problem for the list : Range[10^4] and I include the timing to each solution. 1: Partition[list,2,1] 2.64 Second 2: Map[ Module[ {qux = absolutelyNothing}, (If[ qux === absolutelyNothing, qux = #; Sequence @@ {}, {qux, qux = #} ])& ], list ] 9.51 Second 3: Transpose[ { Drop[ list, -1 ], Drop[ list, 1 ] } ] 1.81 Second 4: r[ {x__} ] := Map[ Pattern[ #, Blank[] ]&, {x} ] -> Table[ { {x}[[i]], {x}[[i+1]] }, {i, 1, Length[ {x} ] - 1 } ] x /. r[x] 33.94 Second P.S. Not all responses are included, some didn't do exactly what I wanted or I couldn't get the answer with them. Some minor changes have been made in a solution. P.P.S. Thank you all for replying to my question, it's been of great help. Yours, Bart van der Zwet Student Mathematics Eindhoven University of Technology The Netherlands e-mail : B.W.v.d.Zwet at stud.tue.nl