human
What is a code to program to generate all possible valid IP addresses from given string and the other steps required given the following:
Program to generate all possible valid IP addresses from given string | Set 2 | Function to get all the valid ip - addresses.
If index greater than givenString size and we have four block.
Remove the last dot.
Add ip - address to the results.
To add one index to ip - address.
Select one digit and call the same function for other blocks.
Backtrack to generate another possible ip address So we remove two index ( one for the digit and other for the dot ) from the end.
Select two consecutive digits and call the same function for other blocks.
Backtrack to generate another possible ip address So we remove three index from the end.
Select three consecutive digits and call the same function for other blocks.
Backtrack to generate another possible ip address So we remove four index from the end.
Driver code.
Fill result vector with all valid ip - addresses.
Print all the generated ip - addresses"
How can the above be solved in Python?
assistant
#Here is code to program to generate all possible valid IP addresses from given string and the other steps required.
 def GetAllValidIpAddress ( result , givenString , index , count , ipAddress ) :
	if ( len ( givenString ) == index and count == 4 ) :
		ipAddress . pop ( ) ;
		result . append ( ipAddress ) ;
		return ;
	if ( len ( givenString ) < index + 1 ) :
		return ;
	ipAddress = ( ipAddress + givenString [ index : index + 1 ] + [ ' . ' ] ) ;
	GetAllValidIpAddress ( result , givenString , index + 1 , count + 1 , ipAddress ) ;
	ipAddress = ipAddress [ : - 2 ] ;
	if ( len ( givenString ) < index + 2 or givenString [ index ] == '0' ) :
		return ;
	ipAddress = ipAddress + givenString [ index : index + 2 ] + [ ' . ' ] ;
	GetAllValidIpAddress ( result , givenString , index + 2 , count + 1 , ipAddress ) ;
	ipAddress = ipAddress [ : - 3 ] ;
	if ( len ( givenString ) < index + 3 or int ( " " . join ( givenString [ index : index + 3 ] ) ) > 255 ) :
		return ;
	ipAddress += givenString [ index : index + 3 ] + [ ' . ' ] ;
	GetAllValidIpAddress ( result , givenString , index + 3 , count + 1 , ipAddress ) ;
	ipAddress = ipAddress [ : - 4 ] ;
if __name__ == " _ _ main _ _ " :
	givenString = list ( "25525511135" ) ;
	result = [ ] ;
	GetAllValidIpAddress ( result , givenString , 0 , 0 , [ ] ) ;
	for i in range ( len ( result ) ) :
		print ( " " . join ( result [ i ] ) ) ;