~telemachus/Exercism

2abaa8cd183d0b2e9d2fecf8e7682a44394c3c40 — Peter Aronoff 5 months ago 5475d33 main
Brackets!
A go/matching-brackets/.exercism/config.json => go/matching-brackets/.exercism/config.json +38 -0
@@ 0,0 1,38 @@
{
  "blurb": "Make sure the brackets and braces all match.",
  "authors": [
    "pminten"
  ],
  "contributors": [
    "alebaffa",
    "bitfield",
    "dvrkps",
    "ekingery",
    "ferhatelmas",
    "hilary",
    "ilmanzo",
    "kytrinyx",
    "leenipper",
    "petertseng",
    "robphoenix",
    "rpottsoh",
    "sebito91",
    "strangeman",
    "tleen"
  ],
  "files": {
    "solution": [
      "matching_brackets.go"
    ],
    "test": [
      "matching_brackets_test.go"
    ],
    "example": [
      ".meta/example.go"
    ],
    "editor": [
      "cases_test.go"
    ]
  },
  "source": "Ginna Baker"
}

A go/matching-brackets/.exercism/metadata.json => go/matching-brackets/.exercism/metadata.json +1 -0
@@ 0,0 1,1 @@
{"track":"go","exercise":"matching-brackets","id":"5f29164f3064433194d57a22c11c9cf7","url":"https://exercism.org/tracks/go/exercises/matching-brackets","handle":"telemachus","is_requester":true,"auto_approve":false}
\ No newline at end of file

A go/matching-brackets/HELP.md => go/matching-brackets/HELP.md +40 -0
@@ 0,0 1,40 @@
# Help

## Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
flags:

    go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with
different specs, so the results from these benchmark tests may vary.

## Submitting your solution

You can submit your solution using the `exercism submit matching_brackets.go` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [Go track's documentation](https://exercism.org/docs/tracks/go)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

To get help if you're having trouble, you can use one of the following resources:

- [How to Write Go Code](https://golang.org/doc/code.html)
- [Effective Go](https://golang.org/doc/effective_go.html)
- [Go Resources](http://golang.org/help)
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)
\ No newline at end of file

A go/matching-brackets/README.md => go/matching-brackets/README.md +38 -0
@@ 0,0 1,38 @@
# Matching Brackets

Welcome to Matching Brackets on Exercism's Go Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Given a string containing brackets `[]`, braces `{}`, parentheses `()`,
or any combination thereof, verify that any and all pairs are matched
and nested correctly.

## Source

### Created by

- @pminten

### Contributed to by

- @alebaffa
- @bitfield
- @dvrkps
- @ekingery
- @ferhatelmas
- @hilary
- @ilmanzo
- @kytrinyx
- @leenipper
- @petertseng
- @robphoenix
- @rpottsoh
- @sebito91
- @strangeman
- @tleen

### Based on

Ginna Baker
\ No newline at end of file

A go/matching-brackets/cases_test.go => go/matching-brackets/cases_test.go +98 -0
@@ 0,0 1,98 @@
package brackets

// Source: exercism/problem-specifications
// Commit: 51418ec bracket-push: rename to matching-brackets (#1501)
// Problem Specifications Version: 2.0.0

type bracketTest struct {
	input    string
	expected bool
}

var testCases = []bracketTest{
	{
		// paired square brackets
		"[]",
		true,
	},
	{
		// empty string
		"",
		true,
	},
	{
		// unpaired brackets
		"[[",
		false,
	},
	{
		// wrong ordered brackets
		"}{",
		false,
	},
	{
		// wrong closing bracket
		"{]",
		false,
	},
	{
		// paired with whitespace
		"{ }",
		true,
	},
	{
		// partially paired brackets
		"{[])",
		false,
	},
	{
		// simple nested brackets
		"{[]}",
		true,
	},
	{
		// several paired brackets
		"{}[]",
		true,
	},
	{
		// paired and nested brackets
		"([{}({}[])])",
		true,
	},
	{
		// unopened closing brackets
		"{[)][]}",
		false,
	},
	{
		// unpaired and nested brackets
		"([{])",
		false,
	},
	{
		// paired and wrong nested brackets
		"[({]})",
		false,
	},
	{
		// paired and incomplete brackets
		"{}[",
		false,
	},
	{
		// too many closing brackets
		"[]]",
		false,
	},
	{
		// math expression
		"(((185 + 223.85) * 15) - 543)/2",
		true,
	},
	{
		// complex latex expression
		"\\left(\\begin{array}{cc} \\frac{1}{3} & x\\\\ \\mathrm{e}^{x} &... x^2 \\end{array}\\right)",
		true,
	},
}

A go/matching-brackets/go.mod => go/matching-brackets/go.mod +3 -0
@@ 0,0 1,3 @@
module brackets

go 1.16

A go/matching-brackets/matching_brackets.go => go/matching-brackets/matching_brackets.go +57 -0
@@ 0,0 1,57 @@
package brackets

import "errors"

type Stack struct {
	stack  []byte
}

var ErrEmptyStack = errors.New("cannot Pop empty stack")

func Bracket(input string) bool {
	brackets := NewStack(len(input))
	for _, c := range []byte(input) {
		switch c {
		case '{', '(', '[':
			brackets.Push(c)
		case '}':
			if b := brackets.Pop(); b != '{' {
				return false
			}
		case ')':
			if b := brackets.Pop(); b != '(' {
				return false
			}
		case ']':
			if b := brackets.Pop(); b != '[' {
				return false
			}
		}
	}
	return brackets.Len() == 0
}

func NewStack(n int) *Stack {
	s := &Stack{stack: make([]byte, 0, n)}
	return s
}

func (s *Stack) Push(c byte) {
	s.stack = append(s.stack, c)
}

func (s *Stack) Pop() byte {
	l := len(s.stack)
	if l < 1 {
		return 0
	}
	n := l - 1
	c := s.stack[n]
	s.stack[n] = 0
	s.stack = s.stack[:n]
	return c
}

func (s *Stack) Len() int {
	return len(s.stack)
}

A go/matching-brackets/matching_brackets_test.go => go/matching-brackets/matching_brackets_test.go +26 -0
@@ 0,0 1,26 @@
package brackets

import (
	"testing"
)

func TestBracket(t *testing.T) {
	for _, tt := range testCases {
		actual := Bracket(tt.input)
		if actual != tt.expected {
			t.Fatalf("Bracket(%q) was expected to return %v but returned %v.",
				tt.input, tt.expected, actual)
		}
	}
}

func BenchmarkBracket(b *testing.B) {
	if testing.Short() {
		b.Skip("skipping benchmark in short mode.")
	}
	for i := 0; i < b.N; i++ {
		for _, tt := range testCases {
			Bracket(tt.input)
		}
	}
}