This is hard to explain, I want to make sure a certain item does not exist between two other items. Here's some examples, "-1" is the item to watch:
1, 3, -1, 0, 1, 2 == BAD!
-1, 0, 3, -1, 1, 3 == BAD!
2, -1, -1, 4, 3, 1 == BAD!
1, -1, -1, -1, 2, 0 == BAD!
-1, -1, 0, -1, -1, -1 == BAD!
however...
if it's at the end or the beginning it's okay:
-1, 1, 2, 4, 0, -1 == OKAY!
0, 1, 2, -1, -1, -1 == OKAY!
-1, -1, -1, 2, 2, 1 == OKAY!
context: these are guitar chords and the "-1" is a muted string. I don't want the muted string in the middle of the chord because that's very hard to play
I hope that makes sense. If you know a better way to phrase it let me know and I'll change the title
edit: here's what I have so far because someone asked, but I honestly don't know where to start:
for i := 0; i < len(c); i {
//don't include if there's a -1 between two numbers
if c[i] != 0 && c[i] != len(c) {
}
}
CodePudding user response:
Try this. Change the problem statement. The slice of int is OKAY
if the numbers that are not -1
are adjacent.
func check(input []int) bool {
lastNonNeg := -1
for i, n := range input {
if n != -1 {
// already encountered non negative and non adjacent (distance gt 1)
if lastNonNeg != -1 && i-lastNonNeg > 1 {
return false
}
// otherwise remember the index
lastNonNeg = i
}
}
return true
}
Input and output:
[1 3 -1 0 1 2] false
[-1 0 3 -1 1 3] false
[2 -1 -1 4 3 1] false
[1 -1 -1 -1 2 0] false
[-1 -1 0 -1 -1 -1] true
[-1 1 2 4 0 -1] true
[0 1 2 -1 -1 -1] true
[-1 -1 -1 2 2 1] true
Attention that input {-1, -1, -1, -1, -1, -1}
is OKAY
, but all mute chords is not difficult.
https://play.golang.org/p/Fzf8R4Seqan
CodePudding user response:
What if you put all values in a slice? numbers := strings.Split("1,2,3,4,5", ",")
then you can check where the particular thing is, and if it isn't the beginning, or the end, you might be good.