JUnit: P2J1Test.java public static long fallingPower(int n, int k) Python has the integer exponentiation operator ** conveniently built in the language, whereas Java unfortunately does not offer that operator that would be mostly useless anyway in a language with fixed size integers that silently hide the overflows easily produced by integer exponentiation. (In both languages, the caret character ^ denotes the bitwise exclusive or operation that has bupkis to do with integer exponentiation.) In the related operation of falling power that is useful in many combinatorial formulas and denoted syntactically by underlining the exponent, each term that gets multiplied into the product is always one less than the previous term. For example, the falling power 83 is computed as 8 * 7 * 6 = 336. Similarly, the falling power 105 equals 10 * 9 * 8 * 7 * 6 = 30240. Nothing essential changes even if the base n is negative; the falling power (-4)5 is computed as -4 * -5 * -6 * -7 * -8 = -6720. This method should compute and return the falling power nk where the base n can be any integer, and the exponent k can be any nonnegative integer. (Analogous to ordinary powers, n0 = 1 for any integer n.) The JUnit fuzz tests are designed so that your method does not have to worry about potential integer overflow… provided that you perform your arithmetic calculations with the long kind of 64-bit integers! If you use the bare 32-bit int type, a silent integer overflow will make your method occasionally return incorrect results and fail the JUnit tests, even if that method totes worked correctly when you tried it with your own small values of n.
JUnit: P2J1Test.java
public static long fallingPower(int n, int k)
Python has the integer exponentiation operator ** conveniently built in the language, whereas Java
unfortunately does not offer that operator that would be mostly useless anyway in a language with
fixed size integers that silently hide the overflows easily produced by integer exponentiation. (In
both languages, the caret character ^ denotes the bitwise exclusive or operation that has bupkis to
do with integer exponentiation.)
In the related operation of falling power that is useful in many combinatorial formulas and denoted
syntactically by underlining the exponent, each term that gets multiplied into the product is always
one less than the previous term. For example, the falling power 83 is computed as 8 * 7 * 6 = 336.
Similarly, the falling power 105 equals 10 * 9 * 8 * 7 * 6 = 30240. Nothing essential changes even if
the base n is negative; the falling power (-4)5 is computed as -4 * -5 * -6 * -7 * -8 = -6720.
This method should compute and return the falling power nk where the base n can be any integer,
and the exponent k can be any nonnegative integer. (Analogous to ordinary powers, n0 = 1 for any
integer n.) The JUnit fuzz tests are designed so that your method does not have to worry about
potential integer overflow… provided that you perform your arithmetic calculations with the long
kind of 64-bit integers! If you use the bare 32-bit int type, a silent integer overflow will make your
method occasionally return incorrect results and fail the JUnit tests, even if that method totes
worked correctly when you tried it with your own small values of n.
public static int[] everyOther(int[] arr)
Given an integer array arr, create and return a new array that contains precisely the elements in
the even-numbered positions in the array arr. Make sure that your method works correctly for
arrays of both odd and even lengths, and for arrays that contain zero or only one element. The
length of the result array that you return must be exactly right so that there are no extra zeros at the
end of the array.
public static int[][] createZigZag(int rows, int cols, int start)
This method creates and returns a new two-dimensional integer array, which in Java is really just a
one-dimensional array whose elements are one-dimensional arrays of type int[]. The returned
array must have the correct number of rows that each have exactly cols columns. This array must
contain the numbers start, start+1, … , start+(rows*cols-1) in its rows in sorted order,
except that the elements in each odd-numbered row must be listed in descending order.
For example, when called with rows=4, cols=5 and start=4, this method should create and
return the two-dimensional array whose contents show up as
4 5 6 7 8
13 12 11 10 9
14 15 16 17 18
23 22 21 20 19
when displayed in the standard matrix form whose regular grid is more readable for humans than
the more truthful form of a one-dimensional array that contains one-dimensional arrays of integers
as its row elements.
public static int countInversions(int[] arr)
Inside an array arr, an inversion is a pair of positions i and j inside the array that satisfy
simultaneously both i<j and arr[i]>arr[j]. In combinatorics, the inversion count inside an
array is a rough measure of how much “out of order” that array is. If an array is sorted in ascending
order, it has zero inversions, whereas an n-element array sorted in reverse order has n(n-1)/2
inversions, the largest number possible. (You will encounter inversions again if you work through
the labs 45 to 47 that deal with permutations and their operations.) This method should count the
inversions inside the given array arr, and return that count. As always when writing methods that
operate on arrays, make sure that your method works correctly for arrays of any length, including
the important special cases of zero and one.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images