Last active
November 20, 2015 18:03
-
-
Save caelifer/a5b2a7851720078f1caf to your computer and use it in GitHub Desktop.
This file contains 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
// Implementation of merge task from http://raganwald.com/2015/11/03/a-coding-problem.html | |
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
func main() { | |
tests := []struct { | |
a, b []int | |
}{ | |
{ | |
[]int{1, 7, 11, 17}, | |
[]int{3, 5, 13}, | |
}, | |
{ | |
[]int{2, 3, 5, 7, 11}, | |
[]int{2, 4, 6, 8, 10, 12, 14}, | |
}, | |
} | |
for _, t := range tests { | |
fmt.Println(merge(t.a, t.b)) | |
} | |
} | |
func merge(a, b []int) []int { | |
x := clone(a) | |
x = append(x, clone(b)...) | |
sort.Ints(x) | |
return x | |
} | |
func clone(x []int) []int { | |
return append(make([]int, 0, len(x)), x...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live code - http://play.golang.org/p/WBQAcHdLV9