Median and percentiles iven a data set, the n-th percentile is the value under which n percent of values fall (there are many variants for what this exactly means, please see the link). For stance, given the set of heights of all Finnish adults, the 90th percentile is the smallest height such that 90 percent of these people are shorter than, or the same eight as that. The most commonly used percentile is the median, which is the 50th percentile. s a second example, Tekniikan Akateemiset labor organization regularly publishes in their magazine a plot indicating what are the median as well as the 10th and 0th percentile salaries of people with M.Sc. degree who have graduated in a given year. this small exercise we implement a method that computes percentiles from a sequence of items. The exact definition for the method and its running time equirements are given in the comments above it in the source code. lints: after sorting the input sequence, finding percentiles should be a rather straightforward task. urther information (voluntary) computer science a closely related concept is that of a selection algorithm, which finds the kth smallest number in a sequence. Many selection algorithms, like uickselect, are based on sorting algorithms. Your tasks • Implement the methods marked with ??? in percentiles. scala.
Median and percentiles iven a data set, the n-th percentile is the value under which n percent of values fall (there are many variants for what this exactly means, please see the link). For stance, given the set of heights of all Finnish adults, the 90th percentile is the smallest height such that 90 percent of these people are shorter than, or the same eight as that. The most commonly used percentile is the median, which is the 50th percentile. s a second example, Tekniikan Akateemiset labor organization regularly publishes in their magazine a plot indicating what are the median as well as the 10th and 0th percentile salaries of people with M.Sc. degree who have graduated in a given year. this small exercise we implement a method that computes percentiles from a sequence of items. The exact definition for the method and its running time equirements are given in the comments above it in the source code. lints: after sorting the input sequence, finding percentiles should be a rather straightforward task. urther information (voluntary) computer science a closely related concept is that of a selection algorithm, which finds the kth smallest number in a sequence. Many selection algorithms, like uickselect, are based on sorting algorithms. Your tasks • Implement the methods marked with ??? in percentiles. scala.
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
Your tasks
- Implement the methods marked with ??? in percentiles.scala
- Please use scala to write code
package percentiles:
/**
* Given a non-empty sequence of elements of type T, returns the smallest element e
* in the sequence for which it holds that at least p percent of the elements in the sequence
* are smaller or equal to e.
* Note the special cases:
* - when p == 0, the result should be the smallest element in the sequence
* - when p == 100, the result should be the largest element in the sequence
* If the sequence is empty, java.lang.IllegalArgumentException is thrown.
*
* For a sequence of length n, the method should make at most n*log(n)
* [again, here log means base-2 logarithm] comparison operations between
* the elements in the sequence.
* Furthermore, assuming that comparison is a constant-time operation,
* the whole method should run in O(n*log(n)) time.
* Hint: you can assume that the "sorted" method of Seq performs at most n*log(n) comparisons for
* sequences of length n (the real underlying implementation of "sorted" is currently
* http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort%28T[],%20java.util.Comparator%29 )
*
* Note that there are alternative definitions for "percentile",
* see e.g. http://en.wikipedia.org/wiki/Percentile
*
* Advanced (non-obligatory) stuff: for the implicit keyword below, one can consult, for instance,
* http://www.artima.com/pins1ed/implicit-conversions-and-parameters.html
*/
def percentile[T](p: Int)(seq: Seq[T])(implicit ord: Ordering[T]): T =
require(0 <= p && p <= 100)
require(!seq.isEmpty, "cannot compute percentiles on an empty sequence")
???
end percentile
/**
* Calculate the 10th percentile.
*/
def percentile10[T](seq: Seq[T])(implicit ord: Ordering[T]): T = percentile(10)(seq)
/**
* Calculate the median.
*/
def median[T](seq: Seq[T])(implicit ord: Ordering[T]): T = percentile(50)(seq)
/**
* Calculate the 90th percentile.
*/
def percentile90[T](seq: Seq[T])(implicit ord: Ordering[T]): T = percentile(90)(seq)
/*
* The following functions are only provided for "additional reading"
* to illustrate the use of implicit keyword.
* You can safely ignore them if you wish.
*/
/** Average value of a sequence of Ints*/
def averageInt(seq: Seq[Int]): Double = if seq.isEmpty then 0.0 else seq.sum / seq.length
/** Average value of a sequence of Doubles */
def averageDouble(seq: Seq[Double]): Double = if seq.isEmpty then 0.0 else seq.sum / seq.length
/**
* With implicit conversions and the Numeric package, we can do a generic average
* value function that works for all types T that can be implicitly translated into Numeric[T].
* Performance-wise this is not as good as the specialized methods above!
*/
def average[T](seq: Seq[T])(implicit conv: Numeric[T]): Double =
if seq.isEmpty then 0.0
else
val sum = seq.foldLeft[T](conv.zero)((s, e) => conv.plus(s, e))
conv.toDouble(sum) / seq.length
end average
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 3 steps
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question
Hi, are you sure this is the reworked code? It seems to be the same as the previous code. . . Both previous codes are wrong, including the modified one
Solution
by Bartleby Expert
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY