Forum: Ruby array max value

Ab6f6d347725dbaa2e8d1d3f34d393d2?d=identicon&s=25 CC Chen (bigdicky)
on 2011-07-21 08:27
When I set an array k = [3,5,6,0,1,4,5,6,9,1]

And I used the k.max can know the max value is 9

How do I get the number "8" (as k[8] = 9)??



Thanks,
197480de020954041de6366056d080c5?d=identicon&s=25 Michael Edgar (Guest)
on 2011-07-21 08:33
(Received via mailing list)
On Jul 21, 2011, at 2:27 AM, CC Chen wrote:

> When I set an array k = [3,5,6,0,1,4,5,6,9,1]
>
> And I used the k.max can know the max value is 9
>
> How do I get the number "8" (as k[8] = 9)??

k.index(k.max) will work. It's not completely optimal, but it's concise
and asymptotically optimal.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2011-07-21 08:58
Michael Edgar wrote in post #1012048:
> On Jul 21, 2011, at 2:27 AM, CC Chen wrote:
>
>> When I set an array k = [3,5,6,0,1,4,5,6,9,1]
>>
>> And I used the k.max can know the max value is 9
>>
>> How do I get the number "8" (as k[8] = 9)??
>
> k.index(k.max) will work. It's not completely optimal, but it's concise
> and asymptotically optimal.

:-)

Here's another approach which is a tad more involved and iterates the
array only once:

irb(main):001:0> k = [3,5,6,0,1,4,5,6,9,1]
=> [3, 5, 6, 0, 1, 4, 5, 6, 9, 1]
irb(main):002:0> k.each_with_index.max_by {|e,i| e}.last
=> 8

Kind regards

robert
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2011-07-21 12:31
(Received via mailing list)
On Thu, Jul 21, 2011 at 3:27 PM, CC Chen <dickyhide@gmail.com> wrote:
> When I set an array k = [3,5,6,0,1,4,5,6,9,1]
>
> And I used the k.max can know the max value is 9
>
> How do I get the number "8" (as k[8] = 9)??
>
>

What if you have an array like this [3,5,6,0,1,4,5,6,9,1,9] ?
Do you want 8 or 10 or both?

If you want both you could try something like this.


per = [3,5,6,0,1,4,5,6,9,1,9]

so_my_jacket, wont_open, now = [], per.max, per.size # because,
(0...now).zip(per){|is_stuck, shut| so_my_jacket << is_stuck if
shut==wont_open}

p so_my_jacket #> [8, 10]


OR


k = [3,5,6,0,1,4,5,6,9,1,9]
m,s,r = k.max,k.size,[]
(0...s).zip(k){|x,i| r <<  x if i == m}
p r   #> [8, 10]



Harry
99804651e19d8b0dffbc0da53e39114e?d=identicon&s=25 Adam Prescott (Guest)
on 2011-07-21 13:02
(Received via mailing list)
On Thu, Jul 21, 2011 at 11:30 AM, Harry Kakueki <list.push@gmail.com>
wrote:

> per = [3,5,6,0,1,4,5,6,9,1,9]
>
> so_my_jacket, wont_open, now = [], per.max, per.size # because,
> (0...now).zip(per){|is_stuck, shut| so_my_jacket << is_stuck if
> shut==wont_open}
>
> p so_my_jacket #> [8, 10]
>

Excellent example. :)
E755752b8dbe10787b31214c817da87f?d=identicon&s=25 esha d. (esha_d)
on 2011-07-28 14:16
Try following :

irb(main):035:0> k = [9, 9, 6, 0, 1, 4, 5, 6, 9, 1, 9]
=> [9, 9, 6, 0, 1, 4, 5, 6, 9, 1, 9]

irb(main):034:0> k.each_with_index.collect{|t,i| i if t ==
k.max}.compact
=> [0, 1, 8, 10]

Esha
C6c2432a4223656cc1b0d4e2746bfadf?d=identicon&s=25 Lunatic Leo (lleo)
on 2011-07-28 17:00
@esha_d you walk the n-element array once for the collect call. And
within each collect call you walk the array via k.max. That is O(n^2)
if my vague recollection of uni serves me. Each call to k.max requires
a walk of the array, unless the interpreter used caches k.max after the
first walk.

The way I see this working (please check me):
k.each_with_index creates an enumerator so no looping there. But collect
walks the array (with the embedded k.max). Then compact must walk the
result of collect to strip out the nil's.

Given we know k doesn't change over the collect loop we can pull the
k.max out.
k_max = k.max;
Then you loop over a k.size array once for the initial k.max, once for
the collect, and a last time for the compact.

It is possible to write the loop to walk the k array once with an each.
And loop over the result set once to strip out the indexes.

a = [9, 9, 6, 0, 1, 4, 5, 6, 9, 1, 9]
b = []
a.each_with_index do |e,i|
  cur_max = (b.empty?) ? e : b[0][1]
  if e > cur_max
    b = [[i,e]]
  elsif e == cur_max
    b.push [i,e]
  end
end
p b.collect {|e| e[0] }

Algorithmically both approaches are O(n), other than the k.max thing.
However, it is clear that one is 3n versus n+m (where m is the result
set size).

-LLeo
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2011-07-28 20:53
(Received via mailing list)
On Thu, Jul 21, 2011 at 3:27 PM, CC Chen <dickyhide@gmail.com> wrote:
> When I set an array k = [3,5,6,0,1,4,5,6,9,1]
>
> And I used the k.max can know the max value is 9
>
> How do I get the number "8" (as k[8] = 9)??
>
>
>



k = [3,5,6,0,1,4,5,6,9,1,9]

m,s = k.max,k.size
p (0...s).select{|x| k[x]==m}



Not as easy to make a goofy story out of this one :)

Harry
C6c2432a4223656cc1b0d4e2746bfadf?d=identicon&s=25 Lunatic Leo (lleo)
on 2011-07-29 00:42
@Harry Thanks. I am new to ruby. I look at these kinds of things to help
my understanding. My "solution" was very strained and I didn't like it.


-LLeo
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.