Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | Redistribution and use in source and binary forms, with or without |
| 3 | modification, 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 | |
| 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | POSSIBILITY 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 | |
| 36 | package main |
| 37 | |
| 38 | import ( |
| 39 | "bufio"; |
| 40 | "bytes"; |
| 41 | "fmt"; |
Rob Pike | b0683bd | 2009-12-02 22:02:14 -0800 | [diff] [blame] | 42 | "io/ioutil"; |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 43 | "os"; |
| 44 | "sort"; |
| 45 | "strings"; |
| 46 | ) |
| 47 | |
| 48 | var in *bufio.Reader |
| 49 | |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 50 | func count(data string, n int) map[string]int { |
| 51 | counts := make(map[string]int); |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 52 | top := len(data) - n; |
| 53 | for i := 0; i <= top; i++ { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 54 | s := data[i : i+n]; |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 55 | if k, ok := counts[s]; ok { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 56 | counts[s] = k + 1 |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 57 | } else { |
| 58 | counts[s] = 1 |
| 59 | } |
| 60 | } |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 61 | return counts; |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | func countOne(data string, s string) int { |
| 65 | counts := count(data, len(s)); |
| 66 | if i, ok := counts[s]; ok { |
| 67 | return i |
| 68 | } |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 69 | return 0; |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | type kNuc struct { |
| 74 | name string; |
| 75 | count int; |
| 76 | } |
| 77 | |
| 78 | type kNucArray []kNuc |
| 79 | |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 80 | func (kn kNucArray) Len() int { return len(kn) } |
| 81 | func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] } |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 82 | func (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 Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 86 | return kn[i].count > kn[j].count; |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 89 | func sortedArray(m map[string]int) kNucArray { |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 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 | |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 101 | func print(m map[string]int) { |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 102 | a := sortedArray(m); |
| 103 | sum := 0; |
| 104 | for _, kn := range a { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 105 | sum += kn.count |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 106 | } |
| 107 | for _, kn := range a { |
Russ Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 108 | fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum)) |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | func main() { |
| 113 | in = bufio.NewReader(os.Stdin); |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 114 | three := strings.Bytes(">THREE "); |
| 115 | for { |
Russ Cox | 4b40928 | 2009-08-27 11:20:15 -0700 | [diff] [blame] | 116 | line, err := in.ReadSlice('\n'); |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 117 | 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 Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 122 | break |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Rob Pike | b0683bd | 2009-12-02 22:02:14 -0800 | [diff] [blame] | 125 | data, err := ioutil.ReadAll(in); |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 126 | 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 Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 135 | j++; |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 136 | } |
| 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 Cox | 19dae07 | 2009-11-20 13:11:42 -0800 | [diff] [blame] | 148 | fmt.Printf("%d %s\n", countOne(str, s), s) |
Rob Pike | ae3939c | 2009-08-06 13:00:26 -0700 | [diff] [blame] | 149 | } |
| 150 | } |