Re: How to use Rules on Sequences?
- To: mathgroup at smc.vnet.net
- Subject: [mg29472] Re: [mg29452] How to use Rules on Sequences?
- From: BobHanlon at aol.com
- Date: Thu, 21 Jun 2001 01:56:48 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 2001/6/20 5:06:38 AM, dmueller at mathematik.uni-kassel.de
writes:
>I am faced to the following Problem:
>
>given a List of Pairs of the Form
>
>L = {{a1,m1},{a2,m2}, ... ,{ai,mi}}
>
>and a Number a, I want to replace the Pair
>{a, mj}, if it appears in the List by
>{a, mj-1}.
>
>This Job is done by
>L /. {{a,_m}->{a,m-1}}.
>
>Now I want to remove an Item of the Form
>{a,0} entirely from The List.
>
>Can this be done by applying rules (without
>leaving Null-Entrys), or is "Select" the only
>Way?
>(By the way: L is sorted with respect to the first Elements
>of the Sublists, this first elements are unic identifiers.
>Another Question is to replace an {a,_m} in L by {a,m+1},
>if it exists or else insert {a,1} in the propper
>Place - the inverse Operation of the above desribed
>one. Suerly not difficult to realize - but what might
>be the most efficient Method?)
>
>More general, I wonder, how to replace
>a Pattern in a Sequence - for example to change
>every occurence of "x_, y_?positive, x_" in a Sequence
>by "x,0,x".
>
>Say
>
>Emil[1,2,3,2,0,a,b,c]/.R ---> (Replacing ... 2,3,2 ... )
>Emil[1,2,0,2,0,a,b,c]/.R ---> (Replacing ... 0,2,0 ... )
>Emil[1,2,0,0,0,a,b,c]/.R (No more changes).
>
>I Fail to write down a syntactically correct rule R to
>do this Job.
>
a=2;
L = {{1,m1},{2,1},{3,m3}, {4,m4}};
% /. {a, m_} :> {a, m-1} /. {x___, {a, 0}, y___} :> {x, y}
{{1, m1}, {3, m3}, {4, m4}}
The inverse is then
L == ((Last /@
Split[Sort[Append[%, {a, 0}]], #2[[1]]===#1[[1]]&]) /. {a,
m_} :> {a, m+1})
True
Emil[1,2,3,2,0,a,b,c] //.
Emil[s___, x_, y_?Positive, x_, e___] :> Emil[s, x, 0, x, e]
Emil[1, 2, 0, 0, 0, a, b, c]
Bob Hanlon
Chantilly, VA USA