Files
bootdotdev-fcc-learn-golang…/course/9-maps/exercises/4-maps_count/complete.go
wagslane 9be3074de6 first
2023-05-01 15:25:27 -06:00

47 lines
900 B
Go

package main
import (
"crypto/md5"
"fmt"
"io"
)
func getCounts(userIDs []string) map[string]int {
m := map[string]int{}
for _, id := range userIDs {
if _, ok := m[id]; !ok {
m[id] = 0
}
m[id]++
}
return m
}
// don't edit below this line
func test(userIDs []string, ids []string) {
fmt.Printf("Generating counts for %v user IDs...\n", len(userIDs))
counts := getCounts(userIDs)
fmt.Println("Counts from select IDs:")
for _, k := range ids {
v := counts[k]
fmt.Printf(" - %s: %d\n", k, v)
}
fmt.Println("=====================================")
}
func main() {
userIDs := []string{}
for i := 0; i < 10000; i++ {
h := md5.New()
io.WriteString(h, fmt.Sprint(i))
key := fmt.Sprintf("%x", h.Sum(nil))
userIDs = append(userIDs, key[:2])
}
test(userIDs, []string{"00", "ff", "dd"})
test(userIDs, []string{"aa", "12", "32"})
test(userIDs, []string{"bb", "33"})
}