# parse ipv4 stirng into a returned list of numbers, or -1 if there is an input err # JGMiner June 2014 v3:Jan 2015 def addr2ints(s): a=[-1] # in order to catch dots with no digits will initialze negitive for d in s: if d.isdigit() : if a[-1]==-1 : a[-1] += 1 a[-1]=a[-1]*10+int(d) if a[-1]>255 : return [-1] #catch too large input elif d =='.' : if a[-1]==-1 : return [-1] a.append(-1) else : return [-1] # Not valid addr if len(a) != 4 or a[-1] == -1 : return [-1] return a # TEST HARNESS and TEST PATTERNS BELOW ================ def testa2i(s): #tests the function above : addr2ints print print "test input :",s a=addr2ints(s) print "list of integers from function: ", a print "python string split to list of strings: ", str.split(s,'.') #test coverage list testa2i("192.1.223.5") testa2i("34.123") testa2i("34.56.78.90.12") testa2i("23..12.3") testa2i("23.0.13.3") testa2i("2.3.255.1") testa2i("192.323.223.5") testa2i("192.-45.14.3") testa2i("192,1.23.5") testa2i("19w2.1.23.5") testa2i("....") testa2i("...") testa2i(".") testa2i("12345678901234567890")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question