Skip to content

Generated code to complete all available test cases #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 86 additions & 10 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.zipcodewilmington;

import com.sun.tools.javac.util.ArrayUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* Created by leon on 1/29/18.
*/
public class StringArrayUtils {


/**
* @param array array of String objects
* @return first element of specified array
Expand All @@ -25,15 +35,15 @@ 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];
}

/**
* @param array array of String objects
* @return second to last element in specified array
*/ // TODO
public static String getSecondToLastElement(String[] array) {
return null;
return array[array.length - 2];
}

/**
Expand All @@ -42,23 +52,45 @@ 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 result = false;
for (int i = 0; i < array.length; i++) {
if (array[i].contains(value)) {
result = true;
}
}
return result;
}

/**
* @param array of String objects
* @return an array with identical contents in reverse order
*/ // TODO

public static String[] reverse(String[] array) {
return null;
String[] result = new String[array.length];
int decrement = array.length - 1;
for (int i = 0; i < result.length; i++) {
result[decrement] = array[i];
decrement--;
}
return result;
}

/**
* @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;
int decrement = array.length - 1;
for (int i = 0; i < decrement - 1; i++) {
if (array[i].equals(array[decrement--])) {
return true;
}
if (!array[i].equals(array[decrement--])) {
return false;
}
}
return true;
}

/**
Expand All @@ -75,7 +107,14 @@ 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 numberOfTimes = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(value)) {
numberOfTimes += 1;
}
}

return numberOfTimes;
}

/**
Expand All @@ -84,24 +123,61 @@ 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;
// Generate a Constructor with a new Object
// Add all elements(or indexes) to the newly generated 'arrList'
// call to remove the specific value from the new arrList
// return the arrList casted to a new String Array starting at the 0'th index
ArrayList<String> arrList = new ArrayList<String>();
Collections.addAll(arrList, array);
arrList.remove(valueToRemove);
return arrList.toArray(new String[0]);
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
// this returns out of bounds exception for 2/3 (2/3 are 10 indexes (11 places)
// test 1 returns array lengths differ/ (9 indexes (10 places)
ArrayList<String> arrList = new ArrayList<String>();
Collections.addAll(arrList, array);
for (int i = 1; i < arrList.size() - 1; i++) {
if (arrList.equals(i) == arrList.equals(i + 1)) {
arrList.remove(i);
}
}
return arrList.toArray(new String[0]);
}

/**
* @param array array of chars
* @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;
}

//

int amountOfChars = 1;
for (int i = 0; i < array.length - 1; i++) {
if (array[i] != array[i + 1]) {
amountOfChars++;
}
}
String[] newArr = new String[amountOfChars];
Integer index = 0;
String stringToBeAdded = "";
stringToBeAdded += array[0];
for (int i = 0; i < array.length; i++) {
if (array[i] == array[i - 1]) {
stringToBeAdded += array[i - 1];
} else if (array[i] != array[i - 1]) {
newArr[index] = stringToBeAdded;
stringToBeAdded = array[i];
index++;
}
}
newArr[index] = stringToBeAdded;
return newArr;
}
}