re Challenge: More Compact?

Tim Hammerquist tim at vegeta.ath.cx
Mon Jul 16 16:20:47 EDT 2001


Me parece que Marcin 'Qrczak' Kowalczyk <qrczak at knm.org.pl> dijo:
> Sun, 15 Jul 2001 21:36:22 -0400, Tim Peters <tim.one at home.com> pisze:
> 
> > def valid_ip(ip):
> >     m = re.match(r'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$', ip)
> >     if m:
> >         elements = map(int, m.groups())
> >         return max(elements) < 256
> >     return 0
> 
> Is 0127.0.0.00001 a valid IP?
> 
> If yes, then it should be accepted. If no, what about 127.0.0.001?

with
: def valid_ip(ip):
:     m = re.match(r'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(?!\n)$', ip)
:     return (m and max(map(int, m.groups())) < 256) or 0
valid_ip('127.0.0.001') returns 1
valid_ip('0127.0.0.001') returns 0
valid_ip('127.0.0.00001') returns 0

with Mozilla/4.73 [en] (X11; I; Linux 2.2.15-4mdk i686):
    0127.0.0.001 fails.
    127.0.0.001 succeeds.
    127.0.0.00001 succeeds.

This is, of course, just what the browser accepts. =)

-- 
C combines all the power of assembly language with all
the ease of use of assembly language.
    -- trad



More information about the Python-list mailing list