-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Pr-3 #3
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 | |
There was a problem hiding this 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" |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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?
max = nil |
else | ||
max = array[0] | ||
index = 0 | ||
while index < max_index |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this 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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
puts "Array is empty" | |
return array[0] |
else | ||
max = array[0] | ||
index = 0 | ||
while index < max_index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 < ?
This method find the maximum value of an array.