print "I was challenged to parse an Ip address into 4 integers at the whiteboard, having long ago left coding by rote this was my answer : " def whiteboard(s): temp=0 num=0 a=[] #missing from the board for i in s: if i == '.': a.append(temp) #on the board I erred by direct addressing vs appending num+=1 temp=0 else: temp=10*temp+int(i) a.append(temp) #on the board I erred by direct addressing vs appending return a print "Results:", s="192.168.23.1" print "Input ",s a = whiteboard(s) print "S = ",a print " " print "I was able to refactor later that day to this : " def whiteboard2(s): a=[0] for i in s: try : a[-1]=10*a[-1]+int(i) except : a.append(0) return a s="192.168.23.2" print "Input ",s print "Results:", a = whiteboard2(s) print "S = ",a
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