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: find_max method #3

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

PR 3: find_max method #3

wants to merge 1 commit into from

Conversation

CheezItMan
Copy link

No description provided.

@CheezItMan CheezItMan changed the title PR 4: find_max method PR 3: find_max method Mar 11, 2021
@steve-messing
Copy link

looks so good Chris

Copy link

@scottzec scottzec left a comment

Choose a reason for hiding this comment

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

Beautiful code! I'm wondering if you have any tests for this method

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.

I'm not sure, but maybe "return" be more appropriate than "puts"? Otherwise, the code will continue to run after the print statement and then try to return nil

Copy link

Choose a reason for hiding this comment

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

I think it's great idea, thank you for pointing it out.

Copy link

@trambui09 trambui09 left a comment

Choose a reason for hiding this comment

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

let me know if you have any questions on my comments

Comment on lines +12 to +14
if max < array[index]
max = array[index]
end

Choose a reason for hiding this comment

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

Suggested change
if max < array[index]
max = array[index]
end
max = array[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.

is another shorter option to consider!

index += 1
end
end
return max

Choose a reason for hiding this comment

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

nice job! I think this would correctly return the max. Have you considered writing tests for this method?

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

@indiakato indiakato Mar 12, 2021

Choose a reason for hiding this comment

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

I think a return statement would be better than a puts, then the code wouldn't continue to run and would just return immediately.
ie: return nil

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.

Is it possible to add a return instead of a puts statement for this part? That way the code would stop running in case the array is empty.

Copy link

@OlgaSe OlgaSe left a comment

Choose a reason for hiding this comment

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

The function in general works great, I've added couple of comments please consider them.


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

def find_max(array, max_index)
Copy link

Choose a reason for hiding this comment

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

Would it be more readable to rename variable max_index into length?

Suggested change
def find_max(array, max_index)
def find_max(array, length)

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.

Max variable is initially assigned to nil and then to the array[0], is there a purpose to do that or initial value can be set to array[0]?

@@ -0,0 +1,19 @@

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

@RenCarothers RenCarothers Mar 12, 2021

Choose a reason for hiding this comment

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

This comment is helpful, but you might consider (even if this is a small change) getting into the practice of adding a comment to your PR discussing what you changed/added, how it improves/impacts your code base -- for reviewer folx to be able to better understand your code's goals and scope quickly

Copy link

@ichbinorange ichbinorange left a comment

Choose a reason for hiding this comment

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

Want to check the return value

index += 1
end
end
return max

Choose a reason for hiding this comment

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

if max_index == 0, it prints "Array is empty" and returns max (ie nil), is that what we want?

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.

9 participants