Skip to content

Commit

Permalink
UVa 739 - Soundex Indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
codingsince1985 committed Sep 9, 2016
1 parent 455c64e commit 9d43cef
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 739/739.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// UVa 739 - Soundex Indexing

package main

import (
"fmt"
"os"
)

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++ {
code := codeMap[name[i]]
if code == "" || code == soundex[len(soundex)-1:] {
continue
}
if len(soundex) == 1 && code == codeMap[name[0]] && !skipped {
skipped = true
continue
}
soundex += code
if len(soundex) == 4 {
break
}
}
for len(soundex) < 4 {
soundex += "0"
}
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)
for len(name) < 25 {
name += " "
}
fmt.Fprintf(out, " %s%s\n", name, encoded)
}
fmt.Fprintln(out, " END OF OUTPUT")
}
6 changes: 6 additions & 0 deletions 739/739.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LEE
KUHNE
EBELL
EBELSON
SCHAEFER
SCHAAK
8 changes: 8 additions & 0 deletions 739/739.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
NAME SOUNDEX CODE
LEE L000
KUHNE K500
EBELL E140
EBELSON E142
SCHAEFER S160
SCHAAK S200
END OF OUTPUT
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ UVa 713 - Adding Reversed Numbers
UVa 725 - Division
UVa 727 - Equation
UVa 729 - The Hamming Distance Problem
UVa 739 - Soundex Indexing
UVa 740 - Baudot Data Communication Code
UVa 750 - 8 Queens Chess Problem
UVa 755 - 487--3279
Expand Down

0 comments on commit 9d43cef

Please sign in to comment.