new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
24
Task/Balanced_brackets/Ruby/balanced_brackets.rb
Normal file
24
Task/Balanced_brackets/Ruby/balanced_brackets.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
re = /\A # beginning of string
|
||||
(?<bb> # begin capture group <bb>
|
||||
\[ # literal [
|
||||
\g<bb>* # zero or more <bb>
|
||||
\] # literal ]
|
||||
)* # end group, zero or more such groups
|
||||
\z/x # end of string
|
||||
|
||||
(0..9).each do |i|
|
||||
s = "[]" * i
|
||||
|
||||
# There is no String#shuffle! method.
|
||||
# This is a Knuth shuffle.
|
||||
(s.length - 1).downto(1) do |a; b|
|
||||
b = rand(a + 1)
|
||||
s[a], s[b] = s[b], s[a]
|
||||
end
|
||||
|
||||
puts((s =~ re ? " OK: " : "bad: ") + s)
|
||||
end
|
||||
|
||||
["[[]", "[]]", "[letters]"].each do |s|
|
||||
puts((s =~ re ? " OK: " : "bad: ") + s)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue