

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to generate all possible valid ID address from given string
String is given. String contains only digit. Our task is to check all possible valid IP address combinations.
Here first we check the length of the string then split by ".". Then we check the different combination of ".".
Example
Input : "255011123222" It's not a valid IP address. Input : 255011345890 Valid IP address is 255.011.123.222
Algorithm
Step 1: First check the length of the string. Step 2: Split the string by ".". We will place 3 dots in the given string. W, X, Y, and Z are numbers from 0-255 the numbers cannot be 0 prefixed unless they are 0. Step 3: Generating different combinations. Step 4: Check for the validity of combination.
Example Code
# Python code to check valid possible IP # Function checks wheather IP digits # are valid or not. def ipvalid(ip): # Spliting by "." ip = ip.split(".") # Checking for the corner cases for i in ip: if len(i) > 3 or int(i) < 0 or int(i) > 255: return False if len(i) > 1 and int(i) == 0: return False if len(i) > 1 and int(i) != 0 and i[0] == '0': return False return True # Function converts string to IP address def ipconvert(A): con = len(A) # Check for string size if con > 12: return [] newip = A l = [] # Generating different combinations. for i in range(1, con - 2): for j in range(i + 1, con - 1): for k in range(j + 1, con): newip = newip[:k] + "." + newip[k:] newip = newip[:j] + "." + newip[j:] newip = newip[:i] + "." + newip[i:] # Check for the validity of combination if ipvalid(newip): l.append(newip) newip = A return l # Driver code A = input("Enter IP address") print(ipconvert(A))
Output
Enter IP address25525522134 ['255.255.22.134', '255.255.221.34']
- Related Questions & Answers
- Program to find the maximum score from all possible valid paths in Python
- Program to find length of longest valid parenthesis from given string in Python
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- Program to generate all possible strings by taking the choices in python
- Program to find all possible IP address after restoration in C++
- How to find all possible permutations of a given string in Python?
- Program to find list of all possible combinations of letters of a given string s in Python
- Python Generate random string of given length
- Python - Generate all possible permutations of words in a Sentence
- Remove all duplicates from a given string in Python
- Python Program to print all permutations of a given string
- All possible strings of any length that can be formed from a given string?
- Java program to remove all the white spaces from a given string
- Java Program to generate random elements from a given array
- Program to find a good string from a given string in Python
Advertisements