diff --git a/pom.xml b/pom.xml
index d10c35e..6e775f1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,6 +7,18 @@
com.zipcodewilmington.labs
arrayutils
1.0-SNAPSHOT
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 15
+ 15
+
+
+
+
junit
diff --git a/src/main/java/com/zipcodewilmington/StringArrayUtils.java b/src/main/java/com/zipcodewilmington/StringArrayUtils.java
index 4bcce66..cdf2866 100644
--- a/src/main/java/com/zipcodewilmington/StringArrayUtils.java
+++ b/src/main/java/com/zipcodewilmington/StringArrayUtils.java
@@ -1,5 +1,8 @@
package com.zipcodewilmington;
+import java.util.ArrayList;
+import java.util.Arrays;
+
/**
* Created by leon on 1/29/18.
*/
@@ -8,9 +11,8 @@ public class StringArrayUtils {
* @param array array of String objects
* @return first element of specified array
*/ // TODO
- public static String getFirstElement(String[] array) {
- return array[0];
- }
+ public static String getFirstElement(String[] array) { return array[0]; }
+
/**
* @param array array of String objects
@@ -25,7 +27,7 @@ public static String getSecondElement(String[] array) {
* @return last element in specified array
*/ // TODO
public static String getLastElement(String[] array) {
- return null;
+ return array[array.length - 1];
}
/**
@@ -33,7 +35,7 @@ public static String getLastElement(String[] array) {
* @return second to last element in specified array
*/ // TODO
public static String getSecondToLastElement(String[] array) {
- return null;
+ return array[array.length - 2];
}
/**
@@ -42,7 +44,13 @@ public static String getSecondToLastElement(String[] array) {
* @return true if the array contains the specified `value`
*/ // TODO
public static boolean contains(String[] array, String value) {
- return false;
+ boolean containValue = false;
+ for(int i = 0; i < array.length; i++){
+ if (array[i] == value) {
+ containValue = true;
+ }
+ }
+ return containValue;
}
/**
@@ -50,15 +58,31 @@ public static boolean contains(String[] array, String value) {
* @return an array with identical contents in reverse order
*/ // TODO
public static String[] reverse(String[] array) {
- return null;
+ String[] reversedArray = new String[array.length];
+ int indexOfReversedArray = 0;
+ for(int indexOfArray = 0; indexOfArray < array.length; indexOfArray++) { //filter through array
+ reversedArray[indexOfReversedArray] = array[array.length - indexOfArray - 1]; //assigns reversedOrderArray at first position to array at last position
+ indexOfReversedArray++; //increase for next index of ReversedArray
+ }
+ return reversedArray;
}
+
/**
* @param array array of String objects
* @return true if the order of the array is the same backwards and forwards
*/ // TODO
public static boolean isPalindromic(String[] array) {
- return false;
+ String[] reversedOrderArr = reverse(array);
+ return Arrays.equals(reversedOrderArr, array);
+ /*for (int indexOfArray = 0; indexOfArray < reversedOrderArr.length; indexOfArray++) {
+ String currentStringReversed = reversedOrderArr[indexOfArray];
+ String currentStringForward = array[indexOfArray];
+ if(!currentStringReversed.equals(currentStringForward)){
+ return false;
+ }
+ }
+ return true;*/
}
/**
@@ -66,7 +90,19 @@ public static boolean isPalindromic(String[] array) {
* @return true if each letter in the alphabet has been used in the array
*/ // TODO
public static boolean isPangramic(String[] array) {
- return false;
+ String stringArray = Arrays.toString(array); //change array to string
+ stringArray = stringArray.toLowerCase();
+
+ if(stringArray.length() < 26 ){ //does the sentence have 26 characters? if less, does not contain every letter
+ return false;
+ } else {
+ for (char letters = 'a'; letters <= 'z'; letters++) {
+ if (stringArray.indexOf(letters) < 0) {
+ return false;
+ }
+ }
+ return true;
+ }
}
/**
@@ -75,7 +111,13 @@ public static boolean isPangramic(String[] array) {
* @return number of occurrences the specified `value` has occurred
*/ // TODO
public static int getNumberOfOccurrences(String[] array, String value) {
- return 0;
+ int numOfValueOccurrences = 0;
+ for(int i = 0; i < array.length; i++){
+ if(array[i] == value){
+ numOfValueOccurrences++;
+ }
+ }
+ return numOfValueOccurrences;
}
/**
@@ -84,7 +126,16 @@ public static int getNumberOfOccurrences(String[] array, String value) {
* @return array with identical contents excluding values of `value`
*/ // TODO
public static String[] removeValue(String[] array, String valueToRemove) {
- return null;
+ int removeCount = 0;
+ int j = 0;
+ String[] newArray = new String[(array.length - 1) - removeCount];
+ for(int i = 0; i < array.length; i++){
+ if (array[i] != valueToRemove) {
+ newArray[j++] = array[i];
+ }else removeCount++;
+
+ }
+ return newArray;
}
/**
@@ -92,7 +143,22 @@ public static String[] removeValue(String[] array, String valueToRemove) {
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
- return null;
+ int counter = 0;
+ String listOfSingles = "";
+ String lastString = "";
+ String[] newArray = new String[counter];
+
+ for(String currentString : array){ //loop through array
+ if(currentString == lastString){ //if current string is same as last string, assign to last string and continue
+ lastString = currentString;
+ } else if(currentString != lastString){ //if they are different..
+ listOfSingles += currentString + " "; //add the current string to the list
+ lastString = currentString; //assign to last string for comparison next loop
+ counter++; //add to the counter
+ }
+ }
+ newArray = listOfSingles.split(" ");
+ return newArray;
}
/**
@@ -100,8 +166,19 @@ public static String[] removeConsecutiveDuplicates(String[] array) {
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
*/ // TODO
public static String[] packConsecutiveDuplicates(String[] array) {
- return null;
- }
+ ArrayList newList = new ArrayList<>();
+ newList.add(array[0]); //input this array to newList
+ int listIndex = 0; //index of newList
+ for(int i = 1; i < array.length; i++){
+ if(newList.get(listIndex).contains(array[i])) { //checks if the new list contains array[i] at its most recent index
+ newList.set(listIndex, (newList.get(listIndex) + array[i])); //if yes, adds character to newList without increasing the index of newList AND groups duplicates together
+ }else { //not consecutive
+ listIndex++; //increase index of newList
+ newList.add(array[i]); //add array[i] to newList
+ }
+ }
+ return newList.toArray(new String[0]); //convert to array
+ }
}