Sha256: 83d7745209dfef9eb68808efa0194ecc31bc7a6e5571efb7f625edcce91920f7
Contents?: true
Size: 990 Bytes
Versions: 122
Compression:
Stored size: 990 Bytes
Contents
package sublist import "reflect" // Relation is the comparison between lists type Relation string // Possible relations const ( RelationEqual Relation = "equal" RelationSublist Relation = "sublist" RelationSuperlist Relation = "superlist" RelationUnequal Relation = "unequal" ) // Sublist checks difference of two lists and // returns equal, sublist, superlist or unequal according // to their relation to each other. func Sublist(l1, l2 []int) Relation { if reflect.DeepEqual(l1, l2) { return RelationEqual } else if contains(l1, l2) { return RelationSuperlist } else if contains(l2, l1) { return RelationSublist } return RelationUnequal } func contains(l1, l2 []int) bool { if len(l2) == 0 { return true } else if len(l2) > len(l1) { return false } for i := 0; i <= len(l1)-len(l2); i++ { if l1[i] != l2[0] { continue } rest := true for j, e := range l2 { rest = rest && l1[i+j] == e } if rest { return true } } return false }
Version data entries
122 entries across 122 versions & 1 rubygems