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.

@CheezItMan CheezItMan changed the title find_max method Pr-3 Aug 13, 2019
Copy link

@ranuseh ranuseh left a comment

Choose a reason for hiding this comment

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

Nothing to add. All good.

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.

what if max index is a negative number? then the max should be 0 and should return 0 instead of nil


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

You can do better than this!

Choose a reason for hiding this comment

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

Suggested change
def find_max(array, max_index)
def find_max(array)
if array.nil?
return "Array has no max element"
end
max = nil
idx = 0
while max < array.length
if max < array[idx]
max = array[idx]
end
idx ++
end
return "Max element is " + max
end

Copy link

@shubha-rajan shubha-rajan left a comment

Choose a reason for hiding this comment

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

Please see my comments. I left a few suggestions for improving the clarity of output messaging and handling edge cases.

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.

Array is empty might be a confusing message to print out since a user can call the method on an array that isn't empty, but set the max index to zero or negative.

Suggested change
puts "Array is empty"
if max_index == 0
puts "Array has no elements in the given range"
return nil
else if max_index < 0
puts "Invalid range: Max index can't be less than zero"
return nil

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

def find_max(array, max_index)
max = nil
Copy link

@shubha-rajan shubha-rajan Aug 15, 2019

Choose a reason for hiding this comment

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

the only cases in which max array should return nil are where max_index is zero or negative. There is explicit messaging for max_index==0, but nothing for negative. Maybe return nil in both those cases (with explicit messaging) and initialize max later in the else condition?

Suggested change
max = nil

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.

What happens here if max_index is greater than array.length?


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

max_index as a variable name sounds misleading, is this variable supposed to represent the length of the array?

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

def find_max(array, max_index)
max = nil

Choose a reason for hiding this comment

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

Edge case for if array is nil


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

+- does max_index need to be a param? Can you define it inside method if array provided?

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.

does it make sense here to return array[0] if max_index is negative?

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

Choose a reason for hiding this comment

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

Suggested change
puts "Array is empty"
# will also need to check first that array.length != nil
return array[0]

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

Choose a reason for hiding this comment

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

max is initialized as nil at the beginning and then reassigned when the else statement gets executed. It might be ok to either initialize it at the beginning as max = array[0] and delete line 9.


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.

If we check for max_index > 0 here and then execute the code in the else statement, max can be declared and returned from within the same statement.

Copy link

@amythetester amythetester left a comment

Choose a reason for hiding this comment

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

Might want to consider some of these changes, we might also want to look for more edge cases.

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.

Suggested change
puts "Array is empty"
return 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.

Suggested change
while index < max_index
while index <= max_index && index <= array.length - 1

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

def find_max(array, max_index)
max = nil

Choose a reason for hiding this comment

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

This seems not needed.

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

def find_max(array, max_index)
max = nil

Choose a reason for hiding this comment

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

Suggested change
max = nil
if array.length == 0
return "Array is empty"
end


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

Consider finding the end of the array programmatically using length.

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

def find_max(array, max_index)
max = nil
Copy link

Choose a reason for hiding this comment

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

this is redundant since it's defined again in theelse statement


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

max_index is an unclear variable name - would length be more readable?

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

def find_max(array, max_index)
max = nil
Copy link

Choose a reason for hiding this comment

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

it's not necessary to declare max at this point since it's declared on line 9 as well.

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

def find_max(array, max_index)
max = nil

Choose a reason for hiding this comment

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

do we need to define this here or can we wait and define it on line 9?


# 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 max_index the max index of array? if so, could we just find that inside the method instead of passing it in as a parameter?


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

Do we want to change this function name to something more descriptive? Maybe 'find_max_value', or 'find_max_array_value'?

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.

Should this be <= instead of < ?

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.