Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions find_max.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

# Find the maximum element between index 0 and max_index, exclusive

def find_max(array, max_index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is man_index the best name for this var? isn't it actually the array length?

max = nil
if max_index == 0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also check if max_index is nil or not a number

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seconding this

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we check if the array is empty and forego the if/else statement? That way it can print the 'array is empty' statement if it's true and then after the check, we can just set max to array[0].

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean:

Suggested change
if max_index == 0
if array.length == 0

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add check to see if max_index is nil.

puts "Array is empty"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is neat

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If max index is 0, maybe we should say the maximum number is equal to the value at that index.

else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make the code a little less nested to have the emptiness check as its own stand-alone function with a short-circuit return and then run the code currently in the "else" portion of the code independently.

max = array[0]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check if array[0] exists first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably check to ensure array has this index first

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check if array is empty

index = 0
while index < max_index

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an inclusive search because of the "<" sign. Is this on purpose?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if max_index is a negative number, it will not be == to zero but this while loop will also never be true.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if index == max_index

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while index <= max_index

if max < array[index]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max is nil, can't compare nil and a number

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you're increasing index until index < max_index is false, you should check that the array has at least max_index number of indices

max = array[index]
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can change lines 11-14 to one line statement:
max = array[index] if max < array[index]

index += 1
end
end
return max
end