-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_project.rb
108 lines (88 loc) · 1.65 KB
/
window_project.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'byebug'
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
class MyStackQueue
attr_accessor :in_stack, :out_stack
def initialize
@in_stack = MyStack.new
@out_stack = MyStack.new
end
def maximum
[@in_stack.maximum, @out_stack.maximum].max
end
def minimum
[@in_stack.minimum, @out_stack.minimum].min
end
def size
@in_stack.size + @out_stack.size
end
def empty?
@in_stack.empty? && @out_stack.empty?
end
def enqueue(new_value)
@in_stack.push(new_value)
end
def dequeue
return @out_stack.pop unless @out_stack.empty?
while !@in_stack.empty?
@out_stack.push(@in_stack.pop)
end
@out_stack.pop
end
end
if __FILE__ == $PROGRAM_NAME
q = MyStackQueue.new
q.enqueue(5)
q.enqueue(-4)
debugger
q_min = q.minimum
end