Advertisement
999ms

Untitled

Jul 16th, 2020
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. def get_data(address):
  2.     if '/' in address:
  3.         address, routing_prefix = address.split('/')
  4.         size = 32 - int(routing_prefix)
  5.     else:
  6.         size = 0
  7.  
  8.     address = list(map(int, address.split('.')))
  9.  
  10.     value = 0
  11.     for item in address:
  12.         value <<= 8
  13.         value ^= item
  14.  
  15.     value >>= size
  16.     return value, size
  17.  
  18.  
  19. Dict = dict()
  20.  
  21.  
  22. def add_address(address):
  23.     value, size = get_data(address)
  24.     if value not in Dict:
  25.         Dict[value] = 0
  26.     Dict[value] |= 1 << size
  27.  
  28.  
  29. def contains(address):
  30.     value, size = get_data(address)
  31.     if value not in Dict:
  32.         return False
  33.     return (Dict[value] >> size) & 1 == 1
  34.  
  35.  
  36. if __name__ == '__main__':
  37.     arr = ['123.123.132.132/22', '31.3.3.3', '32.23.32.23/1']
  38.     arr2 = ['123.123.132.132/2', '31.3.3.3/2', '32.23.32.23/3']
  39.  
  40.     for val in arr:
  41.         add_address(val)
  42.  
  43.     for address in arr:
  44.         print(contains(address))
  45.  
  46.     for address in arr2:
  47.         print(contains(address))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement