Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR-3 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

PR-3 #3

wants to merge 1 commit into from

Conversation

CheezItMan
Copy link

This method find the maximum value of an array.

@tildeee tildeee changed the title find_max method PR-3 Feb 6, 2020
if max_index == 0
puts "Array is empty"
else
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


def find_max(array, max_index)
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


def find_max(array, max_index)
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.

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].

else
max = array[0]
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?

max = nil
if max_index == 0
puts "Array is empty"
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.

def find_max(array, max_index)
max = nil
if max_index == 0
puts "Array is empty"

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.

Copy link

@Galaxylaughing Galaxylaughing left a comment

Choose a reason for hiding this comment

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

ok

if max_index == 0
puts "Array is empty"
else
max = array[0]

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

else
max = array[0]
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.

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

max = array[0]
index = 0
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.

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


# 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?


def find_max(array, max_index)
max = nil
if max_index == 0

Choose a reason for hiding this comment

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

Seconding this

Copy link

@raisahv raisahv left a comment

Choose a reason for hiding this comment

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

Approved

while index < max_index
if max < array[index]
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]

else
max = array[0]
index = 0
while index < max_index
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


def find_max(array, max_index)
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.

Add check to see if max_index is nil.


def find_max(array, max_index)
max = nil
if max_index == 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

max = array[0]
index = 0
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

def find_max(array, max_index)
max = nil
if max_index == 0
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

if max_index == 0
puts "Array is empty"
else
max = array[0]

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

else
max = array[0]
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.

while index <= max_index

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.