AW: Need a nice way to do this
- To: mathgroup at smc.vnet.net
- Subject: [mg40327] AW: [mg40279] Need a nice way to do this
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 1 Apr 2003 04:53:00 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Here another, perhaps surprising solution, which works, if the the elements
of the list are symbols (as in Steve's example):
In[236]:=
s = With[{syms = Table[Unique["s"], {100}]},
Table[syms\[LeftDoubleBracket]
Random[Integer, {1, 100}]\[RightDoubleBracket], {10000}]];
In[237]:=
(bob4 = Block[{count}, count[any_] := 0;
Range@Length@s - Map[++count[#] &, s]]); // Timing
Out[237]= {0.541 Second, Null}
In[238]:=
(hw6 = Block[#2,
Scan[(#1 = 0) &, #2];
Range[0, Length[#1] - 1] -
Function[{sym}, sym++, {HoldFirst}] /@ Unevaluated[#1]
] &[s, Union[s]]); // Timing
Out[238]= {0.23 Second, Null}
In[239]:= hw6 === bob4
Out[239]= True
--
Hartmut Wolf
-----Ursprüngliche Nachricht-----
Von: Steve Gray [mailto:stevebg at adelphia.net]
Gesendet: Samstag, 29. März 2003 11:20
An: mathgroup at smc.vnet.net
Betreff: [mg40279] Need a nice way to do this
Given a list consisting of only two distinct values, such as
s={a,b,b,a,a,a,b,a,b,a,a}, I want to derive a list of equal length
g={0,1,1,2,2,2,4,3,5,4,4}. The rule is: for each position
1<=p<=Length[s], look at list s and set g[[p]] to the number of
elements in s to the left of p which are not equal to s[[p]].
In a more general version, which I do not need now, s would
not be restricted to only two distinct values.
Thank you for any ideas, including other applications where
this particular calculation is used. The current application is an
unusual conjecture in geometry.