-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.rb
61 lines (51 loc) · 933 Bytes
/
stack.rb
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
class MyStack < Array
def initialize
@store = []
@size = 0
end
def pop
@size -= 1
@store.pop[:value]
end
def minimum
if @store.empty?
return 1.0 / 0.0
end
peek[:minimum_including_item]
end
def maximum
if @store.empty?
return -1.0 / 0.0
end
peek[:maximum_including_item]
end
def push(new_value)
if empty?
min = new_value
max = new_value
else
min = peek[:minimum_including_item]
max = peek[:maximum_including_item]
if new_value < min
min = new_value
end
if new_value > max
max = new_value
end
end
new_entry = {:value => new_value,
:minimum_including_item => min,
:maximum_including_item => max}
@size += 1
@store.push(new_entry)
end
def peek
@store.last
end
def size
@size
end
def empty?
@size == 0
end
end