2 Redistribution and use in source and binary forms, with or without
3 modification, are permitted provided that the following conditions are met:
5 * Redistributions of source code must retain the above copyright
6 notice, this list of conditions and the following disclaimer.
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.
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.
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.
30 /* The Computer Language Benchmarks Game
31 * http://shootout.alioth.debian.org/
33 * contributed by The Go Authors.
47 func count(data
string, n
int) map[string]int {
48 counts
:= make(map[string]int)
50 for i
:= 0; i
<= top
; i
++ {
57 func countOne(data
string, s
string) int {
58 return count(data
, len(s
))[s
]
68 func (kn kNucArray
) Len() int { return len(kn
) }
69 func (kn kNucArray
) Swap(i
, j
int) { kn
[i
], kn
[j
] = kn
[j
], kn
[i
] }
70 func (kn kNucArray
) Less(i
, j
int) bool {
71 if kn
[i
].count
== kn
[j
].count
{
72 return kn
[i
].name
> kn
[j
].name
// sort down
74 return kn
[i
].count
> kn
[j
].count
77 func sortedArray(m
map[string]int) kNucArray
{
78 kn
:= make(kNucArray
, len(m
))
88 func printKnucs(a kNucArray
) {
90 for _
, kn
:= range a
{
93 for _
, kn
:= range a
{
94 fmt
.Printf("%s %.3f\n", kn
.name
, 100*float64(kn
.count
)/float64(sum
))
100 in
:= bufio
.NewReader(os
.Stdin
)
101 three
:= []byte(">THREE ")
103 line
, err
:= in
.ReadSlice('\n')
105 fmt
.Fprintln(os
.Stderr
, "ReadLine err:", err
)
108 if line
[0] == '>' && bytes
.Equal(line
[0:len(three
)], three
) {
112 data
, err
:= ioutil
.ReadAll(in
)
114 fmt
.Fprintln(os
.Stderr
, "ReadAll err:", err
)
117 // delete the newlines and convert to upper case
119 for i
:= 0; i
< len(data
); i
++ {
121 data
[j
] = data
[i
] &^ ' ' // upper case
125 str
:= string(data
[0:j
])
127 var arr1
, arr2 kNucArray
128 countsdone
:= make(chan bool)
130 arr1
= sortedArray(count(str
, 1))
134 arr2
= sortedArray(count(str
, 2))
138 interests
:= []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}
139 results
:= make([]chan string, len(interests
))
140 for i
, s
:= range interests
{
141 ch
:= make(chan string)
143 go func(result
chan string, ss
string) {
144 result
<- fmt
.Sprintf("%d %s\n", countOne(str
, ss
), ss
)
151 for _
, rc
:= range results
{