Last active
January 12, 2017 02:40
-
-
Save nicerobot/f9213971655873737f9b22309d6ea0fb to your computer and use it in GitHub Desktop.
Go questions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .idea/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interviewing | |
| // | |
| type person struct { | |
| name string | |
| age int | |
| } | |
| // Given an array of persons, | |
| type people []person | |
| // Write a function to return a person object by name. | |
| func (ps people) ByName(name string) (p person) { | |
| // Implement this function | |
| return p | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interviewing | |
| import ( | |
| "testing" | |
| "github.com/stretchr/testify/assert" | |
| ) | |
| // | |
| var directory = people{ | |
| { | |
| name: "John", | |
| age: 20, | |
| }, { | |
| name: "Ally", | |
| age: 25, | |
| }, { | |
| name: "Jordan", | |
| age: 30, | |
| }, | |
| } | |
| // | |
| func TestByName(t *testing.T) { | |
| expect := person{name: "Ally", age:25} | |
| ok := assert.ObjectsAreEqualValues( | |
| expect, | |
| directory.ByName("Ally"), | |
| ) | |
| if !ok { | |
| t.Errorf("expected equal: %+v", expect) | |
| } | |
| } | |
| // | |
| func TestMissingByName(t *testing.T) { | |
| ok := assert.ObjectsAreEqual( | |
| nil, | |
| directory.ByName("Missing"), | |
| ) | |
| if !ok { | |
| t.Error("expected nil") | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interviewing | |
| // Given an array if nested arrays where terminals are always an int. | |
| type array []interface{} | |
| // Write a function to return the terminals top to bottom, left to right. | |
| func (a array) Flatten() (is []int) { | |
| // Implement this function | |
| return is | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interviewing | |
| import ( | |
| "testing" | |
| "github.com/stretchr/testify/assert" | |
| ) | |
| type cases struct { | |
| have array | |
| expect []int | |
| } | |
| // | |
| func TestFlatten(t *testing.T) { | |
| tests := []cases{ | |
| {array{1}, []int{1}}, | |
| {array{2, 3}, []int{2, 3}}, | |
| {array{3, array{4, 5}}, []int{3, 4, 5}}, | |
| {array{ | |
| 1, | |
| array{2, 3}, | |
| array{3, array{4, 5}}, | |
| }, []int{1, 2, 3, 3, 4, 5}}, | |
| } | |
| for _, test := range tests { | |
| flattened := test.have.Flatten() | |
| ok := assert.ObjectsAreEqualValues( | |
| flattened, | |
| test.expect, | |
| ) | |
| if !ok { | |
| t.Errorf("expected: %+v from: %+v got: %+v", test.expect, test.have, flattened) | |
| } | |
| } | |
| } | |
| // | |
| func TestUnexpected(t *testing.T) { | |
| tests := []cases{ | |
| {nil, nil}, | |
| {array{}, []int{}}, | |
| {array{""}, []int{}}, | |
| } | |
| for _, test := range tests { | |
| flattened := test.have.Flatten() | |
| ok := assert.ObjectsAreEqualValues( | |
| flattened, | |
| test.expect, | |
| ) | |
| if !ok { | |
| t.Errorf("expected: %+v from: %+v got: %+v", test.expect, test.have, flattened) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interviewing | |
| import ( | |
| "errors" | |
| ) | |
| // | |
| type value struct { | |
| column, operator, value string | |
| } | |
| // | |
| type conditional struct { | |
| a, b value | |
| join string | |
| } | |
| // | |
| func (c conditional) Evaluate(p person) (b bool, err error) { | |
| // Implement this function | |
| return b, errors.New("Unimplemented") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package interviewing | |
| import ( | |
| "testing" | |
| "github.com/stretchr/testify/assert" | |
| ) | |
| // | |
| var ally = person{ | |
| name:"Ally", | |
| age: 20, | |
| } | |
| // | |
| func TestTrue(t *testing.T) { | |
| condition := conditional{ | |
| a: value{ | |
| column: "name", | |
| operator: "=", | |
| value: "John", | |
| }, | |
| join: "OR", | |
| b: value{ | |
| column: "name", | |
| operator: "=", | |
| value: "Ally", | |
| }, | |
| } | |
| r, err := condition.Evaluate(ally) | |
| if err != nil { | |
| t.Error(err) | |
| return | |
| } | |
| assert.True(t, r, "Should be true") | |
| } | |
| // | |
| func TestFalse(t *testing.T) { | |
| condition := conditional{ | |
| a: value{ | |
| column: "name", | |
| operator: "=", | |
| value: "Ally", | |
| }, | |
| join: "AND", | |
| b: value{ | |
| column: "age", | |
| operator: "=", | |
| value: "21", | |
| }, | |
| } | |
| r, err := condition.Evaluate(ally) | |
| if err != nil { | |
| t.Error(err) | |
| return | |
| } | |
| assert.False(t, r, "Should be false") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment