Skip to content

Instantly share code, notes, and snippets.

@sebnyberg
Created July 10, 2022 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebnyberg/b1f582700b415173614f4d0a95701793 to your computer and use it in GitHub Desktop.
Save sebnyberg/b1f582700b415173614f4d0a95701793 to your computer and use it in GitHub Desktop.
snippets.json
{
"TestCases": {
"prefix": "tcs",
"body": [
"func Test_$1(t *testing.T) {",
"\tfor _, tc := range []struct{",
"\t\t$2 $3",
"\t\twant $4",
"\t}{",
"\t\t{$0},",
"\t} {",
"\t\tt.Run(fmt.Sprintf(\"%+v\", tc.$2), func(t *testing.T){",
"\t\t\trequire.Equal(t, tc.want, $1(tc.$2))",
"\t\t})",
"\t}",
"}",
]
},
"MinFunc": {
"prefix": "minfunc",
"body": [
"$0",
"func min(a, b int) int {",
"\tif a < b {",
"\t\treturn a",
"\t}",
"\treturn b",
"}",
]
},
"MaxFunc": {
"prefix": "maxfunc",
"body": [
"$0",
"func max(a, b int) int {",
"\tif a > b {",
"\t\treturn a",
"\t}",
"\treturn b",
"}",
]
},
"AbsFunc": {
"prefix": "absfunc",
"body": [
"$0",
"func abs(a int) int {",
"\tif a < 0 {",
"\t\treturn -a",
"\t}",
"\treturn a",
"}",
]
},
"Reverse": {
"prefix": "revfunc",
"body": [
"$0",
"func rev(s string) string {",
"\tbs = []byte(s)",
"\tfor l, r := 0, len(s)-1; l < r; l, r = l + 1, r - 1{",
"\t\tbs[l], bs[r] = bs[r], bs[l]",
"\t}",
"\treturn string(bs)",
"}",
]
},
"TestFunc": {
"prefix": "testfunc",
"body": [
"func Test$1(t *testing.T) {",
"\t$0",
"}",
]
},
"Heap": {
"prefix": "heapfunc",
"body": [
"$0",
"type $1Heap []$2",
"func (h $1Heap) Len() int { return len(h) }",
"func (h $1Heap) Swap(i, j int) {",
"\th[i], h[j] = h[j], h[i]",
"\t// h[i].idx = i",
"\t// h[j].idx = j",
"}",
"func (h $1Heap) Less(i, j int) bool {",
"\treturn h[i]$3 < h[j]$3",
"}",
"func (h *$1Heap) Push(x interface{}) {",
"\t*h = append(*h, x.($2))",
"}",
"func (h *$1Heap) Pop() interface{} {",
"\tn := len(*h)",
"\tit := (*h)[n-1]",
"\t*h = (*h)[:n-1]",
"\treturn it",
"}",
]
},
"ParseTree": {
"prefix": "lctree",
"body": [
"type TreeNode struct {",
"\tVal int",
"\tLeft *TreeNode",
"\tRight *TreeNode",
"}",
"",
"func ParseTree(input string) *TreeNode {",
"\t// Trim start/end []",
"\tinput = input[1 : len(input)-1]",
"",
"\t// Split by comma",
"\tinputParts := strings.Split(input, \",\")",
"\tn := len(inputParts)",
"",
"\tif n == 0 || inputParts[0] == \"\" {",
"\t\treturn nil",
"\t}",
"",
"\t// Create one node per element in the array",
"\tnodes := make([]*TreeNode, n)",
"\tfor i, part := range inputParts {",
"\t\tif part != \"null\" {",
"\t\t\tval, err := strconv.Atoi(part)",
"\t\t\tif err != nil {",
"\t\t\t\tlog.Fatalln(err)",
"\t\t\t}",
"\t\t\tnodes[i] = &TreeNode{Val: val}",
"\t\t}",
"\t}",
"",
"\tq := list.New()",
"\tq.PushBack(nodes[0])",
"",
"\ti := 1",
"\tfor q.Len() > 0 && i < n {",
"\t\tel := q.Remove(q.Front()).(*TreeNode)",
"\t\tif nodes[i] != nil {",
"\t\t\tel.Left = nodes[i]",
"\t\t\tq.PushBack(nodes[i])",
"\t\t}",
"\t\ti++",
"\t\tif i >= n {",
"\t\t\tbreak",
"\t\t}",
"\t\tif nodes[i] != nil {",
"\t\t\tel.Right = nodes[i]",
"\t\t\tq.PushBack(nodes[i])",
"\t\t}",
"\t\ti++",
"\t}",
"",
"\treturn nodes[0]",
"}",
""
],
"description": "Create Leetcode Tree helpers"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment