Skip to content
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

passes all tests #59

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

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Created by leon on 1/29/18.
*/
Expand All @@ -17,6 +23,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,15 +32,17 @@ 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,6 +51,11 @@ public static String getSecondToLastElement(String[] array) {
* @return true if the array contains the specified `value`
*/ // TODO
public static boolean contains(String[] array, String value) {
for(int i=0;i< array.length;i++){
if(array[i]==value){
return true;
}
}
return false;
}

Expand All @@ -50,23 +64,54 @@ 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[] strArray=new String[array.length];
for(int i=array.length-1,j=0;i>=0;i--,j++){
strArray[j]=array[i];
}
return strArray;
}

/**
* @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[] strArray=new String[array.length];
strArray=array.clone();
for(int i= array.length-1,j=0;i>=0;i--,j++){
if(array[i].equals(strArray[j])){
continue;
}
else 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;
Boolean[] flag=new Boolean[26];
Arrays.fill(flag, false);
String str=Arrays.toString(array).toUpperCase();
if (str == null) {
return false;
}
System.out.println(str);
int alphabetIndex=0;
for(int i=0;i<str.length();i++){
if('A'<=str.charAt(i) && str.charAt(i)<='Z'){
alphabetIndex=str.charAt(i)-'A';
//System.out.println(alphabetIndex);
flag[alphabetIndex]=true;
}
}
for(boolean index: flag){
if(!index)
return false;
}
return true;
}

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

/**
Expand All @@ -84,24 +135,55 @@ 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;
String[] anotherArray=new String[array.length-1];
for(int i=0,k=0;i< array.length;i++){
if(array[i].equals(valueToRemove)){
continue;
}
anotherArray[k++]=array[i];
}
return anotherArray;
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
ArrayList<String> newList = new ArrayList<String>();
newList.add(array[0]);
for (int i = 1; i < array.length; i++) {
// Compare current value to previous
if (array[i - 1] != array[i]) {
newList.add(array[i]);
}
}
//to convert list to string array
String[] strArray = newList.toArray(new String[0]);
return strArray;
}

/**
* @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;
ArrayList<String> newList = new ArrayList<String>();
int count=0;
newList.add(array[0]);
for (int i = 1; i < array.length; i++) {
// Compare current value to previous
String str=newList.get(count);
if (str.contains((array[i]))) {
newList.set(count,str+array[i]);
//count++;
}
else{
count++;
newList.add(array[i]);
}
}
//to convert list to string array
return newList.toArray(new String[0]);
}


}