Skip to content

Raymond Fitzgerald #64

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 2 commits 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
118 changes: 105 additions & 13 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.zipcodewilmington;


import java.lang.reflect.Array;
import java.util.*;

/**
* Created by leon on 1/29/18.
*/
Expand All @@ -9,6 +13,7 @@ public class StringArrayUtils {
* @return first element of specified array
*/ // TODO
public static String getFirstElement(String[] array) {

return array[0];
}

Expand All @@ -17,6 +22,7 @@ public static String getFirstElement(String[] array) {
* @return second element in specified array
*/
public static String getSecondElement(String[] array) {

return array[1];
}

Expand All @@ -25,48 +31,91 @@ public static String getSecondElement(String[] array) {
* @return last element in specified array
*/ // TODO
public static String getLastElement(String[] array) {
return null;
String lastElement = array[array.length - 1];
return lastElement;
}

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

/**
* @param array array of String objects
* @param value value to check array for
* @return true if the array contains the specified `value`
*/ // TODO
public static boolean contains(String[] array, String value) {
return false;
public static boolean contains(String[] array, String value) {
for (int i = 0; i < value.length(); i++){
if(array[i] == value);
}
return true;
}

/**
* @param array of String objects
* @return an array with identical contents in reverse order
*/ // TODO
public static String[] reverse(String[] array) {
return null;
List<String> list = Arrays.asList(array);
Collections.reverse(list);
String[] reverseList = list.toArray(array);
return reverseList;
}

/**
* @param array array of String objects
* @return true if the order of the array is the same backwards and forwards
*/ // TODO

//identify midpoint of an array
//identify last index
//for loop

public static boolean isPalindromic(String[] array) {
return false;
List<String> list = Arrays.asList(array.clone());
Collections.reverse(list);
String[] reverseArray = list.toArray(new String[array.length]);
for (int i = 0; i < reverseArray.length; i++) {
String thisString = reverseArray[i];
String thisOtherString = array[i];
if (!thisString.equals(thisOtherString)) {
return false;
}
}
return true;
}


/**
* @param array array of String objects
* @return true if each letter in the alphabet has been used in the array
*/ // TODO
public static boolean isPangramic(String[] array) {
return false;
/* convert array of string to one string
nested for loop with original string on outside and alphabet on inside
every time they match copy that to a new array
remove duplicates
if that array matches array of the alphabet
return true
*/
ArrayList<String> stringOfArrays = new ArrayList<String>(Arrays.asList(array));
String newString = stringOfArrays.toString().toLowerCase();
String aBCS = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < aBCS.length(); i++){
if (newString.indexOf(aBCS.charAt(i)) == -1 ){
return false;
}
}




return true;
}

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

/**
Expand All @@ -84,24 +138,62 @@ 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;
List<String> list = new ArrayList<String>(Arrays.asList(array));
list.remove(valueToRemove);
array = list.toArray(new String[0]);
return array;
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;

ArrayList<String> newArrayList = new ArrayList<String>();
newArrayList.add(array[0]);
for (int i = 1; i < array.length; i++){
if(array[i] != array[i - 1]){
newArrayList.add(array[i]);
}

}


return newArrayList.toArray(new String[newArrayList.size()]);

}



/**
* @param array array of chars
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
* @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;
}
// iterate through array with for loop counter
// identify if character in string is the same as the next character
// if they are same, concatenate
//add concatenation to new string array

/* StringBuilder stringBuilder = new StringBuilder(arrayToString.charAt(i)).append(arrayToString.charAt(i+1));
if(stringBuilder.lastIndexOf())
String newString = stringBuilder.toString();
String[] stringWithSpaces = newString.split(" ");
array = stringWithSpaces;*/
ArrayList<String> list = new ArrayList<String>();
list.add(array[0]);
int counter = 0;
for (int i = 1; i < array.length; i++) {
if(list.get(counter).contains(array[i])){
list.set(counter, list.get(counter) + array[i]);

}else{
counter++;
list.add(array[i]);
}
}
return list.toArray(new String[0]);
}
}