-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeliminators.rb
More file actions
157 lines (132 loc) · 3.76 KB
/
deliminators.rb
File metadata and controls
157 lines (132 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# These interview questions are addicting; I have to stop with them and get on something else. But one more won’t hurt:
# Write a function that takes a string and determines if the delimiters in the string are balanced. The pairs of delimiters are (), [], {}, and <>, and delimiters may be nested. In addition, determine that string delimiters ‘ and ” are properly matched; other delimiters lose their magical delimiter-ness property within quoted strings. Any delimiter is escaped if it follows a backslash.
# Your task is to write the function to determine if a string has balanced delimiters. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
require 'rspec'
class Deliminator
class Invalid < StandardError; end
def initialize(string)
@string = string
@position = 0
end
def balanced?
parse
build
rescue
false
end
def parse
@tokens = []
@string.each_char do |chr|
if @skip
@skip = false
else
@tokens << case chr
when '[' then :open_square
when ']' then :close_square
when '{' then :open_swig
when '}' then :close_swig
when '(' then :open_round
when ')' then :close_round
when '<' then :open_angle
when '>' then :close_angle
when '"' then :double_quotes
when "'" then :single_quotes
when '\\' then
@skip = true
nil
end
end
end
@tokens.compact!
end
def build
tree = Tree.new
@tokens.each do |token|
tree = tree.add(token)
end
tree.valid?
end
class Tree
def initialize(parent=nil)
@parent = parent
end
def close(token)
token == @closer
end
def valid?
@parent.nil?
end
def map
{
:open_square => :close_square,
:open_swig => :close_swig,
:open_round => :close_round,
:open_angle => :close_angle
}
end
def add(token)
case token
when *map.keys
@closer = map[token]
Tree.new(self)
when *map.values
raise Invalid unless @parent && @parent.close(token)
@parent
when :double_quotes, :single_quotes
@closer = token
TreeQuotes.new(self)
else
raise "Unknown Symbol: #{token}"
end
end
end
class TreeQuotes < Tree
def add(token)
if @parent.close(token)
@parent
else
self
end
end
end
end
describe Deliminator do
it 'returns true for an empty string' do
Deliminator.new('').should be_balanced
end
it 'returns false when a single unbalanced element' do
Deliminator.new('[').should_not be_balanced
end
it 'returns false when a single unbalanced close element' do
Deliminator.new(')').should_not be_balanced
end
it 'returns true when a balances set' do
Deliminator.new('[]').should be_balanced
end
it 'returns false when a uneven set' do
Deliminator.new('[[]').should_not be_balanced
end
it 'returns true when unbalanced within quotes' do
Deliminator.new('"["').should be_balanced
end
it 'ignores escaped charcters' do
Deliminator.new('[\]]').should be_balanced
end
it 'swiggle sets' do
Deliminator.new('{').should_not be_balanced
Deliminator.new('{}').should be_balanced
end
it 'round sets' do
Deliminator.new('(').should_not be_balanced
Deliminator.new(')').should_not be_balanced
Deliminator.new('()').should be_balanced
end
it 'angle sets' do
Deliminator.new('<').should_not be_balanced
Deliminator.new('>').should_not be_balanced
Deliminator.new('<>').should be_balanced
end
it 'with single quote' do
Deliminator.new("'['").should be_balanced
end
end