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

Version Path
trackler-2.2.1.180 tracks/go/exercises/sublist/example.go
trackler-2.2.1.179 tracks/go/exercises/sublist/example.go
trackler-2.2.1.178 tracks/go/exercises/sublist/example.go
trackler-2.2.1.177 tracks/go/exercises/sublist/example.go
trackler-2.2.1.176 tracks/go/exercises/sublist/example.go
trackler-2.2.1.175 tracks/go/exercises/sublist/example.go
trackler-2.2.1.174 tracks/go/exercises/sublist/example.go
trackler-2.2.1.173 tracks/go/exercises/sublist/example.go
trackler-2.2.1.172 tracks/go/exercises/sublist/example.go
trackler-2.2.1.171 tracks/go/exercises/sublist/example.go
trackler-2.2.1.170 tracks/go/exercises/sublist/example.go
trackler-2.2.1.169 tracks/go/exercises/sublist/example.go
trackler-2.2.1.167 tracks/go/exercises/sublist/example.go
trackler-2.2.1.166 tracks/go/exercises/sublist/example.go
trackler-2.2.1.165 tracks/go/exercises/sublist/example.go
trackler-2.2.1.164 tracks/go/exercises/sublist/example.go
trackler-2.2.1.163 tracks/go/exercises/sublist/example.go
trackler-2.2.1.162 tracks/go/exercises/sublist/example.go
trackler-2.2.1.161 tracks/go/exercises/sublist/example.go
trackler-2.2.1.160 tracks/go/exercises/sublist/example.go