RE: Pattern Matching in Lists
- To: mathgroup at smc.vnet.net
- Subject: [mg35563] RE: [mg35547] Pattern Matching in Lists
- From: "David Park" <djmp at earthlink.net>
- Date: Fri, 19 Jul 2002 06:08:18 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Anthony, Here are three ways. w = {1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0}; w2 = Partition[w, 2, 1]; Count[w2, {1, 0}] 4 count = 0; Do[If[w[[i]] == 1 && w[[i + 1]] == 0, count++], {i, 1, Length[w] - 1}]; count 4 Count[Drop[w - RotateLeft[w], -1], 1] 4 David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Anthony Mendes [mailto:amendes at zeno.ucsd.edu] To: mathgroup at smc.vnet.net Hello, Suppose w={1,1,1,0,0,1,0,1,0,0,1,0,0}. How can I count the number of occurrences of a 1 in w immediately followed by a 0 in w? I have tried every incarnation of Count[] I can think of; for example, Count[w,{___,1,0,___}] does not seem to work. In general, how can I count the number of occurrences of a 1 followed by a 0 in a list of 1's and 0's? Thank you! -- Tony _____________________ amendes at math.ucsd.edu