Sha256: 1788f85c7ea08a739f5fb816e83c33c8121689aeb144ebeebb663134588280c4

Contents?: true

Size: 484 Bytes

Versions: 161

Compression:

Stored size: 484 Bytes

Contents

package luhn

import "strings"

const testVersion = 2

func Valid(id string) bool {

	if len(strings.TrimSpace(id)) == 1 {
		return false
	}

	d := make([]int, 0, len(id))

	for _, r := range id {
		if r == ' ' {
			continue
		}
		if r < '0' || r > '9' {
			return false
		}
		d = append(d, int(r-'0'))
	}

	return sum(d)%10 == 0
}

func sum(d []int) (s int) {
	for i, x := range d {
		j := len(d) - i
		if j%2 == 0 {
			x *= 2
			if x > 9 {
				x -= 9
			}
		}
		s += x
	}
	return
}

Version data entries

161 entries across 161 versions & 1 rubygems

Version Path
trackler-2.2.0.3 tracks/go/exercises/luhn/example.go
trackler-2.2.0.2 tracks/go/exercises/luhn/example.go
trackler-2.2.0.1 tracks/go/exercises/luhn/example.go
trackler-2.2.0.0 tracks/go/exercises/luhn/example.go
trackler-2.1.0.55 tracks/go/exercises/luhn/example.go
trackler-2.1.0.54 tracks/go/exercises/luhn/example.go
trackler-2.1.0.53 tracks/go/exercises/luhn/example.go
trackler-2.1.0.52 tracks/go/exercises/luhn/example.go
trackler-2.1.0.51 tracks/go/exercises/luhn/example.go
trackler-2.1.0.50 tracks/go/exercises/luhn/example.go
trackler-2.1.0.49 tracks/go/exercises/luhn/example.go
trackler-2.1.0.48 tracks/go/exercises/luhn/example.go
trackler-2.1.0.47 tracks/go/exercises/luhn/example.go
trackler-2.1.0.46 tracks/go/exercises/luhn/example.go
trackler-2.1.0.45 tracks/go/exercises/luhn/example.go
trackler-2.1.0.44 tracks/go/exercises/luhn/example.go
trackler-2.1.0.43 tracks/go/exercises/luhn/example.go
trackler-2.1.0.42 tracks/go/exercises/luhn/example.go
trackler-2.1.0.41 tracks/go/exercises/luhn/example.go
trackler-2.1.0.40 tracks/go/exercises/luhn/example.go