Problem statement

https://leetcode.com/problems/ip-to-cidr/

Code

class Solution:
    def ipToCIDR(self, ip, n):
        def ipToInt(ip):
            return sum(int(d)*256**(3-i) for i,d in enumerate(ip.split('.')))

        def intToIP(n):
            return ".".join(str((n >> i) % 256) for i in (24, 16, 8, 0))

        start = ipToInt(ip)
        result = []
        
        while n:
            step = start & -start if start != 0 else 1<<32
            while (step > n): step //= 2
                
            result.append(intToIP(start) + "/" + str(32-int(log2(step))))
            start += step
            n -= step

        return result