Re: [Tutor] Counting matching elements in sequences

Magnus Lycka magnus at thinkware.se
Wed Dec 10 13:59:49 EST 2003


I wrote:
> >def alleq(seq):
> >    for item in seq[1:]:
> >        if item != seq[0]:
> >            return False
> >    return True

Gregor wrote: 
> What do you think about:
> 
>  >>> def alleq(seq):
> ...     return seq == seq[:1]*len(seq)

I think that if I had come up with either one
I would leave the subject and consider the problem
to be solved unless there was a performance problem.

If there was a performance problem, I'd use the
profiler or the new timeit module to measure which
is faster. With very big sequences, there might be
a memory issue as well, since "seq[:1]*len(seq)"
makes an extra list, but that would probably lead
to a performance problem if it caused swapping etc,
so the profiler would probably give the same answer
as a memory measurement.

-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus at thinkware.se



More information about the Tutor mailing list