Re: [Tutor] Counting matching elements in sequences

Magnus Lycka magnus at thinkware.se
Wed Dec 10 14:00:20 EST 2003


> > def alleq(seq):
> >     for item in seq[1:]:
> >         if item != seq[0]:
> >             return False
> >     return True
> 
> I came up with nearly the same one.
> 
> def alleq(seq):
>     k = seq[0]
>     for i in seq:
>         if i != k:
>             return 0
>     return 1

The reason I didn't write it like that was to
avoid the IndexError you get in "k = seq[0]"
if seq is empty. 

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