Detecting Valid Latitude and Longitude Pairs

  • + 0 comments

    PHP solution:

    <?php
        $_fp = fopen("php://stdin", "r");
        $input = stream_get_contents($_fp);
    
        $lines = preg_split('/\R/', $input);
        // Remove first line
        array_shift($lines);
    
        $pattern = '/^\([\+\-]?(90(?:\.0+)?|[0-8]?\d(?:\.\d+)?),\s?[\+\-]?(180(?:\.0+)?|(?:1?[0-7]\d|0?\d?\d)(?:\.\d+)?)\)$/';
        foreach ($lines as $line) {
            // Check each line
            $result = preg_match($pattern, $line) ? 'Valid' : 'Invalid';
            echo $result . PHP_EOL;
        }
    ?>