Browse Source

move sort func into separate file

master
Stephen McQuay 4 years ago
parent
commit
6e3a40dd04
No known key found for this signature in database GPG Key ID: 1ABF428F71BAFC3D
2 changed files with 23 additions and 23 deletions
  1. +7
    -23
      main.go
  2. +16
    -0
      sort.go

+ 7
- 23
main.go View File

@ -32,37 +32,21 @@ func main() {
}
}
type letter struct {
r rune
count int
}
func (l letter) String() string {
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
type letter struct {
r rune
count int
}
func (l letter) String() string {
return fmt.Sprintf("%v: %d", string(l.r), l.count)
}
func freq(in string) histogram {

+ 16
- 0
sort.go View 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…
Cancel
Save