-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path401.go
59 lines (51 loc) · 1.35 KB
/
401.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
// UVa 401 - Palindromes
package main
import (
"fmt"
"os"
)
var dict = func() map[byte]byte {
srcBytes := []byte{'A', 'E', 'H', 'I', 'J', 'L', 'M', 'O', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '5', '8'}
dstBytes := []byte{'A', '3', 'H', 'I', 'L', 'J', 'M', 'O', '2', 'T', 'U', 'V', 'W', 'X', 'Y', '5', '1', 'S', 'E', 'Z', '8'}
dict := make(map[byte]byte)
for i := range srcBytes {
dict[srcBytes[i]] = dstBytes[i]
}
return dict
}()
var isPalindromic = func(a, b byte) bool { return a == b }
var isMirrored = func(a, b byte) bool { return dict[a] == b }
func testIf(str string, testMode func(a, b byte) bool) bool {
size := len(str)
half := size / 2
for i := 0; i < half; i++ {
if !testMode(str[i], str[size-1-i]) {
return false
}
}
return true
}
func main() {
in, _ := os.Open("401.in")
defer in.Close()
out, _ := os.Create("401.out")
defer out.Close()
var line string
for {
if _, err := fmt.Fscanf(in, "%s", &line); err != nil {
break
}
fmt.Fprintf(out, "%s -- ", line)
switch p, m := testIf(line, isPalindromic), testIf(line, isMirrored); {
case !p && !m:
fmt.Fprintln(out, "is not a palindrome.")
case p && !m:
fmt.Fprintln(out, "is a regular palindrome.")
case !p && m:
fmt.Fprintln(out, "is a mirrored string.")
default:
fmt.Fprintln(out, "is a mirrored palindrome.")
}
fmt.Fprintln(out)
}
}