Initial Commit
This commit is contained in:
44
go/secret-handshake/README.md
Normal file
44
go/secret-handshake/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# 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.
|
||||
```
|
||||
|
||||
```
|
||||
handshake = SecretHandshake.new 9
|
||||
handshake.commands # => ["wink","jump"]
|
||||
|
||||
handshake = SecretHandshake.new "11001"
|
||||
handshake.commands # => ["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://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
Bert, in Mary Poppins [view source](http://www.imdb.com/character/ch0011238/quotes)
|
31
go/secret-handshake/secret_handshake.go
Normal file
31
go/secret-handshake/secret_handshake.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package secret
|
||||
|
||||
// Handshake converts an integer into a string slice
|
||||
// of the code
|
||||
func Handshake(code int) []string {
|
||||
if code < 0 {
|
||||
return nil
|
||||
}
|
||||
var ret []string
|
||||
if code&1 == 1 {
|
||||
ret = append(ret, "wink")
|
||||
}
|
||||
if code&2 == 2 {
|
||||
ret = append(ret, "double blink")
|
||||
}
|
||||
if code&4 == 4 {
|
||||
ret = append(ret, "close your eyes")
|
||||
}
|
||||
if code&8 == 8 {
|
||||
ret = append(ret, "jump")
|
||||
}
|
||||
if code&16 == 16 {
|
||||
var revRet []string
|
||||
for len(ret) > 0 {
|
||||
revRet = append(revRet, ret[len(ret)-1])
|
||||
ret = ret[:len(ret)-1]
|
||||
}
|
||||
ret = revRet
|
||||
}
|
||||
return ret
|
||||
}
|
43
go/secret-handshake/secret_handshake_test.go
Normal file
43
go/secret-handshake/secret_handshake_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user