MathGroup Archive 2003

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

Search the Archive

Re: Need a nice way to do this

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40294] Re: [mg40279] Need a nice way to do this
  • From: Selwyn Hollis <selwynh at earthlink.net>
  • Date: Sun, 30 Mar 2003 04:08:52 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Steve,

Nice problem. The following seems to work pretty well (and on the more 
general problem):

  countdiffs[s_List] := Module[{members, totals, g},
     members = Union[s];
     totals = Count[s, #] & /@ members;
     g = {};
     Scan[ Module[{i}, i = First@First@Position[members, #];
                 PrependTo[g, Plus @@ totals - totals[[i]]];
                 totals[[i]]--]&,
           Reverse[s] ];
     g ]

  s = Table[Random[Integer, {1, 9}], {10}]

            {3, 4, 4, 9, 9, 4, 1, 6, 1, 3}

  countdiffs[s]

            {0, 1, 1, 3, 3, 3, 6, 7, 7, 8}

Since this will probably become a speed contest :) ...

  s = Table[Random[Integer, {1, 9}], {5000}];
  First@Timing[countdiffs[s];]

        1.53 Second

(4.1.5, Mac OS X, 1GHz DP)
-----
Selwyn Hollis
http://www.math.armstrong.edu/faculty/hollis


On Saturday, March 29, 2003, at 05:19  AM, Steve Gray wrote:

> 	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.
>
>
>



  • Prev by Date: Re: Need a nice way to do this
  • Next by Date: Re: Map Projection with Mathematica
  • Previous by thread: Re: Need a nice way to do this
  • Next by thread: Re: Need a nice way to do this