Re: number of switches
- To: mathgroup at smc.vnet.net
- Subject: [mg47544] Re: number of switches
- From: bobhanlon at aol.com (Bob Hanlon)
- Date: Thu, 15 Apr 2004 03:40:53 -0400 (EDT)
- References: <c5j6vd$qqo$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
After sending my first response I thought of a much faster method:
nbrSwitches1[x_?VectorQ] :=
Length[x //. {s___, a_, a_,e___} :> { s,a,e}]-1;
nbrSwitches1 /@
{{1,1,0,1},{1,1,0,0},{1,0,1,0,1}}
{2,1,4}
nbrSwitches2[x_?VectorQ] :=
(Length[Split[x]]-1);
nbrSwitches2 /@
{{1,1,0,1},{1,1,0,0},{1,0,1,0,1}}
{2,1,4}
data=Table[Random[Integer], {2000}];
nbrSwitches1[data]//Timing
{16.59 Second,1031}
nbrSwitches2[data]//Timing
{0.01 Second,1031}
Bob Hanlon
In article <c5j6vd$qqo$1 at smc.vnet.net>, "fake" <fake at fake.it> wrote:
<< Consider the lists {1,1,0,1} and {1,1,0,0},{1,0,1,0,1}.
The first sequence (1101) switches 2 times (#2digit~#3digit,
#3digit~#4digit}, the second (1100) 1 time, the third 10101 4 times.
I have the following problem.
Consider a list of binary digits. Which is the easiest way to count the
number of switches of the list (using Mathematica commands)?