Skip to content

Commit 0fe417f

Browse files
committed
Merge pull request exercism#155 from jimmbraddock/bracket-push
Add bracket-push exercise
2 parents 0fe42d6 + 9fd4be7 commit 0fe417f

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"list-ops",
1313
"sublist",
1414
"anagram",
15+
"bracket-push",
1516
"nucleotide-count",
1617
"rna-transcription",
1718
"hamming",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule BracketPush do
2+
@doc """
3+
Checks that all the brackets and braces in the string are matched correctly, and nested correctly
4+
"""
5+
@spec check_brackets(String.t) :: boolean
6+
def check_brackets(str) do
7+
8+
end
9+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
2+
Code.load_file("bracket_push.exs")
3+
end
4+
5+
ExUnit.start
6+
ExUnit.configure exclude: :pending, trace: true
7+
8+
defmodule BracketPushTest do
9+
use ExUnit.Case
10+
11+
# @tag :pending
12+
test "empty string" do
13+
assert BracketPush.check_brackets("")
14+
end
15+
16+
@tag :pending
17+
test "appropriate bracketing in a set of brackets" do
18+
assert BracketPush.check_brackets("{}")
19+
end
20+
21+
@tag :pending
22+
test "unclosed brackets" do
23+
refute BracketPush.check_brackets("{{")
24+
end
25+
26+
@tag :pending
27+
test "more than one pair of brackets" do
28+
assert BracketPush.check_brackets("{}[]")
29+
end
30+
31+
@tag :pending
32+
test "brackets are out of order" do
33+
refute BracketPush.check_brackets("}{")
34+
end
35+
36+
@tag :pending
37+
test "nested brackets" do
38+
assert BracketPush.check_brackets("{[()]}")
39+
end
40+
41+
@tag :pending
42+
test "unbalanced nested brackets" do
43+
refute BracketPush.check_brackets("{[}]")
44+
end
45+
46+
@tag :pending
47+
test "bracket closure with deeper nesting" do
48+
refute BracketPush.check_brackets("{[)][]}")
49+
end
50+
51+
@tag :pending
52+
test "bracket closure in a long string of brackets" do
53+
assert BracketPush.check_brackets("{[]([()])}")
54+
end
55+
56+
@tag :pending
57+
test "should ignore non-bracket characters" do
58+
assert BracketPush.check_brackets("{hello[]([a()])b}c")
59+
end
60+
61+
@tag :pending
62+
test "string with newlines" do
63+
assert BracketPush.check_brackets("[]\n{()}\n[(({}))]\n")
64+
end
65+
end

exercises/bracket-push/example.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
defmodule BracketPush do
2+
@brackets %{
3+
"{" => "}",
4+
"(" => ")",
5+
"[" => "]"
6+
}
7+
8+
@doc """
9+
Check that all the brackets and braces in the string are matched correctly, and nested correctly
10+
"""
11+
@spec check_brackets(String.t) :: boolean
12+
def check_brackets(str) do
13+
str
14+
|> String.replace(~r/[^\{\}\[\]\(\)]/, "")
15+
|> String.codepoints
16+
|> check([])
17+
end
18+
19+
defp check([], []), do: true
20+
defp check([], _), do: false
21+
defp check([h|t], acc) do
22+
cond do
23+
Map.has_key?(@brackets, h) ->
24+
check(t, [Map.get(@brackets, h) | acc])
25+
Enum.empty?(acc) and !Map.has_key?(@brackets, h) ->
26+
false
27+
h != hd(acc) ->
28+
false
29+
h == hd(acc) ->
30+
check(t, tl(acc))
31+
true ->
32+
true
33+
end
34+
end
35+
end

0 commit comments

Comments
 (0)