Skip to content

Instantly share code, notes, and snippets.

@geoHeil
Created August 1, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoHeil/4578c82a1c39930aa18f3628ec2d92bb to your computer and use it in GitHub Desktop.
Save geoHeil/4578c82a1c39930aa18f3628ec2d92bb to your computer and use it in GitHub Desktop.
Validate sequence of numbers within allowed range / bounds
val valuesRight = Seq(1, 2, 3, 4, 5, 6, 7)
val valuesWrong = Seq(1, 2, 5, 6, 7, 8, 9)
val allowedValues = Range(1, 8)
def containsNotAllowedValues(allowed: Range, input: Seq[Int]): Boolean = {
!allowed.containsSlice(input)
}
containsNotAllowedValues(allowedValues, valuesRight) // expected false as no wrong element contained
// result: false ==> correct
containsNotAllowedValues(allowedValues, valuesWrong) // expected true as at least single wrong element is contained
// result: true ==> correct
// ------------------------
val valuesRightPartialMatch = Seq(1,3, 7)
containsNotAllowedValues(allowedValues, valuesRightPartialMatch) // expected true as at least single wrong element is contained
// result: true ==> incorrect, should also return false as no invalid number is found
@geoHeil
Copy link
Author

geoHeil commented Aug 1, 2019

It was my mistake that I did not specify

val valuesRightPartialMatch = Seq(1,3, 7)

initially as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment