Detecting Valid Latitude and Longitude Pairs

Sort by

recency

|

126 Discussions

|

  • + 0 comments

    TypeScript or JavaScript

    function main() {
        const regex = /^\([-+]?(((\d|[0-8]\d)(\.\d+)?)|(90(\.0+)?)),\s[-+]?(((\d{1,2}|1[0-7]\d)(\.\d+)?)|(180(\.0+)?))\)$/;
        for (let i = 1; i < inputLines.length; i++) {
            const str = inputLines[i];
            if (regex.test(str)) {
                console.info('Valid');
            } else {
                console.info('Invalid');
            }
        }
    }
    
  • + 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;
        }
    ?>
    
  • + 0 comments

    My code for Python (3)

    import re
    import sys
    data = sys.stdin.read()
    
    pattern=r'^\([\-\+]?(90(\.0{1,})?|(|[1-8][0-9]|[1-9])(\.[0-9]{1,})?)\,\s?[\-\+]?((180(\.0{1,})?|(|1[0-7][0-9]|[1-9][0-9]|[1-9])(\.[0-9]{1,})?))?\)$'
    
    for line in data.split("\n")[1:]:
        match = re.findall(pattern, line)
        print("Valid" if match else "Invalid")
    
  • + 0 comments

    PYTHON

    N = int(input())
    while N:
        N = N - 1
        s = input()
        m = re.search(r'[+-]?(?P<xAxis>\d+(\.\d+)?), [+-]?(?P<yAxis>\d+(\.\d+)?)', s)
        if m:
            x = m['xAxis']
            xf = float(x)
            y = m['yAxis']
            yf = float(y)
            if x[0] == '0' and len(x) > 1 and x[1] != '.':
                print("Invalid")
                continue
            if y[0] == '0' and len(y) > 1 and y[1] != '.':
                print("Invalid")
                continue
            if(xf > 90 or yf > 180) :
                print("Invalid")
            else:
                print("Valid")
        else:
            print("Invalid")
    
  • + 0 comments

    Perl

    while(<>){
        if ($. == 1){
            next;
        }
        if( /\((?<LAT>[\-\+]?[1-9]\d?(?:\.\d+)?),\s(?<LON>[\-\+]?1?\d{0,2}(?:\.?\d+))\)/ ){
            my $LAT = $+{LAT};
            my $LON = $+{LON};
           ($LAT >= -90 && $LAT <= 90 && $LON >=-180 && $LON <=180) ? print "Valid\n" : print "Invalid\n";
        } else {
            print "Invalid\n";
        }
    }