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.139 tracks/go/exercises/sublist/example.go
trackler-2.2.1.138 tracks/go/exercises/sublist/example.go
trackler-2.2.1.137 tracks/go/exercises/sublist/example.go
trackler-2.2.1.136 tracks/go/exercises/sublist/example.go
trackler-2.2.1.135 tracks/go/exercises/sublist/example.go
trackler-2.2.1.134 tracks/go/exercises/sublist/example.go
trackler-2.2.1.133 tracks/go/exercises/sublist/example.go
trackler-2.2.1.132 tracks/go/exercises/sublist/example.go
trackler-2.2.1.131 tracks/go/exercises/sublist/example.go
trackler-2.2.1.130 tracks/go/exercises/sublist/example.go
trackler-2.2.1.129 tracks/go/exercises/sublist/example.go
trackler-2.2.1.128 tracks/go/exercises/sublist/example.go
trackler-2.2.1.127 tracks/go/exercises/sublist/example.go
trackler-2.2.1.126 tracks/go/exercises/sublist/example.go
trackler-2.2.1.125 tracks/go/exercises/sublist/example.go
trackler-2.2.1.124 tracks/go/exercises/sublist/example.go
trackler-2.2.1.123 tracks/go/exercises/sublist/example.go
trackler-2.2.1.122 tracks/go/exercises/sublist/example.go
trackler-2.2.1.121 tracks/go/exercises/sublist/example.go
trackler-2.2.1.120 tracks/go/exercises/sublist/example.go