-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsort_by.rb
executable file
·46 lines (31 loc) · 993 Bytes
/
sort_by.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
#!/usr/bin/env ruby
require './lib/initialize'
def timer repeat=1, &block
start = Time.now
repeat.times{ block.call }
Time.now - start
end
print "numbers sorted: "
p @numbers.sort_by{|number| number}
print "numbers sorted by negative values: "
p @numbers.sort_by{|number| 0 - number}
divider
print "sorting pets by quantity: "
p @inventory.sort_by(&:quantity).map(&:name)
puts
puts "all the comparisons required to sort: "
comparisons = 0
duration = timer do
@inventory.sort_by{|pet| comparisons += 1; puts " #{pet.name}"; pet}
end
puts "total comparisons: #{comparisons}"
puts "total time: #{sprintf('%.4f', duration)} seconds"
divider
puts "sorting pets by time-consuming secret code: "
puts "all the comparisons required to sort: "
comparisons = 0
duration = timer do
@inventory.sort_by{|pet| comparisons += 1; code = pet.secret_code; puts " #{code}"; code}
end
puts "total comparisons: #{comparisons}"
puts "total time: #{sprintf('%.4f', duration)} seconds"