Standard Forth versus Python: a case study

idknow at gmail.com idknow at gmail.com
Wed Oct 11 22:25:58 EDT 2006


bearophileHUGS at lycos.com wrote:
> John Doty:
> > Yes. The efficient exact algorithms for this problem use *partial*
> > sorts. The Forth one from the FSL is of this class (although I know of
> > two better ones for big arrays). But it's tough to beat the efficiency
> > of the approximate histogram-based method the Python stats module
> > implements.
>
> The usual way to compute a true median with Python may be:
>
> def median(inlist):
>     newlist = sorted(inlist)
>     index = len(newlist) // 2
>     if len(newlist) % 2:
>         return newlist[index]
>     else:
>         return (newlist[index] + newlist[index-1]) / 2.0
>
[snip]

no sort() is needed to calculate the median of a list.

you just need one temp var.




More information about the Python-list mailing list