Re: Position within a list[2]
- To: mathgroup at smc.vnet.net
- Subject: [mg32972] Re: Position within a list[2]
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Fri, 22 Feb 2002 01:48:57 -0500 (EST)
- References: <a4t08v$igf$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hartmut Wolf's solution seems the way to do this.
But I did miss a trick -- using the function Ordering to speed up sorting.
This is generally useful and speeds up the code by a factor of seven
(Hartmut's code is still more than three times as quick than the new code)
Here are the timings.
rdp=RealDigits[Pi,10,10000][[1]];
Old code
Timing[Last[Sort[Split[Transpose[
{rdp, Range[Length[rdp]]}],
#1[[1]] == #2[[1]] & ],
Length[#1] <= Length[#2] & ]]]
{12.14 Second,{{9,763},{9,764},{9,765},{9,766},{9,767},{9,768}}}
New code
Timing[(Last[#1[[Ordering[Length /@ #1]]]] & )[
Split[Transpose[{rdp, Range[Length[rdp]]}],
#1[[1]] == #2[[1]] & ]]]
{1.59 Second,{{9,763},{9,764},{9,765},{9,766},{9,767},{9,768}}}
Most of the time is now take with Split - there seems to be no similar
trick for speeding this up when the equality condition is non-default.
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Dana DeLouis" <ng_only at hotmail.com> wrote in message
news:a4t08v$igf$1 at smc.vnet.net...
> Hello. A long time ago someone posted an elegant solution, but I can not
> find it in the Archives.
> Given a list that may have repeating data, this returned the character
that
> was repeated the most, and its position..
>
> The example given was a list of the first 10,000 digits of Pi. I believe
> the answer was that the number 9 was repeated 6 times around position
> 700 or 800.
> The function was very short and elegant.
> I don't believe the Split function was used, but I might be wrong.
>
> Does anyone know how to do this? Thank you.
>
> lst = RealDigits[Pi,10,10000][[1]];
>
> The list would have...
> {3, 1, 4, 1, 5, 9, 2, 6, etc)
>
>
> --
> Dana DeLouis
> Windows XP & Mathematica 4.1
> = = = = = = = = = = = = = = = = =
>
>
>
>