diff options
| author | mo khan <mo.khan@gmail.com> | 2020-06-28 16:02:55 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-06-28 16:02:55 -0600 |
| commit | b666decae0dbdc7716df52f8d4cdf21b3144b565 (patch) | |
| tree | c14a8ff137f80b5eabffd331a05d58b640e5da36 /unit/01/matched_string.rb | |
| parent | 8c2c29ec02da11b67b77ee8cd2538edbfd540257 (diff) | |
Move src and doc
Diffstat (limited to 'unit/01/matched_string.rb')
| -rw-r--r-- | unit/01/matched_string.rb | 51 |
1 files changed, 0 insertions, 51 deletions
diff --git a/unit/01/matched_string.rb b/unit/01/matched_string.rb deleted file mode 100644 index 2836d16..0000000 --- a/unit/01/matched_string.rb +++ /dev/null @@ -1,51 +0,0 @@ -require 'bundler/inline' - -gemfile do - source 'https://rubygems.org' - - gem 'minitest' -end - -require 'minitest/autorun' - -=begin -A matched string is a sequence of {, }, (, ), [, and ] characters that are properly matched. -For example, “{{()[]}}” is a matched string, but this “{{()]}” is not, since the second { is matched with a ]. -Show how to use a stack so that, given a string of length n, you can determine if it is a matched string in O(n) time. -=end - -class Example < Minitest::Test - def matches?(open, close) - case open - when '(' - return close == ')' - when '{' - return close == '}' - when '[' - return close == ']' - else - raise [open, close].inspect - end - end - - def matched_string?(string) - stack = [] - string.chars.each do |char| - case char - when '{', '[', '(' - stack.push(char) - else - return unless matches?(stack.pop, char) - end - end - stack.size.zero? - end - - def test_valid - assert matched_string?("{{()[]}}") - end - - def test_invalid - refute matched_string?("{{()]}") - end -end |
