Skip to content

Commit

Permalink
feat: Add parentheses example.
Browse files Browse the repository at this point in the history
  • Loading branch information
goloroden committed Nov 23, 2024
1 parent ebf3f32 commit 182270e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ So far, the following exercises have been covered:
- [Floyd](./floyd/) – implements Floyd's cycle-finding "Tortoise and Hare" algorithm
- [Heap](./heap/) – implements a heap from scratch, without using the built-in `container/heap` package
- [Markov count](./markovcount/) – figures out how often states are reached by a Markov chain
- [Parentheses](./parentheses/) – checks if a string has balanced parentheses.
- [Radix sort](./radixsort/) – implements radix sort
- [Remove k-th last element](./removekthlastelement/) – removes the k-th last element from a single-linked list
- [Run-length encoding](./runlengthencoding/) – encodes and decodes strings using run-length encoding
Expand Down
2 changes: 2 additions & 0 deletions parentheses/documentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package parentheses checks if a string has balanced parentheses.
package parentheses
24 changes: 24 additions & 0 deletions parentheses/is_balanced.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package parentheses

func IsBalanced(s string) bool {
low, high := 0, 0
for _, char := range s {
switch char {
case '(':
low++
high++
case ')':
low = max(low-1, 0)
high--
case '*':
low = max(low-1, 0)
high++
}

if high < 0 {
return false
}
}

return low == 0
}
48 changes: 48 additions & 0 deletions parentheses/is_balanced_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package parentheses_test

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/thenativeweb/codingcircle/parentheses"
)

func TestIsBalanced(t *testing.T) {
t.Run("empty string", func(t *testing.T) {
assert.True(t, parentheses.IsBalanced(""))
})

t.Run("balanced string", func(t *testing.T) {
assert.True(t, parentheses.IsBalanced("()"))
assert.True(t, parentheses.IsBalanced("(())"))
assert.True(t, parentheses.IsBalanced("()()"))
assert.True(t, parentheses.IsBalanced("(()())"))
assert.True(t, parentheses.IsBalanced("((()))"))
})

t.Run("unbalanced string", func(t *testing.T) {
assert.False(t, parentheses.IsBalanced("("))
assert.False(t, parentheses.IsBalanced(")"))
assert.False(t, parentheses.IsBalanced(")("))
assert.False(t, parentheses.IsBalanced("())"))
assert.False(t, parentheses.IsBalanced("(()"))
assert.False(t, parentheses.IsBalanced("((())"))
})

t.Run("balanced string with wildcard", func(t *testing.T) {
assert.True(t, parentheses.IsBalanced("*"))
assert.True(t, parentheses.IsBalanced("(*)"))
assert.True(t, parentheses.IsBalanced("(*))"))
assert.True(t, parentheses.IsBalanced("(**))*"))
assert.True(t, parentheses.IsBalanced("*((**))*"))
assert.True(t, parentheses.IsBalanced("(***"))
})

t.Run("unbalanced string with wildcard", func(t *testing.T) {
assert.False(t, parentheses.IsBalanced("*("))
assert.False(t, parentheses.IsBalanced("*)("))
assert.False(t, parentheses.IsBalanced("***("))
assert.False(t, parentheses.IsBalanced("(***(()"))
assert.False(t, parentheses.IsBalanced("(**("))
})
}

0 comments on commit 182270e

Please sign in to comment.