Detecting Valid Latitude and Longitude Pairs

  • + 1 comment
    n = int(input())
    lines = [input() for _ in range(n)]
    
    regex = r"^\(?([+-]?(?!0)\d+?(?:\.\d+?)?),\s([+-]?(?!0)\d+(?:\.\d+)?)\)?$"
    
    for line in lines:
        match = re.match(regex, line)
        if match:
            lng = float(match.group(1))
            lat = float(match.group(2))
            
            if (-90 <= lng <= 90) and (-180 <= lat <= 180):
                print("Valid")
            else:
                print("Invalid")
        else:
            print("Invalid")