Find the largest value of given 3 values

15 views
Skip to first unread message

Maddy

unread,
Sep 18, 2012, 1:47:12 AM9/18/12
to rubyonra...@googlegroups.com
Hi folks,

Good day!

I want to find the largest value of given 3 values.

Please suggest me,the simple ways of finding the largest value.


Manoj M.

unread,
Sep 18, 2012, 2:00:42 AM9/18/12
to rubyonra...@googlegroups.com
Maddy wrote in post #1076411:
> Hi folks,
>
> Good day!
>
> *I want to find the largest value of given 3 values.*
>
> *Please suggest me,the simple ways of finding the largest value.*

if a,b and c are numbers

if(a>b)
{
if(a>c)
{("a is largest")}
else
{("c is largest")}
}
else
{
if(b>c)
{("b is largest")}
else
{("c is largest")}
}

--
Posted via http://www.ruby-forum.com/.

rovin varshney

unread,
Sep 18, 2012, 2:40:37 AM9/18/12
to rubyonra...@googlegroups.com
simply put all numbers in array and call max method.

a=[2,3,4]
a.max



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.

Manoj M.

unread,
Sep 18, 2012, 3:04:00 AM9/18/12
to rubyonra...@googlegroups.com
rovin varshney wrote in post #1076418:
> simply put all numbers in array and call max method.
>
> a=[2,3,4]
> a.max

yes Rovin ruby way :)

roh

unread,
Sep 18, 2012, 5:42:47 AM9/18/12
to rubyonra...@googlegroups.com
if we want to get the highest 3 values , then how can we write the code for that .?

7stud --

unread,
Sep 19, 2012, 12:13:31 AM9/19/12
to rubyonra...@googlegroups.com
roh wrote in post #1076535:
> if we want to get the highest 3 values , then how can we write the code
> for
> that .?

data = [10, 20, 70, 60, 40, 30]
ordered_data = data.sort_by {|num| -num}

p ordered_data
p ordered_data[0..2]


--output:--
[70, 60, 40, 30, 20, 10]
[70, 60, 40]

Frederick Cheung

unread,
Sep 19, 2012, 3:28:15 AM9/19/12
to Ruby on Rails: Talk


On Sep 19, 5:14 am, 7stud -- <li...@ruby-forum.com> wrote:
> roh wrote in post #1076535:
>
> > if we want to get the highest 3 values , then how can we write the code
> > for
> > that .?
>
> data = [10, 20, 70, 60, 40, 30]
> ordered_data = data.sort_by {|num| -num}
>
> p ordered_data
> p ordered_data[0..2]

Or data.sort.last(3)

I think you might need ruby 1.9 to be able to pass an argument to last
like that.

Fred
Reply all
Reply to author
Forward
0 new messages