Initial Commit
This commit is contained in:
22
go/parallel-letter-frequency/README.md
Normal file
22
go/parallel-letter-frequency/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Parallel Letter Frequency
|
||||
|
||||
Write a program that counts the frequency of letters in texts using parallel computation.
|
||||
|
||||
Parallelism is about doing things in parallel that can also be done
|
||||
sequentially. A common example is counting the frequency of letters.
|
||||
Create a function that returns the total frequency of each letter in a
|
||||
list of texts and that employs parallelism.
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
[view source]()
|
27
go/parallel-letter-frequency/by_the_letter.go
Normal file
27
go/parallel-letter-frequency/by_the_letter.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package letter
|
||||
|
||||
import "time"
|
||||
|
||||
// ConcurrentFrequency concurrently calls Frequency
|
||||
// and returns the final Frequency Map
|
||||
func ConcurrentFrequency(input []string) FreqMap {
|
||||
var procCnt = len(input)
|
||||
var toMerge []FreqMap
|
||||
for _, chunk := range input {
|
||||
go func(chunk string) {
|
||||
toMerge = append(toMerge, Frequency(chunk))
|
||||
procCnt--
|
||||
}(chunk)
|
||||
}
|
||||
for procCnt > 0 {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
// We should have all FreqMaps now... merge them
|
||||
ret := make(map[rune]int)
|
||||
for _, aResult := range toMerge {
|
||||
for k, v := range aResult {
|
||||
ret[k] += v
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
11
go/parallel-letter-frequency/frequency.go
Normal file
11
go/parallel-letter-frequency/frequency.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package letter
|
||||
|
||||
type FreqMap map[rune]int
|
||||
|
||||
func Frequency(s string) FreqMap {
|
||||
m := FreqMap{}
|
||||
for _, r := range s {
|
||||
m[r]++
|
||||
}
|
||||
return m
|
||||
}
|
30
go/parallel-letter-frequency/letter.go
Normal file
30
go/parallel-letter-frequency/letter.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package letter
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// MyConcurrentFrequency ...
|
||||
func MyConcurrentFrequency(input []string) FreqMap {
|
||||
var ProcMap = struct {
|
||||
sync.RWMutex
|
||||
m map[rune]int
|
||||
}{m: make(map[rune]int)}
|
||||
|
||||
var procCnt = len(input)
|
||||
for _, chunk := range input {
|
||||
go func(chunk string) {
|
||||
for _, rn := range chunk {
|
||||
ProcMap.Lock()
|
||||
ProcMap.m[rn]++
|
||||
ProcMap.Unlock()
|
||||
}
|
||||
procCnt--
|
||||
}(chunk)
|
||||
}
|
||||
for procCnt > 0 {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
return ProcMap.m
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
package letter
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// In the separate file frequency.go, you are given a function, Frequency(),
|
||||
// to sequentially count letter frequencies in a single text.
|
||||
// Perform this exercise on parallelism using Go concurrency features.
|
||||
// Make concurrent calls to Frequency and combine results to obtain the answer.
|
||||
|
||||
func TestConcurrentFrequency(t *testing.T) {
|
||||
seq := Frequency(euro + dutch + us)
|
||||
con := ConcurrentFrequency([]string{euro, dutch, us})
|
||||
if !reflect.DeepEqual(con, seq) {
|
||||
t.Fatal("ConcurrentFrequency wrong result")
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
euro = `Freude schöner Götterfunken
|
||||
Tochter aus Elysium,
|
||||
Wir betreten feuertrunken,
|
||||
Himmlische, dein Heiligtum!
|
||||
Deine Zauber binden wieder
|
||||
Was die Mode streng geteilt;
|
||||
Alle Menschen werden Brüder,
|
||||
Wo dein sanfter Flügel weilt.`
|
||||
dutch = `Wilhelmus van Nassouwe
|
||||
ben ik, van Duitsen bloed,
|
||||
den vaderland getrouwe
|
||||
blijf ik tot in den dood.
|
||||
Een Prinse van Oranje
|
||||
ben ik, vrij, onverveerd,
|
||||
den Koning van Hispanje
|
||||
heb ik altijd geëerd.`
|
||||
us = `O say can you see by the dawn's early light,
|
||||
What so proudly we hailed at the twilight's last gleaming,
|
||||
Whose broad stripes and bright stars through the perilous fight,
|
||||
O'er the ramparts we watched, were so gallantly streaming?
|
||||
And the rockets' red glare, the bombs bursting in air,
|
||||
Gave proof through the night that our flag was still there;
|
||||
O say does that star-spangled banner yet wave,
|
||||
O'er the land of the free and the home of the brave?`
|
||||
)
|
||||
|
||||
func BenchmarkConcurrentFrequency(b *testing.B) {
|
||||
b.StopTimer()
|
||||
var bTest []string
|
||||
for i := 0; i < 5; i++ {
|
||||
bTest = append(bTest, euro)
|
||||
bTest = append(bTest, dutch)
|
||||
bTest = append(bTest, us)
|
||||
}
|
||||
b.StartTimer()
|
||||
ConcurrentFrequency(bTest)
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func BenchmarkMyConcurrentFrequency(b *testing.B) {
|
||||
b.StopTimer()
|
||||
var bTest []string
|
||||
for i := 0; i < 5; i++ {
|
||||
bTest = append(bTest, euro)
|
||||
bTest = append(bTest, dutch)
|
||||
bTest = append(bTest, us)
|
||||
}
|
||||
b.StartTimer()
|
||||
MyConcurrentFrequency(bTest)
|
||||
b.StopTimer()
|
||||
}
|
Reference in New Issue
Block a user