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