-
Notifications
You must be signed in to change notification settings - Fork 0
/
shifty.go
48 lines (44 loc) · 837 Bytes
/
shifty.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
// shifty prints the number of characters in files that require pressing the
// shift key, and the ratio of unshifted to shifted characters.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
func main() {
var yes, no int
for _, file := range os.Args[1:] {
y, n := shiftedChars(file)
yes += y
no += n
}
fmt.Println(yes, no, float64(no)/float64(yes))
}
func shiftedChars(file string) (yes, no int) {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
for _, c := range data {
if shiftedChar(c) {
yes++
} else {
no++
}
}
return
}
func shiftedChar(c byte) bool {
if 'A' <= c && c <= 'Z' {
return true
}
switch c {
case '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+':
return true
case '{', '}', '|', ':', '"', '<', '>', '?':
return true
}
return false
}