Reputation: 49
I need to find the max value of an array without using the max
method. Right now I have:
def max(arr)
largest_num = arr(1)
arr.each do |num|
if element >= largest_num
largest_num = num
else
largest_num = largest_num
end
puts largest_num
end
my_numbers = [20, 30, 40, 50]
puts max(my_numbers)
end
Upvotes: 0
Views: 2978
Reputation: 1
First sort the array without using the sort method if you are trying to solve this without the default function
asc = [45,34,12,4,5,32,54,76]
asc.each do |a|
i = 0
asc.each do |b|
if b > asc[i +1]
asc[i], asc[i + 1] = asc[i + 1], asc[i]
end
i += 1 if i < asc.size - 2
end
end
p asc.last
This will give the largest number from the sorted array
Upvotes: 0
Reputation: 4362
If you are required to use sort
and last
, then the answer below will not work for you.
arr.sort.last
is inefficient, since it requires sorting the whole array when all you need is the biggest. Try something like:
arr.reduce{|largest, num| if num > largest then num else largest end}
The reduce
function (also aliased as inject
) is used to "reduce" an array down to a single object. To implement a sum, you could do something like:
num_arr = [1,2,3,4,5]
num_arr.reduce(0){|sum, num| sum + num} #=> 15
0
is the starting value of the sum, and then for each element the block is run with the sum so far and the element in the array. Then the result of the block (sum + num
is returned since in ruby the last statement is implicitly returned) is set as the new value of sum
for the next element. The final value of sum
is what is returned.
This is similar to doing:
sum = 0
sum = sum + num_arr[0]
sum = sum + num_arr[1]
sum = sum + num_arr[2]
sum = sum + num_arr[3]
sum = sum + num_arr[4]
If you don't specify a starting value, then the first element is taken as the starting value. So, in my reduce
solution, the first element is set as the "largest", and then for each element in turn the largest is either passed on or if the element is bigger than the current largest, it becomes the new largest.
Upvotes: 1
Reputation: 71
If you were really told that you should use sort
and last
then the answer is quite simple:
2.1.1 :003 > [2,3,1,4,8,6].sort.last
=> 8
If you want it in method:
def max(arr); arr.sort.last; end
2.1.1 :004 > def max(arr); arr.sort.last; end
=> :max
2.1.1 :005 > max([2,1,3])
=> 3
Upvotes: 0
Reputation: 160551
i was told i should use .sort and .last but not quite sure where to even start
It's really important to be a self-starter when programming. It's an essential characteristic, because the field and all technologies are moving quickly. I'd suggest reading "How much research effort is expected of Stack Overflow users?" also, especially if you want to use Stack Overflow as a resource.
Here's how to learn to experiment with Ruby and to teach yourself:
Ruby has IRB bundled with it. Type irb
at the command-line and it should open and present you with a prompt. At that prompt you can enter Ruby expressions and see the result. My prompt is probably not the same as yours because mine is customized, but you can figure out how to work with that:
$ irb
irb(main):001:0>
To assign an array to a variable:
irb(main):001:0> my_numbers = [20, 30, 40, 50]
=> [20, 30, 40, 50]
I can look at the value assigned to my_numbers
by entering the name of the variable and pressing Return or Enter:
irb(main):002:0> my_numbers
=> [20, 30, 40, 50]
I can experiment using methods on my_numbers
:
irb(main):003:0> my_numbers.shuffle
=> [50, 30, 40, 20]
That took the array and randomized it. It didn't change my_numbers
, it only shuffled the array and output a new array in the shuffled order:
irb(main):004:0> my_numbers
=> [20, 30, 40, 50]
Each time shuffle
is run it randomizes the array and returns another array:
irb(main):005:0> my_numbers.shuffle
=> [50, 20, 30, 40]
irb(main):006:0> my_numbers.shuffle
=> [40, 20, 30, 50]
Since you want to use sort
and last
to find the maximum value, it'd be good to start with an out-of-order array. Testing sort
:
irb(main):009:0> my_numbers.shuffle.sort
=> [20, 30, 40, 50]
The array is sorted after shuffling.
Here's what last
does:
irb(main):010:0> my_numbers.last
=> 50
Now you know enough to figure it out for yourself.
Upvotes: 1
Reputation: 106802
Why don't you just use sort
and last
?
array = [3,7,2,4,6,1,8,5]
array.sort.last
#=> 8
Upvotes: 4