public class Practice1B extends Object
Modifier and Type | Method and Description |
---|---|
static int |
alternatingSum(List<Integer> t)
Computes the alternating sum of the integers in the argument
list.
|
static List<Integer> |
encode(List<Integer> t)
Returns a run length encoded list representing the numbers in the
the list
t . |
static Integer |
last(List<Integer> t)
Returns the last integer in the argument list.
|
static int |
totalArea(List<Integer> widths,
List<Integer> heights)
Computes the sum of the areas of zero or more rectangles.
|
public static Integer last(List<Integer> t)
t
- a list of integerspublic static int totalArea(List<Integer> widths, List<Integer> heights)
widths [1, 5, 10] heights [2, 3, 4]
The sum of the areas is equal to (1 * 2 + 5 * 3 + 10 * 4)
.
If a width or height of a rectangle is less than zero then
that area is not included in the sum.
widths
- a non-null list of rectangle widthsheights
- a non-null list of rectangle heightsIllegalArgumentException
- if widths and heights are lists
of different sizespublic static int alternatingSum(List<Integer> t)
[0, 1, 2, 3, 4, 5]
The alternating sum is given by (0 - 1 + 2 - 3 + 4 - 5). The alternating sum for an empty list is zero.
t
- a non-null list of integerspublic static List<Integer> encode(List<Integer> t)
t
. The list t
is not modified by this method.
Consider the list containing twelve ones:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
A run length encoded version of the list would be:
[12, 1] (twelve 1s)
Similarly, consider the list containing four tens and three fives:
[10, 10, 10, 10, 5, 5, 5]
A run length encoded version of the list would be:
[4, 10, 3, 5] (four 10s, three 5s)
If the list contains few or no repeated values, then the run length encoded version of the list is longer than the original list. For example, the run length encoded version of the list:
[1, 2, 3, 4]
is:
[1, 1, 1, 2, 1, 3, 1, 4] (one 1, one 2, one 3, one 4)
t
- a list of integer values