Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_data(address):
- if '/' in address:
- address, routing_prefix = address.split('/')
- size = 32 - int(routing_prefix)
- else:
- size = 0
- address = list(map(int, address.split('.')))
- value = 0
- for item in address:
- value <<= 8
- value ^= item
- value >>= size
- return value, size
- Dict = dict()
- def add_address(address):
- value, size = get_data(address)
- if value not in Dict:
- Dict[value] = 0
- Dict[value] |= 1 << size
- def contains(address):
- value, size = get_data(address)
- if value not in Dict:
- return False
- return (Dict[value] >> size) & 1 == 1
- if __name__ == '__main__':
- arr = ['123.123.132.132/22', '31.3.3.3', '32.23.32.23/1']
- arr2 = ['123.123.132.132/2', '31.3.3.3/2', '32.23.32.23/3']
- for val in arr:
- add_address(val)
- for address in arr:
- print(contains(address))
- for address in arr2:
- print(contains(address))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement