nfreq is a little program that answers the question "who has the most repeated letters in their name?"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

67 lines
918 B

package main
import (
"bufio"
"fmt"
"os"
"sort"
)
func main() {
s := bufio.NewScanner(os.Stdin)
max := 0
r := results{}
for s.Scan() {
if len(s.Text()) == 0 {
continue
}
if len(s.Text()) > max {
max = len(s.Text())
}
r = append(r, result{s.Text(), freq(s.Text())})
}
if err := s.Err(); err != nil {
panic(err)
}
sort.Sort(r)
f := fmt.Sprintf("%%%ds: %%v", max)
for _, r := range r {
fmt.Printf(f+"\n", r.name, r.h)
}
}
type result struct {
name string
h histogram
}
type results []result
type histogram []letter
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 {
m := map[rune]int{}
for _, r := range in {
if r == ' ' {
continue
}
m[r]++
}
r := histogram{}
for k, v := range m {
r = append(r, letter{k, v})
}
sort.Sort(r)
return r
}