-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path739.go
60 lines (53 loc) · 1.21 KB
/
739.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// UVa 739 - Soundex Indexing
package main
import (
"fmt"
"os"
"strings"
)
var codeMap = map[byte]string{
'B': "1", 'P': "1", 'F': "1", 'V': "1",
'C': "2", 'S': "2", 'K': "2", 'G': "2", 'J': "2", 'Q': "2", 'X': "2", 'Z': "2",
'D': "3", 'T': "3",
'L': "4",
'M': "5", 'N': "5",
'R': "6",
}
func encode(name string) string {
skipped := false
soundex := name[0:1]
for i := 1; i < len(name); i++ {
if code := codeMap[name[i]]; code != "" && code != soundex[len(soundex)-1:] {
if len(soundex) == 1 && code == codeMap[name[0]] && !skipped {
skipped = true
continue
}
if soundex += code; len(soundex) == 4 {
break
}
}
}
if len(soundex) < 4 {
soundex += strings.Repeat("0", 4-len(soundex))
}
return soundex
}
func main() {
in, _ := os.Open("739.in")
defer in.Close()
out, _ := os.Create("739.out")
defer out.Close()
fmt.Fprintln(out, " NAME SOUNDEX CODE")
var name string
for {
if _, err := fmt.Fscanf(in, "%s", &name); err != nil {
break
}
encoded := encode(name)
if len(name) < 25 {
name += strings.Repeat(" ", 25-len(name))
}
fmt.Fprintf(out, " %s%s\n", name, encoded)
}
fmt.Fprintln(out, " END OF OUTPUT")
}