Skip to content

Commit 182270e

Browse files
committed
feat: Add parentheses example.
1 parent ebf3f32 commit 182270e

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ So far, the following exercises have been covered:
1616
- [Floyd](./floyd/) – implements Floyd's cycle-finding "Tortoise and Hare" algorithm
1717
- [Heap](./heap/) – implements a heap from scratch, without using the built-in `container/heap` package
1818
- [Markov count](./markovcount/) – figures out how often states are reached by a Markov chain
19+
- [Parentheses](./parentheses/) – checks if a string has balanced parentheses.
1920
- [Radix sort](./radixsort/) – implements radix sort
2021
- [Remove k-th last element](./removekthlastelement/) – removes the k-th last element from a single-linked list
2122
- [Run-length encoding](./runlengthencoding/) – encodes and decodes strings using run-length encoding

parentheses/documentation.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package parentheses checks if a string has balanced parentheses.
2+
package parentheses

parentheses/is_balanced.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package parentheses
2+
3+
func IsBalanced(s string) bool {
4+
low, high := 0, 0
5+
for _, char := range s {
6+
switch char {
7+
case '(':
8+
low++
9+
high++
10+
case ')':
11+
low = max(low-1, 0)
12+
high--
13+
case '*':
14+
low = max(low-1, 0)
15+
high++
16+
}
17+
18+
if high < 0 {
19+
return false
20+
}
21+
}
22+
23+
return low == 0
24+
}

parentheses/is_balanced_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package parentheses_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/thenativeweb/codingcircle/parentheses"
8+
)
9+
10+
func TestIsBalanced(t *testing.T) {
11+
t.Run("empty string", func(t *testing.T) {
12+
assert.True(t, parentheses.IsBalanced(""))
13+
})
14+
15+
t.Run("balanced string", func(t *testing.T) {
16+
assert.True(t, parentheses.IsBalanced("()"))
17+
assert.True(t, parentheses.IsBalanced("(())"))
18+
assert.True(t, parentheses.IsBalanced("()()"))
19+
assert.True(t, parentheses.IsBalanced("(()())"))
20+
assert.True(t, parentheses.IsBalanced("((()))"))
21+
})
22+
23+
t.Run("unbalanced string", func(t *testing.T) {
24+
assert.False(t, parentheses.IsBalanced("("))
25+
assert.False(t, parentheses.IsBalanced(")"))
26+
assert.False(t, parentheses.IsBalanced(")("))
27+
assert.False(t, parentheses.IsBalanced("())"))
28+
assert.False(t, parentheses.IsBalanced("(()"))
29+
assert.False(t, parentheses.IsBalanced("((())"))
30+
})
31+
32+
t.Run("balanced string with wildcard", func(t *testing.T) {
33+
assert.True(t, parentheses.IsBalanced("*"))
34+
assert.True(t, parentheses.IsBalanced("(*)"))
35+
assert.True(t, parentheses.IsBalanced("(*))"))
36+
assert.True(t, parentheses.IsBalanced("(**))*"))
37+
assert.True(t, parentheses.IsBalanced("*((**))*"))
38+
assert.True(t, parentheses.IsBalanced("(***"))
39+
})
40+
41+
t.Run("unbalanced string with wildcard", func(t *testing.T) {
42+
assert.False(t, parentheses.IsBalanced("*("))
43+
assert.False(t, parentheses.IsBalanced("*)("))
44+
assert.False(t, parentheses.IsBalanced("***("))
45+
assert.False(t, parentheses.IsBalanced("(***(()"))
46+
assert.False(t, parentheses.IsBalanced("(**("))
47+
})
48+
}

0 commit comments

Comments
 (0)