Skip to content

[Go] Challenge 10 (Unreviewed) #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions challenge_10/go/erocs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Challenge 10: Match Closers

## 1. Solution Description

Create a pushdown atomata (utilizing a stack) to verify character group opening
characters are matched with appropriate closing characters in a given string.
Supported character sets:

Opener Closer
( )
[ ]
{ }
< >

## 2. Running Tests

In bash in this directory:

export GOPATH=`pwd`
go get challenge10
go test challenge10
37 changes: 37 additions & 0 deletions challenge_10/go/erocs/src/challenge10/challenge10.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package challenge10

import (
lane "gopkg.in/oleiade/lane.v1"
)

var openers map[rune]rune = map[rune]rune{
'(': ')',
'[': ']',
'{': '}',
'<': '>',
}

var closers map[rune]rune

// Initialize package private variables.
func init() {
closers = map[rune]rune{}
for k, v := range openers {
closers[v] = k
}
}

func HasValidClosers(s string) bool {
stk := lane.NewStack()
for _, ch := range s {
if closer, ok := openers[ch]; ok {
stk.Push(closer)
} else if !stk.Empty() && ch == stk.Head().(rune) {
stk.Pop()
} else if _, ok := closers[ch]; ok {
// Closer without an opener
return false
}
}
return stk.Empty()
}
102 changes: 102 additions & 0 deletions challenge_10/go/erocs/src/challenge10/challenge10_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package challenge10

import (
"testing"
)

func TestNoClosers(t *testing.T) {
if !HasValidClosers("The quack brown fax jumped over the dazy dog.") {
t.Fail()
}
}

func TestSingleCloser(t *testing.T) {
if !HasValidClosers("(Y)") {
t.Fail()
}
}

func TestNoOpener(t *testing.T) {
if HasValidClosers("hi)") {
t.Fail()
}
}

func TestNoCloser(t *testing.T) {
if HasValidClosers("(hi") {
t.Fail()
}
}

func TestParens(t *testing.T) {
if !HasValidClosers("()") {
t.Error("Bad valid order")
}
if HasValidClosers(")(") {
t.Error("Bad invalid order")
}
}

func TestBrackets(t *testing.T) {
if !HasValidClosers("[]") {
t.Error("Bad valid order")
}
if HasValidClosers("][") {
t.Error("Bad invalid order")
}
}

func TestBraces(t *testing.T) {
if !HasValidClosers("{}") {
t.Error("Bad valid order")
}
if HasValidClosers("}{") {
t.Error("Bad invalid order")
}
}

func TestLtGt(t *testing.T) {
if !HasValidClosers("<>") {
t.Error("Bad valid order")
}
if HasValidClosers("><") {
t.Error("Bad invalid order")
}
}

func TestValidNesting(t *testing.T) {
if !HasValidClosers("{[(<>)[<<{}>>]]}") {
t.Fail()
}
}

func TestInvalidNesting(t *testing.T) {
if HasValidClosers("({[<)}]>") {
t.Fail()
}
}

func TestDeepNesting(t *testing.T) {
l := 20000
rs := make([]rune, 0, l*2)
for i := 1; i <= l; i++ {
fizz := i%3 == 0
buzz := i%5 == 0
if fizz && buzz {
rs = append(rs, '(')
} else if fizz {
rs = append(rs, '[')
} else if buzz {
rs = append(rs, '{')
} else {
rs = append(rs, '<')
}
}
for i := l - 1; i >= 0; i-- {
rs = append(rs, openers[rs[i]])
}
s := string(rs)
if !HasValidClosers(s) {
t.Fail()
}
}