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" 42 "io/ioutil" 43 "os" 44 "sort" 45 ) 46 47 var in *bufio.Reader 48 49 func count(data string, n int) map[string]int { 50 counts := make(map[string]int) 51 top := len(data) - n 52 for i := 0; i <= top; i++ { 53 s := data[i : i+n] 54 counts[s]++ 55 } 56 return counts 57 } 58 59 func countOne(data string, s string) int { 60 return count(data, len(s))[s] 61 } 62 63 type kNuc struct { 64 name string 65 count int 66 } 67 68 type kNucArray []kNuc 69 70 func (kn kNucArray) Len() int { return len(kn) } 71 func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] } 72 func (kn kNucArray) Less(i, j int) bool { 73 if kn[i].count == kn[j].count { 74 return kn[i].name > kn[j].name // sort down 75 } 76 return kn[i].count > kn[j].count 77 } 78 79 func sortedArray(m map[string]int) kNucArray { 80 kn := make(kNucArray, len(m)) 81 i := 0 82 for k, v := range m { 83 kn[i].name = k 84 kn[i].count = v 85 i++ 86 } 87 sort.Sort(kn) 88 return kn 89 } 90 91 func print(m map[string]int) { 92 a := sortedArray(m) 93 sum := 0 94 for _, kn := range a { 95 sum += kn.count 96 } 97 for _, kn := range a { 98 fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum)) 99 } 100 } 101 102 func main() { 103 in = bufio.NewReader(os.Stdin) 104 three := []byte(">THREE ") 105 for { 106 line, err := in.ReadSlice('\n') 107 if err != nil { 108 fmt.Fprintln(os.Stderr, "ReadLine err:", err) 109 os.Exit(2) 110 } 111 if line[0] == '>' && bytes.Equal(line[0:len(three)], three) { 112 break 113 } 114 } 115 data, err := ioutil.ReadAll(in) 116 if err != nil { 117 fmt.Fprintln(os.Stderr, "ReadAll err:", err) 118 os.Exit(2) 119 } 120 // delete the newlines and convert to upper case 121 j := 0 122 for i := 0; i < len(data); i++ { 123 if data[i] != '\n' { 124 data[j] = data[i] &^ ' ' // upper case 125 j++ 126 } 127 } 128 str := string(data[0:j]) 129 130 print(count(str, 1)) 131 fmt.Print("\n") 132 133 print(count(str, 2)) 134 fmt.Print("\n") 135 136 interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"} 137 for _, s := range interests { 138 fmt.Printf("%d %s\n", countOne(str, s), s) 139 } 140 } 141