Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: number of switches

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47507] Re: [mg47479] number of switches
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Thu, 15 Apr 2004 03:39:10 -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)?
>

I don't know what you consider easy.  If you're list is not too long  
you can do it recursively

countswitches[{x_}]:=0

countswitches[l_List]:=If[l[[1]]! 
=l[[2]],1+countswitches[Rest[l]],countswitches[Rest[l]]]

If you're worried about recursing too deeply do it iteratively

countswitches[l_List]:=Module[{acc=0},Do[If[l[[i]]! 
=l[[i+1]],acc++],{i,Length[l]-1}];acc]

Regards,

Ssezi


  • Prev by Date: Re: When Is Precision[ ] $MachinePrecision, And When Is It Not?
  • Next by Date: How to check if a file exists?
  • Previous by thread: number of switches
  • Next by thread: Re: number of switches