summaryrefslogtreecommitdiff
path: root/doc/unit/01/reverse.rb
blob: a50f06200a5705cc7c1672309aa591cb63e9cfe3 (plain)
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
require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'

  gem 'minitest'
end

require 'minitest/autorun'

=begin
Suppose you have a Stack, s, that supports only the push(x) and pop() operations.
Show how, using only a FIFO Queue, q, you can reverse the order of all elements in s.
=end

class Example < Minitest::Test
  def test_valid
    s = []
    s.push('A')
    s.push('B')
    s.push('C')

    q = Queue.new
    3.times { q.enq(s.pop) }

    x = 3.times.map { q.deq }

    assert x == ['C', 'B', 'A']
  end
end