Skip to content

Commit 9803284

Browse files
committed
initial commit
0 parents  commit 9803284

File tree

9 files changed

+2677
-0
lines changed

9 files changed

+2677
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Peter C.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Algorithms
2+
3+
Collection of algorithm implementations for Go.
4+
5+
## Error detection
6+
7+
[Damm](https://github.com/petoc/algo/tree/master/damm)<br>
8+
[Damm64](https://github.com/petoc/algo/tree/master/damm64) (using quasigroups for character base from 3 to 64 characters)<br>
9+
[Luhn](https://github.com/petoc/algo/tree/master/luhn)<br>
10+
11+
12+
## Usage
13+
14+
### Damm
15+
16+
Implementation of error detection algorithm for numeric codes from H. Michael Damm.
17+
18+
```go
19+
import "github.com/petoc/algo/damm"
20+
```
21+
22+
```go
23+
damm.Calculate("123456789") // 4
24+
damm.Validate("1234567894") // true
25+
```
26+
27+
### Damm64
28+
29+
Based on error detection algorithm from H. Michael Damm. Uses pre-generated quasigroups for character base from 3 to 64 characters.
30+
31+
```go
32+
import "github.com/petoc/algo/damm64"
33+
```
34+
35+
```go
36+
base := "0123456789ABCDEFGHIJKLMNOPQRSTUV"
37+
damm64.Calculate(base, "G12Q") // F
38+
damm64.Validate(base, "G12QF") // true
39+
```
40+
### Luhn
41+
42+
Implementation of error detection algorithm for numeric codes from Hans Peter Luhn.
43+
44+
```go
45+
import "github.com/petoc/algo/luhn"
46+
```
47+
48+
```go
49+
luhn.Calculate("123456789") // 7
50+
luhn.Validate("1234567897") // true
51+
```
52+
53+
## Sources
54+
55+
Damm Quasigroups (http://www.md-software.de/math/DAMM_Quasigruppen.txt)
56+
57+
## License
58+
59+
Licensed under MIT license.

damm/damm.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package damm
2+
3+
var table = [100]int8{
4+
0, 3, 1, 7, 5, 9, 8, 6, 4, 2,
5+
7, 0, 9, 2, 1, 5, 4, 8, 6, 3,
6+
4, 2, 0, 6, 8, 7, 1, 3, 5, 9,
7+
1, 7, 5, 0, 9, 8, 3, 4, 2, 6,
8+
6, 1, 2, 3, 0, 4, 5, 9, 7, 8,
9+
3, 6, 7, 4, 2, 0, 9, 5, 8, 1,
10+
5, 8, 6, 9, 7, 2, 0, 1, 3, 4,
11+
8, 9, 4, 5, 3, 6, 2, 0, 1, 7,
12+
9, 4, 3, 8, 6, 1, 7, 2, 0, 5,
13+
2, 5, 8, 1, 4, 3, 6, 7, 9, 0,
14+
}
15+
16+
func id(i, d int8) int8 {
17+
return table[i*10+d]
18+
}
19+
20+
// Calculate ...
21+
func Calculate(s string) int8 {
22+
var i int8
23+
for _, r := range s[:] {
24+
d := int8(r - '0')
25+
if d < 0 || d > 9 {
26+
return -1
27+
}
28+
i = id(i, d)
29+
}
30+
return i
31+
}
32+
33+
// Validate ...
34+
func Validate(s string) bool {
35+
return Calculate(s) == 0
36+
}

damm/damm_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package damm
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDamm(t *testing.T) {
8+
tt := map[string]bool{
9+
"1234567890": false,
10+
"1234567894": true,
11+
"2345678945": true,
12+
"3456789452": true,
13+
"4567894520": false,
14+
}
15+
for s, e := range tt {
16+
c := int8(s[len(s)-1] - '0')
17+
r := Calculate(s[0 : len(s)-1])
18+
if (c == r) != e {
19+
t.Errorf("%s: expected %v, got %v", s, c, r)
20+
return
21+
}
22+
g := Validate(s)
23+
if g != e {
24+
t.Errorf("%s: expected %v, got %v", s, e, g)
25+
return
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)