move sort func into separate file
This commit is contained in:
parent
d61956e0fa
commit
6e3a40dd04
32
main.go
32
main.go
@ -32,6 +32,14 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type result struct {
|
||||||
|
name string
|
||||||
|
h histogram
|
||||||
|
}
|
||||||
|
type results []result
|
||||||
|
|
||||||
|
type histogram []letter
|
||||||
|
|
||||||
type letter struct {
|
type letter struct {
|
||||||
r rune
|
r rune
|
||||||
count int
|
count int
|
||||||
@ -41,30 +49,6 @@ func (l letter) String() string {
|
|||||||
return fmt.Sprintf("%v: %d", string(l.r), l.count)
|
return fmt.Sprintf("%v: %d", string(l.r), l.count)
|
||||||
}
|
}
|
||||||
|
|
||||||
type result struct {
|
|
||||||
name string
|
|
||||||
h histogram
|
|
||||||
}
|
|
||||||
|
|
||||||
type results []result
|
|
||||||
|
|
||||||
func (r results) Len() int { return len(r) }
|
|
||||||
func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
||||||
func (r results) Less(i, j int) bool {
|
|
||||||
if len(r[i].h) == 0 || len(r[j].h) == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return r[i].h[0].count > r[j].h[0].count
|
|
||||||
}
|
|
||||||
|
|
||||||
type histogram []letter
|
|
||||||
|
|
||||||
func (n histogram) Len() int { return len(n) }
|
|
||||||
func (n histogram) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
|
||||||
func (n histogram) Less(i, j int) bool {
|
|
||||||
return n[i].count > n[j].count
|
|
||||||
}
|
|
||||||
|
|
||||||
func freq(in string) histogram {
|
func freq(in string) histogram {
|
||||||
m := map[rune]int{}
|
m := map[rune]int{}
|
||||||
for _, r := range in {
|
for _, r := range in {
|
||||||
|
16
sort.go
Normal file
16
sort.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func (r results) Len() int { return len(r) }
|
||||||
|
func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
||||||
|
func (r results) Less(i, j int) bool {
|
||||||
|
if len(r[i].h) == 0 || len(r[j].h) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return r[i].h[0].count > r[j].h[0].count
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n histogram) Len() int { return len(n) }
|
||||||
|
func (n histogram) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
|
||||||
|
func (n histogram) Less(i, j int) bool {
|
||||||
|
return n[i].count > n[j].count
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user