167k views
3 votes
Given a list of string that may represent valid latitude and longitude pairs. write a python 3 function that check if the given latitude and longitude pairs are valid or not. a string (x,y) is considerd valid if the following criteria are met:

1. the string starts with a bracket, has a comma after x and ends with a bracket.
2. there is no space between the opening parenthesis and the first character of x.
3. there is no space between the comma and the last character of x.
4. there is no space between the comman and the first character of y.
5. there is no space between y and the closing parenthesis."
6. x and y are decimal numbers and may be preceded by a sign.
7. there are no leading zeros.
8. no other characters are allowed in x or y
9. -90 <= x <= 90 and -180 <= y <= 180

input:
the first line of input consist of an integer - input_size, representing the size of the string(n). the next line consists of n space-separated substrings containing the latitude/longitude pairs.

output:
print n space-separated strings representing the valid and invalid latitude/longitude pairs from the given list of strings. print "Valid" for valid pairs and "Invalid" for invalid pairs example: input: 5 (90,180)(+90,+180)(90.,180)(90.0,180.1)(855,95w)

output: Valid Valid Invalid Invalid Invalid


"""

inputStr, represents the given string containing substring of latitude/longitude pairs.

"""

def funcValidPairs(inputStr):

# Writecode here

return


def main():

#input for inputStr

inputStr = []

inputStr_size = int(input())

inputStr = list(map(str,input().split()))

result = funcValidPairs(inputStr)

print(" ".join([str(res) for res in result]))


if __name__ == "__main__":

main()

User TLS
by
8.0k points

1 Answer

5 votes

Final answer:

To check if a given latitude and longitude pair is valid, we can use regular expressions in Python.

Step-by-step explanation:

To check if a given latitude and longitude pair is valid, we can use regular expressions in Python. Here is the code:

import re
def is_valid(pair):
pattern = r'\((-?\d{1,2}(\.\d{1,})?),(-?\d{1,3}(\.\d{1,})?)\)'
return re.fullmatch(pattern, pair) is not None

inputStr = input()
pairs = inputStr.split()

result = []
for pair in pairs:
if is_valid(pair):
result.append('Valid')
else:
result.append('Invalid')

print(' '.join(result))

User Lukecampbell
by
7.3k points