-
Notifications
You must be signed in to change notification settings - Fork 60
/
min.rb
44 lines (38 loc) · 1.24 KB
/
min.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
# Method name: min
# Input: a list of numbers
# Returns: the SMALLEST number in the list
# Prints: Nothing
#
# In English, the "min" method takes as input a list of numbers and
# returns the SMALLEST number in that list.
# Your job here is to fill in the ______. Be deliberate, don't just guess!
# The number of spaces (____) is meaningless; it's not as if four blank spots
# means you need to replace it with a 4-character chunk of Ruby.
# This is going to be very similar to max, so don't be afraid if
# these two methods look almost identical
def min(list)
____ = ____
____.each do |____|
if ____
____ = ____
end
end
return ____
end
if __FILE__ == $PROGRAM_NAME
# "p" prints something to the screen in a way that's better for debugging
p min([1, 2, 3]) == 1
p min([0, -100, 50, -200]) == -200
p min([-200, -400, -100, -300]) == -400
p min([0]) == 0
p min([1]) == 1
p min([-1]) == -1
p min([11, 11, 11]) == 11
p min([-22, -11, -22]) == -22
end
# Each of the lines above will print out "true" or "false"
# and collectively act as a sanity check.
#
# Think of these like rumble strips on the side of the road. They're here
# to tell you when you're veering off the road, not to guarantee you're
# driving phenomenally. :)