Median in Scala

1,664 views
Skip to first unread message

Jonathan Carpenter

unread,
May 28, 2014, 11:06:46 PM5/28/14
to scala...@googlegroups.com
I'm about two weeks into my first ever Scala course and I have a exercise I could use some help on. I need to write a function to calculate the Median (Middle number) of three values. This is my code so far. def med(a:Double, b:Double, c:Double):Double = () I'm going with type of double so that users would be able to enter floating point numbers into my program. Any ideas about how to calculate the median?
 

Vlad Patryshev

unread,
May 28, 2014, 11:16:00 PM5/28/14
to Jonathan Carpenter, scala-user
Hope you are not banned on Google or wikipedia.

Thanks,
-Vlad


On Wed, May 28, 2014 at 8:06 PM, Jonathan Carpenter <jonath...@gmail.com> wrote:
I'm about two weeks into my first ever Scala course and I have a exercise I could use some help on. I need to write a function to calculate the Median (Middle number) of three values. This is my code so far. def med(a:Double, b:Double, c:Double):Double = () I'm going with type of double so that users would be able to enter floating point numbers into my program. Any ideas about how to calculate the median?
 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Naftoli Gugenheim

unread,
May 29, 2014, 12:08:11 AM5/29/14
to Jonathan Carpenter, scala-user
val List(_, median, _) = List(a,b,c).sorted



On Wed, May 28, 2014 at 11:06 PM, Jonathan Carpenter <jonath...@gmail.com> wrote:
I'm about two weeks into my first ever Scala course and I have a exercise I could use some help on. I need to write a function to calculate the Median (Middle number) of three values. This is my code so far. def med(a:Double, b:Double, c:Double):Double = () I'm going with type of double so that users would be able to enter floating point numbers into my program. Any ideas about how to calculate the median?
 

--

Jonathan Carpenter

unread,
May 29, 2014, 8:39:37 AM5/29/14
to Vlad Patryshev, scala-user
If you were to look up median it tells you how to do it, this is no help in scala as I don’t know how to order items.

Oliver Ruebenacker

unread,
May 29, 2014, 9:35:31 AM5/29/14
to Jonathan Carpenter, Vlad Patryshev, scala-user
--
Oliver Ruebenacker
Be always grateful, but never satisfied.

Som Snytt

unread,
May 29, 2014, 9:46:33 AM5/29/14
to Naftoli Gugenheim, Jonathan Carpenter, scala-user
scala> val _ :: median :: _ = List(1.0, Double.NaN, 2.0).sorted
median: Double = 2.0

scala> val Array(_, median, _) = util.Sorting.stableSort(List(1.0, Double.NaN, 2.0))
median: Double = NaN


to borrow from the puzzlers book.

Oliver Ruebenacker

unread,
May 29, 2014, 9:55:41 AM5/29/14
to Jonathan Carpenter, Vlad Patryshev, scala-user
sorry, didn't mean to be rude, but couldn't resist on that one. :)

Jonathan Carpenter

unread,
May 29, 2014, 10:29:54 AM5/29/14
to Som Snytt, Naftoli Gugenheim, scala-user
It has to be a function, i.e. def med(arguments):output = (equation)

Clint Gilbert

unread,
May 29, 2014, 10:43:20 AM5/29/14
to scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I don't mean to be rude, but this will help people give you
appropriate answers: have you programmed in any language before?

On 05/29/2014 10:29 AM, Jonathan Carpenter wrote:
> It has to be a function, i.e. def med(arguments):output =
> (equation)
>
> On May 29, 2014, at 9:46 AM, Som Snytt <som....@gmail.com
> <mailto:som....@gmail.com>> wrote:
>
>> scala> val _ :: median :: _ = List(1.0, Double.NaN, 2.0).sorted
>> median: Double = 2.0
>>
>> scala> val Array(_, median, _) =
>> util.Sorting.stableSort(List(1.0, Double.NaN, 2.0)) median:
>> Double = NaN
>>
>> to borrow from the puzzlers book.
>>
>>
>>
>> On Wed, May 28, 2014 at 9:08 PM, Naftoli Gugenheim
>> <nafto...@gmail.com <mailto:nafto...@gmail.com>> wrote:
>>
>> val List(_, median, _) = List(a,b,c).sorted
>>
>>
>>
>> On Wed, May 28, 2014 at 11:06 PM, Jonathan Carpenter
>> <jonath...@gmail.com <mailto:jonath...@gmail.com>> wrote:
>>
>> I'm about two weeks into my first ever Scala course and I have a
>> exercise I could use some help on. I need to write a function to
>> calculate the Median (Middle number) of three values. This is my
>> code so far. def med(a:Double, b:Double, c:Double):Double = ()
>> I'm going with type of double so that users would be able to
>> enter floating point numbers into my program. Any ideas about how
>> to calculate the median?
>>
>> -- You received this message because you are subscribed to the
>> Google Groups "scala-user" group. To unsubscribe from this group
>> and stop receiving emails from it, send an email to
>> scala-user+...@googlegroups.com
>> <mailto:scala-user+...@googlegroups.com>. For more
>> options, visit https://groups.google.com/d/optout.
>>
>>
>>
>> -- You received this message because you are subscribed to the
>> Google Groups "scala-user" group. To unsubscribe from this group
>> and stop receiving emails from it, send an email to
>> scala-user+...@googlegroups.com
>> <mailto:scala-user+...@googlegroups.com>. For more
>> options, visit https://groups.google.com/d/optout.
>>
>>
>
> -- You received this message because you are subscribed to the
> Google Groups "scala-user" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to
> scala-user+...@googlegroups.com
> <mailto:scala-user+...@googlegroups.com>. For more options,
> visit https://groups.google.com/d/optout.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iEYEARECAAYFAlOHR4AACgkQ0GFaTS4nYxuy+QCfVYtxmJNmS2ph74sxSLfHGU4e
BksAn0yxiastCk/poIj5Valh/RfsQz6e
=R3ft
-----END PGP SIGNATURE-----

atomly

unread,
May 29, 2014, 12:25:30 PM5/29/14
to Jonathan Carpenter, scala-user
If you really just want somebody to do your homework for you, here you go:

def med(a: Double, b: Double, c: Double) = List(a, b, c).sorted match {
  case _ :: median :: _ => median
}

:: atomly ::

[ ato...@atomly.com : www.atomly.com  : http://blog.atomly.com/ ...
[ atomiq records : new york city : +1.347.692.8661 ...
[ e-mail atomly-new...@atomly.com for atomly info and updates ...


On Wed, May 28, 2014 at 11:06 PM, Jonathan Carpenter <jonath...@gmail.com> wrote:
I'm about two weeks into my first ever Scala course and I have a exercise I could use some help on. I need to write a function to calculate the Median (Middle number) of three values. This is my code so far. def med(a:Double, b:Double, c:Double):Double = () I'm going with type of double so that users would be able to enter floating point numbers into my program. Any ideas about how to calculate the median?
 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.

Jonathan Carpenter

unread,
May 29, 2014, 1:06:17 PM5/29/14
to atomly, scala-user
Thanks for the help… The sarcasm on this forum is terrible. Research is encouraged, I’m not cheating or stealing information. This is one of the least friendly forums I have ever been a part of.

Clint Gilbert

unread,
May 29, 2014, 1:07:09 PM5/29/14
to Jonathan Carpenter, scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(CCing the list)

On 05/29/2014 12:28 PM, Jonathan Carpenter wrote:
> Just scala for about two weeks, i’ve got basic syntax down, I know
> val/vars.
>
> On May 29, 2014, at 10:43 AM, Clint Gilbert
>> -- You received this message because you are subscribed to a
>> topic in the Google Groups "scala-user" group. To unsubscribe
>> from this topic, visit
>> https://groups.google.com/d/topic/scala-user/74i4Kh6ZEaM/unsubscribe.
>>
>>
To unsubscribe from this group and all its topics, send an email to
scala-user+...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iEYEARECAAYFAlOHaTcACgkQ0GFaTS4nYxtSEQCgpVvHNtJ2fUVbHj0kUrvdmH67
IbAAn22YiLUE/GpA/1Oxae3A2uX3Tpr+
=kRpc
-----END PGP SIGNATURE-----

Clint Gilbert

unread,
May 29, 2014, 1:12:17 PM5/29/14
to scala...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'd leave Scala out at first and think through the steps you'd need to
follow to pick the median of three numbers. If someone showed you
three numbers, how would you pick the "middle"? When you can imagine
those steps in some detail, work on expressing them in Scala.
iEYEARECAAYFAlOHamsACgkQ0GFaTS4nYxvA0wCfZJN5Hu6sor0yhZbbnmW7erCw
A40An3GXnc7TFzbGvOXlwlw1rIDXT0vz
=A0RM
-----END PGP SIGNATURE-----

Som Snytt

unread,
May 29, 2014, 2:07:26 PM5/29/14
to Jonathan Carpenter, scala-user
Beginner questions are certainly part of the list's charter.  There are also many eyes on stackoverflow.com and related sites.  It can be easy to get complete and helpful answers there, especially if someone else has already asked the question, because someone will point it out.

I agree that the sarcasm here is terrible.  It used to be quite scathing.

Kevin Wright

unread,
May 29, 2014, 2:19:20 PM5/29/14
to Som Snytt, Jonathan Carpenter, scala-user
To be fair, I think most technical mailing lists will object to questions that appear to be of the form:

  "please do my homework for me"

Without a hint of sarcasm, I can highly recommend that you read this page: http://www.catb.org/esr/faqs/smart-questions.html

Without quoting it verbatim, this bit seems to sum up what you've encountered (emphasis is mine):

...hackers have a reputation for meeting simple questions with what looks like hostility or arrogance. It sometimes looks like we're reflexively rude to newbies and the ignorant. But this isn't really true.

What we are, unapologetically, is hostile to people who seem to be unwilling to think or to do their own homework before asking questions. People like that are time sinks — they take without giving back, and they waste time we could have spent on another question more interesting and another person more worthy of an answer.

Jonathan Carpenter

unread,
May 29, 2014, 2:22:23 PM5/29/14
to Kevin Wright, Som Snytt, scala-user
I understand, and I’m completely sorry. I don’t mean to be a time sink and I don’t want people to do my work for me. What I was asking is for some pointers on how to calculate a median value, I can do the equation, I just had know idea whether to start looking at an if else format or a .method. I apologize for my own rudeness. Thank you all for the help.

Haoyi Li

unread,
May 29, 2014, 2:34:58 PM5/29/14
to Jonathan Carpenter, Kevin Wright, Som Snytt, scala-user
You get as much out of asking questions as you put in. If you had stated exactly what you wanted to know and didn't know how to do, or you had stated the different things you had tried which didn't work, you would have gotten a much warmer, better response.

If you hadn't even bothered googling before asking people, you can't possibly expect to get a meaningful answer form anyone, whether a TA, friend, or person on this list. You actually got *two* meaningful responses, and completely ignored them; if you did this in person and I was a TA, I would probably start getting sarcastic too.


Kevin Wright

unread,
May 29, 2014, 2:35:40 PM5/29/14
to Jonathan Carpenter, Som Snytt, scala-user
Nobody wants to scare you off!

The gist of that post is to "show your working".  Let people know what you've tried and what problems you've had.
That makes it possible to figure out what concepts you're missing, and can be taught, such that you can solve it for yourself.


The "time sink" part comes in when we all have to play 20 questions just to figure that stuff out.

atomly

unread,
May 29, 2014, 3:32:00 PM5/29/14
to Jonathan Carpenter, scala-user

On Thu, May 29, 2014 at 1:06 PM, Jonathan Carpenter <jonath...@gmail.com> wrote:
Thanks for the help… The sarcasm on this forum is terrible. Research is encouraged, I’m not cheating or stealing information. This is one of the least friendly forums I have ever been a part of.

No problem. Sorry if you find the sarcasm overbearing, but it is to be somewhat expected when you ask a question how you did. That said, I hope you keep at it with scala and feel free to ask any questions you have either here or on places like stackoverflow, because I can honestly say that scala is an excellent language and the community is incredibly helpful with any questions you might have, no matter how difficult.

As others said, questions like these give off the impression that you're asking others to do your homework or something similar. If you look, for example, you received two excellent responses (from Naftoli and Som) and, in response to Som's, you said, "It has to be a function, i.e. def med(arguments):output = (equation)" which, once again comes off sounding like you're trying to meet an assignment. If you had instead said something to the effect of, "That's exactly what I need, but how would I turn it into a function call?" you would come off sounding much more grateful and willing to work.

Anyway, I hope my answer worked for you and that you keep at it. If you have any follow-up questions, feel free to ask those as well!

Naftoli Gugenheim

unread,
May 29, 2014, 4:49:57 PM5/29/14
to Jonathan Carpenter, scala-user, atomly

Despite the few unfriendly-sounding responses you got, in general this list is quite friendly and helpful, so I hope you stick around.

Reply all
Reply to author
Forward
0 new messages