From b27b2f0c4758498c8f034298ba423c70d7c4c117 Mon Sep 17 00:00:00 2001 From: motomux Date: Sat, 25 Feb 2017 00:08:23 -0800 Subject: [PATCH] Add problem 9 of chapter 8 --- src/chapter8/problem9.go | 32 ++++++++++++++++++++ src/chapter8/problem9_test.go | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/chapter8/problem9.go create mode 100644 src/chapter8/problem9_test.go diff --git a/src/chapter8/problem9.go b/src/chapter8/problem9.go new file mode 100644 index 0000000..c3362b7 --- /dev/null +++ b/src/chapter8/problem9.go @@ -0,0 +1,32 @@ +package main + +// Parens returns all valid parentheses by given number of pairs +func Parens(num int) []string { + var ( + res []string + bs []byte + ) + + ParensR(num, 0, 0, bs, &res) + + return res +} + +// ParensR adds valid parentheses with recursion +func ParensR(num, leftCnt, rightCnt int, bs []byte, res *[]string) { + if num < leftCnt || num < rightCnt { + return + } + if num == leftCnt && num == rightCnt { + *res = append(*res, string(bs)) + return + } + + if leftCnt < num { + ParensR(num, leftCnt+1, rightCnt, append(bs, '('), res) + } + + if rightCnt < num && rightCnt < leftCnt { + ParensR(num, leftCnt, rightCnt+1, append(bs, ')'), res) + } +} diff --git a/src/chapter8/problem9_test.go b/src/chapter8/problem9_test.go new file mode 100644 index 0000000..ccb10d1 --- /dev/null +++ b/src/chapter8/problem9_test.go @@ -0,0 +1,56 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestParens(t *testing.T) { + tests := map[string]struct { + in int + out []string + }{ + "Input is -1": { + in: -1, + out: []string(nil), + }, + "Input is 0": { + in: 0, + out: []string{ + "", + }, + }, + "Input is 1": { + in: 1, + out: []string{ + "()", + }, + }, + "Input is 2": { + in: 2, + out: []string{ + "(())", + "()()", + }, + }, + "Input is 3": { + in: 3, + out: []string{ + "((()))", + "(()())", + "(())()", + "()(())", + "()()()", + }, + }, + } + + for k, test := range tests { + t.Run(k, func(t *testing.T) { + out := Parens(test.in) + if !reflect.DeepEqual(out, test.out) { + t.Errorf("actual=%+v expected=%+v", out, test.out) + } + }) + } +}