Reputation: 75
Using Go 1.11 I have 5 group of three numbers each in their own slice. I want to be able to compare that to a core data set.
Example data
My Groups: [[1 2 3] [4 9 2] [7 9 3] [4 7 5] [4 3 2]]
My Core Data: [5 9 7 3 2]
So I want to be able to match my core data up with any of the groups. As the example data shows, the core data does have 9, 7 and 3 so it should match the 3rd group.
But each time I try to loop, I am not getting the logic right.
Any help most welcome.
UPDATE
So this is the code I am currently working with
groupData := [][]int{{1,2,3}, {7,8,9}, {9,7,3}}
coreData := []int{5,9,7,3,2}
for _, data := range groupData {
fmt.Println( data )
fmt.Println( groupData )
fmt.Println( reflect.DeepEqual(data, coreData) )
}
This returns false all the time. Even on the last set of data 9,7,3
which you can see is contained within the codeData
var.
But even if I had a coreData
like []int{3,2,7,1,9}
that should still result in true, as it still has 3,7,9 and I want to it match that last group.
Hope that explains what I am looking to do more. Thanks.
Upvotes: 2
Views: 1033
Reputation: 4218
This is an update of my previous answer because you need a partial match.
As you want to have a partial match you cannot use deep equals.
But you can do something like this.
func main() {
groupData := [][]int{{1,2,3}, {7,8,9}, {9,7,3}}
coreData := []int{5,9,7,3,2}
// Loop over each group to check a partial match against core
for _, data := range groupData {
fmt.Println( check(data, coreData ))
}
}
func check(group, core[]int) bool {
// Loop over each group element to check if its available in core
for _, s := range group{
if contains(core, s) == false {
return false
}
}
return true
}
// Returns true if core contains the element
func contains(core []int, element int) bool {
for _, a := range core {
if a == element {
return true
}
}
return false
}
This result in :
false
false
true
Upvotes: 3