Wednesday, February 21, 2024
HomeSoftware EngineeringTips on how to create a Coordinates Validator in Golang

Tips on how to create a Coordinates Validator in Golang


The problem

You should create a perform that may validate if given parameters are legitimate geographical coordinates.

Legitimate coordinates appear to be the next: “23.32353342, -32.543534534”. The return worth ought to be both true or false.

Latitude (which is the primary float) could be between 0 and 90, constructive or detrimental. Longitude (which is the second float) could be between 0 and 180, constructive or detrimental.

Coordinates can solely include digits, or one of many following symbols (together with area after the comma)

There ought to be no area between the minus “-” signal and the digit after it.

Listed below are some legitimate coordinates:

  • -23, 25
  • 24.53525235, 23.45235
  • 04, -23.234235
  • 43.91343345, 143
  • 4, -3

And a few invalid ones:

  • 23.234, – 23.4234
  • 2342.43536, 34.324236
  • N23.43345, E32.6457
  • 99.234, 12.324
  • 6.325624, 43.34345.345
  • 0, 1,2
  • 0.342q0832, 1.2324

The answer in Golang

Possibility 1:

bundle answer
import (
  "strconv"
  "strings"
)
func IsValidCoordinates(coordinates string) bool {
    if strings.Incorporates(coordinates, "e") {
        return false
    }
    coords := strings.Cut up(coordinates, ", ")
    coord1, err1 := strconv.ParseFloat(coords[0], 64)
    coord2, err2 := strconv.ParseFloat(coords[1], 64)
    if err1 != nil || err2 != nil || coord1 < -90 || coord1 > 90 || coord2 < -180 || coord2 > 180 {
        return false
    }   
    return true
}

Possibility 2:

bundle answer
import (
  "math"
  "strings"
  "strconv"
)
func IsValidCoordinates(coordinates string) bool {
  var (
    err error
    coord []string
    num float64
  )
  if coord = strings.Cut up(coordinates, ", "); len(coord) != 2 {
    return false
  }
  for indx, c := vary coord {
    if strings.ContainsRune(c, 'e') { //test for scientific notation
      return false
    }
    if num, err = strconv.ParseFloat(c, 64); err != nil {
      return false
    }
    if math.Abs(num) > float64(90*(indx+1)) { 
      return false
    }
  }
  return true
}

Possibility 3:

bundle answer
import "regexp"
func IsValidCoordinates(coordinates string) bool [0-8]?d(.d+)?), -?(180

Check circumstances to validate our answer

bundle solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("Legitimate coordinates", func() {

	validCoordinates := []string{
		"-23, 25",
		"4, -3",
		"24.53525235, 23.45235",
		"04, -23.234235",
		"43.91343345, 143"}

	It("ought to validate the coordinates", func() {
		for _, coordinates := vary validCoordinates {
			Count on(IsValidCoordinates(coordinates)).To(Equal(true))
		}
	})
})

var _ = Describe("Invalid coordinates", func() {

	invalidCoordinates := []string{
		"23.234, - 23.4234",
		"2342.43536, 34.324236",
		"N23.43345, E32.6457",
		"99.234, 12.324",
		"6.325624, 43.34345.345",
		"0, 1,2",
		"0.342q0832, 1.2324",
		"23.245, 1e1"}
    
	It("ought to invalidate the coordinates", func() {
		for _, coordinates := vary invalidCoordinates {
			Count on(IsValidCoordinates(coordinates)).To(Equal(false))
		}
	})
})
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments