Detecting Valid Latitude and Longitude Pairs

Sort by

recency

|

124 Discussions

|

  • + 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";
        }
    }
    
  • + 0 comments

    Java 15:

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt(); scanner.nextLine();
            String x = "([+-]?((90(\\.0+)?)|([1-8]?[0-9](\\.[0-9]+)?)))";
            String y = "([+-]?((180(\\.0+)?)|((1[0-7][0-9]|[1-9]?[0-9])(\\.[0-9]+)?)))";
            String regex = "^\\(" +x+ ",\s" +y+ "\\)$";
            Pattern pattern = Pattern.compile(regex);
            for(int i=0;i<n;i++)
            {   Matcher matcher = pattern.matcher(scanner.nextLine());
                if (matcher.find()) System.out.println("Valid");
                else System.out.println("Invalid");
            }
        }
    }
    
    /*
    ------------------Regex for x------------------------
    x = "(
            [+-]?
            (
                ( 90              (\\.0+)?     )       |
                ( [1-8]? [0-9]    (\\.[0-9]+)? )
            )
         )"
        
    x is a floating number between (-90,+90)
    [+-]? means there may or may not be sign
    
    First think of integer number between (-90,+90). Ignoring sign, 
        One digit combination: [0-9]
        Two digit combination for 10 to 89 : [1-8][0-9]
        Ninty: 90
    That is, ( 90 | [1-8]?[0-9] ) 
    
    Now think about floating points. \\. denotes original dot.
        If the number is 90, floating point should never cross 0. Only 90.00000 is allowed.
        (\\.0+)? means ".0" or ".00" or ".000" or ...
    
        For any number other than 90, Floating point can be made up of any digit. 
        So (\\.[0-9]+) means any decimal point
    
    Whole thing should be covered by () to avoid ambiguity while concatenating this x to the regex for (x,y).
    
    
    ------------Regex for y------------------
    y = "(
            [+-]?
            (   (180                                (\\.0+)?)       |
                ( ( 1[0-7][0-9] | [1-9]?[0-9] )     (\\.[0-9]+)?)
            )
        )"
    y is a floating number between (-180,+180)
    
    Integer (-180,+180) ignoring the sign:
        180
        1[0-7][0-9]
        [1-9][0-9]
        [0-9]
        So combining 180 or 1[0-7][0-9] or [1-9]?[0-9]
    
    Floating point for 180 is .0000. So (\\.0+)?)
    Floating point for others is (\\.[0-9]+)?)
    */
    
  • + 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")