Exercises

exercise No. 164

Various integer array algorithms Create comment in forum

Q:

The following Maven project contains a series of yet unimplemented methods and corresponding tests currently being excluded by an @Disabled annotation:

...
@Disabled // Remove me to enable testing your application
@SuppressWarnings("javadoc")
public class ArrayMethodTest {

  @Test
  public void testFindIndex(){
    assertEquals(Arraymethods.findIndex(new int[]{1, 3, -3, 5}, -3), 2);...

Import this project for getting started. The following hints may help you completing the implementation

swap

This effectively requires extending the concept of swapping just two integer values within a block

int a = 3, b = 5;

// Other code ...

{// Swap values of a and b
  final int tmp = a;
  a = b;
  b = tmp;
}
isPalindrome

Consider a two step implementation:

  1. Normalize a given palindrome candidate by transforming to lower case and erasing non-letters:

    Hey, Roy! Am I mayor? Yeh! --> heyroyamimayoryeh

    You may search the API of class Character assisting you to distinguish letters from non-letters.

  2. Check the remaining string for being a palindrome.

containsSameElements

You may copy int[] b array to a shadow array and then subsequently erase all elements of int[] a from this copy. The method findIndex is quite helpful.

Consider for example int[] bCopy = {1, 3, 4, 3, 7} containing 5 elements. Suppose our array a contains the value 3 which exists at index position 1 in bCopy. We may override the value index position 1 by the last array value 7 and thereby keeping track of reducing the number of array elements to 4 like {1, 7, 4, 3}.

Off course the array bCopy cannot shrink. But we may introduce an integer variable to account for the effective number of array elements still to be considered. If and only if all elements from a are subsequently found within bCopy the two arrays a and b are equal.

A: