Write a program using Python that completes the following task: You are given N
ID: 3761482 • Letter: W
Question
Write a program using Python that completes the following task:
You are given N strings that may or may not be an Internet Protocol (IP) address. You need to detect if the text contained in each of the lines represents
IPv4 address
IPv6 address or
Neither of these
IPv4 was the first publicly used Internet Protocol; it used 4-byte addresses and permitted 232 distinct value. The typical format of an IPv4 address is A.B.C.D where A, B, C and D are integers lying between 0 and 255 (both inclusive).
IPv6, with 128 bits, was developed to permit the expansion of the address space.
“The 128 bits of an IPv6 address are represented in 8 groups of 16 bits each. Each group is written as 4 hexadecimal digits and the groups are separated by colons (:). The address
2001:0db8:0000:0000:0000:ff00:0042:8329 is an example of this representation.”
Groups with consecutive zeros will be left as they are.
An IPv6 value such as "...:0:..." or "...:5:..." is address-wise identical to "...:0000:..." or "...:0005:....". Leading zeros can be omitted in writing the address.
Constraints
1 N 50
There will be no extra text or white-space leading or trailing the IP address in a line that has an IP address. The number of characters in each line will not exceed 500.
Input Format
An integer N on a separate line, followed by N lines each containing a string that is either an IPv4 address or an IPv6 address, or an arbitrary text which does not correspond to either format.
Output Format
N lines.
The Ith output line should describe what you detected the Ith input line to be, using one of the following strings, with capitalization preserved:
IPv4
IPv6
Neither
“The 128 bits of an IPv6 address are represented in 8 groups of 16 bits each. Each group is written as 4 hexadecimal digits and the groups are separated by colons (:). The address
2001:0db8:0000:0000:0000:ff00:0042:8329 is an example of this representation.”
Explanation / Answer
count = input() count = int(count) def ipv4(addr): r = 0 for i in range(0,4): try: num = int(addr[i]) except: r=2 break if(num255): r=1 if(r==0): print("IPv4") return 1 return 0 def addzero(temp): n = len(temp) s="" for i in range(0,4-n): s+='0' return s+temp def ipv6(addr): r = 0 for i in range(0,8): addr[i]=addzero(addr[i]) for j in range(0,4): if(addr[i][j]'9') and (addr[i][j]'f'): r=1 if(r==0): print("IPv6") return 1 return 0 for i in range(0,count): addr = input() r=0; t1 = addr.split(":") if(len(t1)==8): r = ipv6(t1) if(r==1): continue; t2 = addr.split(".") if(len(t2)==4): r = ipv4(t2) if(r==1): continue; print("Neither");Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.