blob: 2962a6900955e921760565621a1955039ca26f2f [file] [log] [blame]
Andrew Gerrande3933f02016-01-14 15:01:18 +11001// +build ignore
2
Russ Coxa87cfef2016-01-06 12:26:18 -05003/*
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 * Neither the name of "The Computer Language Benchmarks Game" nor the
15 name of "The Computer Language Shootout Benchmarks" nor the names of
16 its contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29POSSIBILITY OF SUCH DAMAGE.
30*/
31
32/* The Computer Language Benchmarks Game
33 * http://shootout.alioth.debian.org/
34 *
35 * contributed by The Go Authors.
36 */
37
38package main
39
40import (
41 "bufio"
42 "bytes"
43 "fmt"
44 "io/ioutil"
45 "os"
46 "sort"
47)
48
49var in *bufio.Reader
50
51func count(data string, n int) map[string]int {
52 counts := make(map[string]int)
53 top := len(data) - n
54 for i := 0; i <= top; i++ {
55 s := data[i : i+n]
56 counts[s]++
57 }
58 return counts
59}
60
61func countOne(data string, s string) int {
62 return count(data, len(s))[s]
63}
64
65type kNuc struct {
66 name string
67 count int
68}
69
70type kNucArray []kNuc
71
72func (kn kNucArray) Len() int { return len(kn) }
73func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
74func (kn kNucArray) Less(i, j int) bool {
75 if kn[i].count == kn[j].count {
76 return kn[i].name > kn[j].name // sort down
77 }
78 return kn[i].count > kn[j].count
79}
80
81func sortedArray(m map[string]int) kNucArray {
82 kn := make(kNucArray, len(m))
83 i := 0
84 for k, v := range m {
85 kn[i].name = k
86 kn[i].count = v
87 i++
88 }
89 sort.Sort(kn)
90 return kn
91}
92
93func print(m map[string]int) {
94 a := sortedArray(m)
95 sum := 0
96 for _, kn := range a {
97 sum += kn.count
98 }
99 for _, kn := range a {
100 fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
101 }
102}
103
104func main() {
105 in = bufio.NewReader(os.Stdin)
106 three := []byte(">THREE ")
107 for {
108 line, err := in.ReadSlice('\n')
109 if err != nil {
110 fmt.Fprintln(os.Stderr, "ReadLine err:", err)
111 os.Exit(2)
112 }
113 if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
114 break
115 }
116 }
117 data, err := ioutil.ReadAll(in)
118 if err != nil {
119 fmt.Fprintln(os.Stderr, "ReadAll err:", err)
120 os.Exit(2)
121 }
122 // delete the newlines and convert to upper case
123 j := 0
124 for i := 0; i < len(data); i++ {
125 if data[i] != '\n' {
126 data[j] = data[i] &^ ' ' // upper case
127 j++
128 }
129 }
130 str := string(data[0:j])
131
132 print(count(str, 1))
133 fmt.Print("\n")
134
135 print(count(str, 2))
136 fmt.Print("\n")
137
138 interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}
139 for _, s := range interests {
140 fmt.Printf("%d %s\n", countOne(str, s), s)
141 }
142}