Detecting Valid Latitude and Longitude Pairs

Sort by

recency

|

127 Discussions

|

  • + 0 comments
    import re
    
    pattern = r'^\(([+-]?(?:0|[1-9]\d*)(?:\.\d+)?),\s([+-]?(?:0|[1-9]\d*)(?:\.\d+)?)\)$'
    #PATTERN BREAKDOWN
    # ^             => start of the match
    # \(            => literal (
    # ([+-]?        => optional sign for x
    # (?:0|         => either just 0 (non-capturing)
    # [1-9]\d*)     => or non zero digits followed by any digits
    # (?:\.\d+)?    => optional decimal part, must have one or more digits after decimal
    # ?:            => non-capturing group
    # ,\s           => exactly one space after ,
    # ([+-]?        => optional sign for Y
    # same as X ahead
    
    n = int(input())
    for _ in range(n):
        line = input().strip()
        match = re.fullmatch(pattern, line)
        
        if match:
            x = float(match.group(1))
            y = float(match.group(2))
            if -90 <= x <= 90 and -180 <= y <= 180:
                print("Valid")
            else:
                print("Invalid")
        else:
            print("Invalid")
    
  • + 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")