2009-08-06 14:00:26 -06:00
|
|
|
/*
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of "The Computer Language Benchmarks Game" nor the
|
|
|
|
name of "The Computer Language Shootout Benchmarks" nor the names of
|
|
|
|
its contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The Computer Language Benchmarks Game
|
|
|
|
* http://shootout.alioth.debian.org/
|
|
|
|
*
|
|
|
|
* contributed by The Go Authors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio";
|
|
|
|
"bytes";
|
|
|
|
"fmt";
|
2009-12-02 23:02:14 -07:00
|
|
|
"io/ioutil";
|
2009-08-06 14:00:26 -06:00
|
|
|
"os";
|
|
|
|
"sort";
|
|
|
|
"strings";
|
|
|
|
)
|
|
|
|
|
|
|
|
var in *bufio.Reader
|
|
|
|
|
2009-11-20 14:11:42 -07:00
|
|
|
func count(data string, n int) map[string]int {
|
|
|
|
counts := make(map[string]int);
|
2009-08-06 14:00:26 -06:00
|
|
|
top := len(data) - n;
|
|
|
|
for i := 0; i <= top; i++ {
|
2009-11-20 14:11:42 -07:00
|
|
|
s := data[i : i+n];
|
2009-08-06 14:00:26 -06:00
|
|
|
if k, ok := counts[s]; ok {
|
2009-11-20 14:11:42 -07:00
|
|
|
counts[s] = k + 1
|
2009-08-06 14:00:26 -06:00
|
|
|
} else {
|
|
|
|
counts[s] = 1
|
|
|
|
}
|
|
|
|
}
|
2009-11-20 14:11:42 -07:00
|
|
|
return counts;
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func countOne(data string, s string) int {
|
|
|
|
counts := count(data, len(s));
|
|
|
|
if i, ok := counts[s]; ok {
|
|
|
|
return i
|
|
|
|
}
|
2009-11-20 14:11:42 -07:00
|
|
|
return 0;
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type kNuc struct {
|
|
|
|
name string;
|
|
|
|
count int;
|
|
|
|
}
|
|
|
|
|
|
|
|
type kNucArray []kNuc
|
|
|
|
|
2009-11-20 14:11:42 -07:00
|
|
|
func (kn kNucArray) Len() int { return len(kn) }
|
|
|
|
func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
|
2009-08-06 14:00:26 -06:00
|
|
|
func (kn kNucArray) Less(i, j int) bool {
|
|
|
|
if kn[i].count == kn[j].count {
|
|
|
|
return kn[i].name > kn[j].name // sort down
|
|
|
|
}
|
2009-11-20 14:11:42 -07:00
|
|
|
return kn[i].count > kn[j].count;
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
|
2009-11-20 14:11:42 -07:00
|
|
|
func sortedArray(m map[string]int) kNucArray {
|
2009-08-06 14:00:26 -06:00
|
|
|
kn := make(kNucArray, len(m));
|
|
|
|
i := 0;
|
|
|
|
for k, v := range m {
|
|
|
|
kn[i].name = k;
|
|
|
|
kn[i].count = v;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
sort.Sort(kn);
|
|
|
|
return kn;
|
|
|
|
}
|
|
|
|
|
2009-11-20 14:11:42 -07:00
|
|
|
func print(m map[string]int) {
|
2009-08-06 14:00:26 -06:00
|
|
|
a := sortedArray(m);
|
|
|
|
sum := 0;
|
|
|
|
for _, kn := range a {
|
2009-11-20 14:11:42 -07:00
|
|
|
sum += kn.count
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
for _, kn := range a {
|
2009-11-20 14:11:42 -07:00
|
|
|
fmt.Printf("%s %.3f\n", kn.name, 100*float64(kn.count)/float64(sum))
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
in = bufio.NewReader(os.Stdin);
|
|
|
|
three := strings.Bytes(">THREE ");
|
|
|
|
for {
|
2009-08-27 12:20:15 -06:00
|
|
|
line, err := in.ReadSlice('\n');
|
2009-08-06 14:00:26 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "ReadLine err:", err);
|
|
|
|
os.Exit(2);
|
|
|
|
}
|
|
|
|
if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
|
2009-11-20 14:11:42 -07:00
|
|
|
break
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-02 23:02:14 -07:00
|
|
|
data, err := ioutil.ReadAll(in);
|
2009-08-06 14:00:26 -06:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "ReadAll err:", err);
|
|
|
|
os.Exit(2);
|
|
|
|
}
|
|
|
|
// delete the newlines and convert to upper case
|
|
|
|
j := 0;
|
|
|
|
for i := 0; i < len(data); i++ {
|
|
|
|
if data[i] != '\n' {
|
|
|
|
data[j] = data[i] &^ ' '; // upper case
|
2009-11-20 14:11:42 -07:00
|
|
|
j++;
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
str := string(data[0:j]);
|
|
|
|
|
|
|
|
print(count(str, 1));
|
|
|
|
fmt.Print("\n");
|
|
|
|
|
|
|
|
print(count(str, 2));
|
|
|
|
fmt.Print("\n");
|
|
|
|
|
|
|
|
interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"};
|
|
|
|
for _, s := range interests {
|
2009-11-20 14:11:42 -07:00
|
|
|
fmt.Printf("%d %s\n", countOne(str, s), s)
|
2009-08-06 14:00:26 -06:00
|
|
|
}
|
|
|
|
}
|