summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--go/secret-handshake/README.md49
-rw-r--r--go/secret-handshake/secret_handshake.go33
-rw-r--r--go/secret-handshake/secret_handshake_test.go43
3 files changed, 125 insertions, 0 deletions
diff --git a/go/secret-handshake/README.md b/go/secret-handshake/README.md
new file mode 100644
index 0000000..8548bbc
--- /dev/null
+++ b/go/secret-handshake/README.md
@@ -0,0 +1,49 @@
+# Secret Handshake
+
+Write a program that will take a decimal number, and convert it to the appropriate sequence of events for a secret handshake.
+
+> There are 10 types of people in the world: Those who understand
+> binary, and those who don't.
+
+You and your fellow cohort of those in the "know" when it comes to
+binary decide to come up with a secret "handshake".
+
+```
+1 = wink
+10 = double blink
+100 = close your eyes
+1000 = jump
+
+
+10000 = Reverse the order of the operations in the secret handshake.
+```
+
+Here's a couple of examples:
+
+Given the input 9, the function would return the array
+["wink", "jump"]
+
+Given the input "11001", the function would return the array
+["jump", "wink"]
+
+
+The program should consider strings specifying an invalid binary as the
+value 0.
+
+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).
+
+## Source
+
+Bert, in Mary Poppins [http://www.imdb.com/character/ch0011238/quotes](http://www.imdb.com/character/ch0011238/quotes)
+
+## 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/secret-handshake/secret_handshake.go b/go/secret-handshake/secret_handshake.go
new file mode 100644
index 0000000..8ad5fff
--- /dev/null
+++ b/go/secret-handshake/secret_handshake.go
@@ -0,0 +1,33 @@
+package secret
+
+var sign = map[int]string{
+ 1 << 0: "wink",
+ 1 << 1: "double blink",
+ 1 << 2: "close your eyes",
+ 1 << 3: "jump",
+}
+
+const rev = 1 << 4
+
+func reverse(s []string) []string {
+ for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
+ s[i], s[j] = s[j], s[i]
+ }
+ return s
+}
+
+func Handshake(n int) []string {
+ if n <= 0 {
+ return nil
+ }
+ var s []string
+ for i := 1; i < rev; i <<= 1 {
+ if n&i != 0 {
+ s = append(s, sign[i])
+ }
+ }
+ if n&rev != 0 {
+ s = reverse(s)
+ }
+ return s
+}
diff --git a/go/secret-handshake/secret_handshake_test.go b/go/secret-handshake/secret_handshake_test.go
new file mode 100644
index 0000000..65fd058
--- /dev/null
+++ b/go/secret-handshake/secret_handshake_test.go
@@ -0,0 +1,43 @@
+package secret
+
+import (
+ "reflect"
+ "testing"
+)
+
+var tests = []struct {
+ code int
+ h []string
+}{
+ {1, []string{"wink"}},
+ {2, []string{"double blink"}},
+ {4, []string{"close your eyes"}},
+ {8, []string{"jump"}},
+ {3, []string{"wink", "double blink"}},
+ {19, []string{"double blink", "wink"}},
+ {31, []string{"jump", "close your eyes", "double blink", "wink"}},
+ {0, nil},
+ {-1, nil},
+ {32, nil},
+ {33, []string{"wink"}},
+}
+
+func TestHandshake(t *testing.T) {
+ for _, test := range tests {
+ h := Handshake(test.code)
+ // use len() to allow either nil or empty list, because
+ // they are not equal by DeepEqual
+ if len(h) == 0 && len(test.h) == 0 {
+ continue
+ }
+ if !reflect.DeepEqual(h, test.h) {
+ t.Fatalf("Handshake(%d) = %v, want %v.", test.code, h, test.h)
+ }
+ }
+}
+
+func BenchmarkHandshake(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Handshake(31)
+ }
+}