Skip to content

Instantly share code, notes, and snippets.

@teknoraver teknoraver/isogram.go
Last active Mar 27, 2017

Embed
What would you like to do?
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
/*
* example run:
*
* go run isogram.go /usr/share/dict/italian |awk -F"'" '{if(length($1) > 9)print length($1), $1}' |sort -gr
*/
func isIsogram(word string) bool {
myMap := make(map[rune]int, len(word))
for _, v := range word {
if _, p := myMap[v]; p {
myMap[v]++
} else {
myMap[v] = 1
}
}
last := myMap[rune(word[0])]
for _, v := range myMap {
if v != last {
return false
}
last = v
}
return true
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "<file>")
os.Exit(1)
}
file, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
word := scanner.Text()
word_ascii := word
word_ascii = strings.Replace(word_ascii, "à", "a", -1)
word_ascii = strings.Replace(word_ascii, "è", "e", -1)
word_ascii = strings.Replace(word_ascii, "è", "e", -1)
word_ascii = strings.Replace(word_ascii, "ì", "i", -1)
word_ascii = strings.Replace(word_ascii, "ò", "o", -1)
word_ascii = strings.Replace(word_ascii, "ù", "u", -1)
if isIsogram(word_ascii) {
fmt.Println(word)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.