summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-26 13:10:33 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-26 13:10:33 +0200
commit754e21075a2b67ab6df1b2aa6201d2506f07cd42 (patch)
tree3f6afadfa75c17846ee562aac7efbee70232f4a0
parentb436ce6e3473ea606259730a1e7c3fe6f72c990e (diff)
Add parallel letter
-rw-r--r--go/parallel-letter-frequency/README.md24
-rw-r--r--go/parallel-letter-frequency/frequency.go11
-rw-r--r--go/parallel-letter-frequency/parallel_letter_frequency.go26
-rw-r--r--go/parallel-letter-frequency/parallel_letter_frequency_test.go58
4 files changed, 119 insertions, 0 deletions
diff --git a/go/parallel-letter-frequency/README.md b/go/parallel-letter-frequency/README.md
new file mode 100644
index 0000000..a2ed1ef
--- /dev/null
+++ b/go/parallel-letter-frequency/README.md
@@ -0,0 +1,24 @@
+# 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://exercism.io/languages/go).
+
+
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/parallel-letter-frequency/frequency.go b/go/parallel-letter-frequency/frequency.go
new file mode 100644
index 0000000..0ea7cb2
--- /dev/null
+++ b/go/parallel-letter-frequency/frequency.go
@@ -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
+}
diff --git a/go/parallel-letter-frequency/parallel_letter_frequency.go b/go/parallel-letter-frequency/parallel_letter_frequency.go
new file mode 100644
index 0000000..e1080b8
--- /dev/null
+++ b/go/parallel-letter-frequency/parallel_letter_frequency.go
@@ -0,0 +1,26 @@
+package letter
+
+import "sync"
+
+func ConcurrentFrequency(s []string) FreqMap {
+ c := make(chan rune, len(s))
+ wg := sync.WaitGroup{}
+ for _, v := range s {
+ wg.Add(1)
+ go func(s string) {
+ for _, r := range s {
+ c <- r
+ }
+ wg.Done()
+ }(v)
+ }
+ go func() {
+ wg.Wait()
+ close(c)
+ }()
+ m := FreqMap{}
+ for r := range c {
+ m[r]++
+ }
+ return m
+}
diff --git a/go/parallel-letter-frequency/parallel_letter_frequency_test.go b/go/parallel-letter-frequency/parallel_letter_frequency_test.go
new file mode 100644
index 0000000..99fed05
--- /dev/null
+++ b/go/parallel-letter-frequency/parallel_letter_frequency_test.go
@@ -0,0 +1,58 @@
+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 BenchmarkSequentialFrequency(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Frequency(euro + dutch + us)
+ }
+}
+
+func BenchmarkConcurrentFrequency(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ConcurrentFrequency([]string{euro, dutch, us})
+ }
+}