-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLargestNumber.rb
More file actions
39 lines (32 loc) · 835 Bytes
/
Copy pathLargestNumber.rb
File metadata and controls
39 lines (32 loc) · 835 Bytes
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
require 'benchmark'
#------------------------------------------
# Find the largest number in an given array
class LargestNumber
#------------
# Constructor
def initialize(arrayValue)
@arrayValue = arrayValue
end
def findLargest_mine()
largest = 0
@arrayValue.length.times do |x|
largest = (@arrayValue[x] >= largest)? @arrayValue[x] : largest
end
return largest
end
def findLargest_efficient()
result = 0
@arrayValue.length.times do |x|
if(@arrayValue[x] > @arrayValue[result])
result = x
end
end
return @arrayValue[result]
end
end
largest = LargestNumber.new [2,5,8,14,6,25, 4,198, 10, 12, 144, 23]
largest.methods
Benchmark.bm do |x|
x.report("My Solution"){largest.findLargest_mine}
x.report("Efficient Solution"){largest.findLargest_efficient}
end