Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created March 9, 2017 10:03
Show Gist options
  • Save alextanhongpin/7f58df97d0ec5b7437e205c64effe702 to your computer and use it in GitHub Desktop.
Save alextanhongpin/7f58df97d0ec5b7437e205c64effe702 to your computer and use it in GitHub Desktop.
package main
// Run the following command
// $ time go run main.go
// Golang Benchmark
// real 0m8.102s
// user 0m1.004s
// sys 0m0.504s
// Python Benchmark
// real 0m0.782s
// user 0m0.617s
// sys 0m0.099s
import (
"bytes"
"fmt"
"io"
"os"
"strings"
)
func main() {
buf := bytes.NewBuffer(nil)
f, _ := os.Open("/usr/share/dict/words")
io.Copy(buf, f)
f.Close()
s := string(buf.Bytes())
data := strings.Split(s, "\n")
var isograms []string
for _, v := range data {
if IsIsogram(v) == true {
fmt.Println(v)
isograms = append(isograms, v)
}
}
fmt.Printf("%d isograms found!", len(isograms))
}
func IsIsogram(s string) bool {
if s == "" {
return false
}
// Create a map to store the unique characters
d := make(map[rune]int)
for _, r := range strings.ToLower(s) {
// Set unique characters
d[r] += 1
}
// Now we have a map of the unique characters
// In order to get an Array of the keys, we loop
var o []string
for k, _ := range d {
o = append(o, string(k))
}
// Array `o` now contains the unique strings
// Check how many times the strings are repeated
repeat := int(len(s) / len(o))
// Now check if each unique characters have the same amount
for _, v := range d {
if v != repeat {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment