master
1require 'bundler/inline'
2
3gemfile do
4 source 'https://rubygems.org'
5
6 gem 'minitest'
7end
8
9require 'minitest/autorun'
10
11=begin
12A Dyck word is a sequence of +1’s and -1’s with the property that the sum of any prefix
13of the sequence is never negative.
14For example, +1,−1,+1,−1 is a Dyck word, but +1,−1,−1,+1 is not a Dyck word since the prefix +1 − 1 − 1 < 0.
15
16Describe any relationship between Dyck words and Stack push(x) and pop() operations.
17=end
18
19class Example < Minitest::Test
20 def dyck_word?(stack)
21 sum = 0
22 stack.each do |item|
23 return if sum.negative?
24 sum += item
25 end
26 true
27 end
28
29 def test_valid_word
30 assert dyck_word?([1, -1, 1, -1])
31 end
32
33 def test_invalid_word
34 refute dyck_word?([1, -1, -1, 1])
35 end
36end