blob: 47debecb3d0d7a577087ff87cb45b66c4816c89a [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";
Rob Pikeb0683bd2009-12-02 22:02:14 -080042 "io/ioutil";
Rob Pikeae3939c2009-08-06 13:00:26 -070043 "os";
44 "sort";
45 "strings";
46)
47
48var in *bufio.Reader
49
Russ Cox19dae072009-11-20 13:11:42 -080050func count(data string, n int) map[string]int {
51 counts := make(map[string]int);
Rob Pikeae3939c2009-08-06 13:00:26 -070052 top := len(data) - n;
53 for i := 0; i <= top; i++ {
Russ Cox19dae072009-11-20 13:11:42 -080054 s := data[i : i+n];
Rob Pikeae3939c2009-08-06 13:00:26 -070055 if k, ok := counts[s]; ok {
Russ Cox19dae072009-11-20 13:11:42 -080056 counts[s] = k + 1
Rob Pikeae3939c2009-08-06 13:00:26 -070057 } else {
58 counts[s] = 1
59 }
60 }
Russ Cox19dae072009-11-20 13:11:42 -080061 return counts;
Rob Pikeae3939c2009-08-06 13:00:26 -070062}
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 }
Russ Cox19dae072009-11-20 13:11:42 -080069 return 0;
Rob Pikeae3939c2009-08-06 13:00:26 -070070}
71
72
73type kNuc struct {
74 name string;
75 count int;
76}
77
78type kNucArray []kNuc
79
Russ Cox19dae072009-11-20 13:11:42 -080080func (kn kNucArray) Len() int { return len(kn) }
81func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
Rob Pikeae3939c2009-08-06 13:00:26 -070082func (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 }
Russ Cox19dae072009-11-20 13:11:42 -080086 return kn[i].count > kn[j].count;
Rob Pikeae3939c2009-08-06 13:00:26 -070087}
88
Russ Cox19dae072009-11-20 13:11:42 -080089func sortedArray(m map[string]int) kNucArray {
Rob Pikeae3939c2009-08-06 13:00:26 -070090 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
Russ Cox19dae072009-11-20 13:11:42 -0800101func print(m map[string]int) {
Rob Pikeae3939c2009-08-06 13:00:26 -0700102 a := sortedArray(m);
103 sum := 0;
104 for _, kn := range a {
Russ Cox19dae072009-11-20 13:11:42 -0800105 sum += kn.count
Rob Pikeae3939c2009-08-06 13:00:26 -0700106 }
107 for _, kn := range a {
Russ Cox19dae072009-11-20 13:11:42 -0800108 fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
Rob Pikeae3939c2009-08-06 13:00:26 -0700109 }
110}
111
112func main() {
113 in = bufio.NewReader(os.Stdin);
Rob Pikeae3939c2009-08-06 13:00:26 -0700114 three := strings.Bytes(">THREE ");
115 for {
Russ Cox4b409282009-08-27 11:20:15 -0700116 line, err := in.ReadSlice('\n');
Rob Pikeae3939c2009-08-06 13:00:26 -0700117 if err != nil {
118 fmt.Fprintln(os.Stderr, "ReadLine err:", err);
119 os.Exit(2);
120 }
121 if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
Russ Cox19dae072009-11-20 13:11:42 -0800122 break
Rob Pikeae3939c2009-08-06 13:00:26 -0700123 }
124 }
Rob Pikeb0683bd2009-12-02 22:02:14 -0800125 data, err := ioutil.ReadAll(in);
Rob Pikeae3939c2009-08-06 13:00:26 -0700126 if err != nil {
127 fmt.Fprintln(os.Stderr, "ReadAll err:", err);
128 os.Exit(2);
129 }
130 // delete the newlines and convert to upper case
131 j := 0;
132 for i := 0; i < len(data); i++ {
133 if data[i] != '\n' {
134 data[j] = data[i] &^ ' '; // upper case
Russ Cox19dae072009-11-20 13:11:42 -0800135 j++;
Rob Pikeae3939c2009-08-06 13:00:26 -0700136 }
137 }
138 str := string(data[0:j]);
139
140 print(count(str, 1));
141 fmt.Print("\n");
142
143 print(count(str, 2));
144 fmt.Print("\n");
145
146 interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"};
147 for _, s := range interests {
Russ Cox19dae072009-11-20 13:11:42 -0800148 fmt.Printf("%d %s\n", countOne(str, s), s)
Rob Pikeae3939c2009-08-06 13:00:26 -0700149 }
150}