def getSubnetMask(ip):
    return int(ip[len(ip)-2:])


def subnet_calculator(ip_list):

    ip_ranges = []
    addresses = []
    for ip in ip_list:


        # find subnet mask
        subnet = getSubnetMask(ip)

        # calculate subnet 2**(8-(subnet % 8)
        free_addresses = 2**(8-(int(subnet) % 8))

        # remove dots in IP-Address so i have a number
        split_ip = ip[:len(ip)-3].split('.')

        block = getBlock(subnet)

        start_ip = list(split_ip)
        end_ip = list(split_ip)
        if free_addresses != 256:
            start_range = 0
            # find start and end of the IP-Range
            while True:
                end_range = start_range + free_addresses
                if start_range <= int(split_ip[block]) and end_range >= int(split_ip[block]):
                    break
                start_range = end_range

            # create two copy's of the split_ip list and change their start and end adress

            start_ip[block] = str(start_range)
            end_ip[block] = str(end_range-1)
        elif subnet != 32:
            start_ip[block] = '0'
            end_ip[block] = '255'

        #create 2d list and add start and end ip
        ip_ranges.append([])
        # save start and end in a 2D-list
        ip_ranges[-1].append(ip[:len(ip)-3])
        ip_ranges[-1].append(subnet)
        ip_ranges[-1].append('.'.join(start_ip))
        ip_ranges[-1].append('.'.join(end_ip))

        addresses.append(ip[:len(ip)-3])


    for address, subnet, start, end in ip_ranges:
        ip_blocks = address.split('.')
       # ip_block = getBlock(subnet)
        for a, s, st, e in ip_ranges:
            if address != a and subnet >= s:
                start_block = st.split('.')
                end_block = e.split('.')
                even = True
                compare_block = getBlock(s)
                for i in reversed(range(compare_block)):
                    if ip_blocks[i] != start_block[i]:
                        even = False
                        break

                # if ip in range -> remove from list
                if int(start_block[compare_block]) <= int(ip_blocks[compare_block]) and int(end_block[compare_block]) >= int(ip_blocks[compare_block]) and even:
                    addresses.remove(address)
                    break


    for ip in addresses:
        print(ip)

    print('--------------')



def getBlock(subnet):
    if subnet >= 0 and subnet <= 7:
        return 0
    elif subnet >= 8 and subnet <= 15:
        return 1
    elif subnet >= 16 and subnet <= 23:
        return 2
    else:
        return 3

subnet_calculator(['172.26.32.162/32', '172.26.32.0/24', '172.26.0.0/16', '172.26.32.199/25'])
subnet_calculator(['192.168.0.0/16', '172.24.96.17/32', '172.50.137.225/32', '202.139.219.192/32', '172.24.68.0/24',
                    '192.183.125.71/32', '201.45.111.138/32', '192.168.59.211/32', '192.168.26.13/32', '172.24.0.0/17',
                    '172.24.5.1/32', '172.24.68.37/32', '172.24.168.32/32'])

subnet_calculator(['172.24.68.0/24', '172.24.0.0/17', '192.168.59.211/32', '192.168.0.0/16'])
subnet_calculator(['172.24.96.17/32', '172.24.0.0/17'])