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