15 points
So, what are the populations of every town in North Carolina and the number of posts by various authors to a Duke sports bulletin board hiding from you?
n
and a list of numbers
nums
and outputs a list of 10 values: the frequency with
which each digit 0–9 appears as the n
th
digit of one of the input numbers.
We provide you with a suggested decomposition of the problem into functions that you can implement. However, you can ignore the framework below and create whatever methods you like as long as you produce the same output in the end.
Note: throughout this problem, you may
assume that the numbers processed are non-negative or you can use the
absolute value function Math.abs
to help you handle
negative numbers in a reasonable way.
nthDigit
.
nthDigit(n,num)
finds the n
th
highest order digit of num
, i.e., the
n
th digit from the left. We take the
leftmost digit to be the 0
th.
nthDigit
should evaluate to 0
for digits
beyond the "end" of the number. For example:
When computing
nthDigit(0,678)
⇒ 6
nthDigit(1,678)
⇒ 7
nthDigit(2,678)
⇒ 8
nthDigit(3,678)
⇒ 0
nthDigit(0,0)
⇒ 0
nthDigit(3,18023)
⇒ 2
nthDigitTally
, using nthDigit
. nthDigitTally(n, nums)
returns a tally of frequencies of 0–9 as the
n
th digits of all the numbers in
nums
.
Here's a sample test case. These are enrollments in Research Triangle Park colleges and universities in Fall 2000 (thanks to the "Research Triangle Regional Partnership" website: http://www.researchtriangle.org/data/enrollment.html).
Institution | Enrollment |
---|---|
Duke University | 12176 |
North Carolina Central University | 5476 |
Louisburg College (Junior College) | 543 |
Campbell University | 3490 |
University of North Carolina at Chapel Hill | 24892 |
North Carolina State University | 28619 |
Meredith College | 2595 |
Peace College | 603 |
Shaw University | 2527 |
St. Augustine's College | 1465 |
Southeastern Baptist Theological Seminary | 1858 |
enrollments
contains the enrollment
numbers from that table. Then:
nthDigitTally(0, enrollments)
⇒
[0,3,4,1,0,2,1,0,0,0]
readNumbers
that reads
whitespace-separated integers from a Scanner
and returns a list of the numbers suitable as input to
nthDigitTally
. Here's the university enrollment data
from above:
12176 5476 543 3490 24892 28619 2595 603 2527 1465 1858From this,
readNumbers
should produce the list
[12176, 5476, 543, 3490, 24892, 28619, 2595, 603, 2527, 1465,
1858]
.
main
method to prompt the user for the number
n
and to choose a file for the data set. The program should tally the
n
th digits of the numbers in the data set and
print out a table of the results. For example, given that n
=0
and the following file:
README
, you should describe the distribution of
digits for each of the data files and how they conform or differ
from what you would expect from a random list of numbers.
readNumbers
to ignore anything between
(* and *). (You may assume that the (* and
*) symbols will be surround by whitespace and that nested
comments — comments inside other comments — are not
allowed.) Now, the unversity data set can look like:
(* Duke University *) 12176 (* North Carolina Central University *) 5476 (* Louisburg College (Junior College) *) 543 (* Campbell University *) 3490 (* University of North Carolina at Chapel Hill *) 24892 (* North Carolina State University *) 28619 (* Meredith College *) 2595 (* Peace College *) 603 (* Shaw University *) 2527 (* St. Augustine's College *) 1465 (* Southeastern Baptist Theological Seminary *) 1858
readNumbers
. The data must all be separate
measurements of a single type of phenomenon. For example:
measurements of university/college enrollments across different
institutions (like above) or at the same institution across different
years; measurements of the flow rates of all the major rivers in
British Columbia; measurements of the height of 10000 randomly chosen
Vancouver residents; measurements of the number of hits per day on the
UBC computer science website over three years; measurements of the
length in characters of each article in the Wikipedia; measurements of
the population of the 1000 largest cities and townships in Canada;
etc. Furthermore, there must be at least 250 measurements in
the list (but more would be better!).
readNumbers
, and one attachment with
labelled data (using the (* *)
style above).
nthDigitTally
). Are there any oddities in
the tallies? What about in other students' data?
Math.log10
method.
Math.pow
method to avoid repeated division.
.equals
method. For example,
if some String token
is equal to (*, then
token.equals("(*")
will evaluate to
true
.