MathGroup Archive 2004

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

Search the Archive

Re: number of switches

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47513] Re: [mg47479] number of switches
  • From: János <janos.lobb at yale.edu>
  • Date: Thu, 15 Apr 2004 03:39:15 -0400 (EDT)
  • References: <200404141116.HAA27212@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Apr 14, 2004, at 7:16 AM, fake 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)?
>
>

My newbie approaches:

lst = {1, 0, 1, 0, 1}

Length[Cases[
     Partition[lst, 2, 1], {1, 0}]] + Length[Cases[Partition[
       lst, 2, 1], {0, 1}]]

4

or

Length[Partition[lst, 2, 1]] - Length[Position[Partition[lst,
   2, 1], {a_, a_} ]]

4

I am sure there is an  easier way without a Plus or Minus, but I am not 
good at pattern matching.
János
-------------------------------------------------
clear perl code is better than unclear awk code; but NOTHING comes 
close to unclear perl code
http://www.faqs.org/faqs/computer-lang/awk/faq/


  • Prev by Date: RE: number of switches
  • Next by Date: Re: number of switches
  • Previous by thread: Re: number of switches
  • Next by thread: Re: number of switches