loading...

re: Daily Challenge #31 - Count IPv4 Addresses VIEW POST

FULL DISCUSSION
 

Python IPv4 and IPv6:

def ipsBetween(s,e): #Ipv4
    if s == e : return 0
    s,e,n=s.split('.'),e.split('.'),0
    for i in range(len(s)):
        if s[i]  < e[i]:    n+=int(e[i])
        elif s[i] == e[i] and n>0:  n+=255
        elif s[i]>e[i]: n+=255-int(s[i])
    return n
def ipsBetweenIPv6(s,e): #Ipv6
    s= [int(i,16) for i in s.split(':')]
    e= [int(i,16) for i in e.split(':')]
    return sum(map(int,e))- sum(map(int,s))

print(ipsBetween("10.0.0.0", "10.0.0.50"))
print(ipsBetween("10.0.0.0", "10.0.1.0"))
print(ipsBetween("20.0.0.10", "20.0.1.0"))  
print(ipsBetweenIPv6('2001:db8:85a3:8d3:1319:8a2e:370:7348','2001:db8:85a3:8d3:1319:8a2e:3a0:7348')) #48
code of conduct - report abuse