Advertisement
teknoraver

isogramma.go

Jan 21st, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.22 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strings"
  8. )
  9.  
  10. /*
  11.  * example run:
  12.  *
  13.  * go run isogram.go /usr/share/dict/italian |awk -F"'" '{if(length($1) > 9)print length($1), $1}' |sort -gr
  14.  */
  15.  
  16. func isIsogram(word string) bool {
  17.     myMap := make(map[rune]int, len(word))
  18.     for _, v := range word {
  19.         if _, p := myMap[v]; p {
  20.             myMap[v]++
  21.         } else {
  22.             myMap[v] = 1
  23.         }
  24.     }
  25.     last := myMap[rune(word[0])]
  26.     for _, v := range myMap {
  27.         if v != last {
  28.             return false
  29.         }
  30.         last = v
  31.     }
  32.  
  33.     return true
  34. }
  35.  
  36. func main() {
  37.     if len(os.Args) != 2 {
  38.         fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "<file>")
  39.         os.Exit(1)
  40.     }
  41.  
  42.     file, err := os.Open(os.Args[1])
  43.     if err != nil {
  44.         panic(err)
  45.     }
  46.  
  47.     scanner := bufio.NewScanner(file)
  48.     for scanner.Scan() {
  49.         word := scanner.Text()
  50.  
  51.         word_ascii := word
  52.         word_ascii = strings.Replace(word_ascii, "à", "a", -1)
  53.         word_ascii = strings.Replace(word_ascii, "è", "e", -1)
  54.         word_ascii = strings.Replace(word_ascii, "è", "e", -1)
  55.         word_ascii = strings.Replace(word_ascii, "ì", "i", -1)
  56.         word_ascii = strings.Replace(word_ascii, "ò", "o", -1)
  57.         word_ascii = strings.Replace(word_ascii, "ù", "u", -1)
  58.  
  59.         if isIsogram(word_ascii) {
  60.             fmt.Println(word)
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement