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

Passed all Tests #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>com.zipcodewilmington.labs</groupId>
<artifactId>arrayutils</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down
71 changes: 60 additions & 11 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.zipcodewilmington;

import java.util.*;

/**
* Created by leon on 1/29/18.
*/
Expand All @@ -17,23 +19,24 @@ public static String getFirstElement(String[] array) {
* @return second element in specified array
*/
public static String getSecondElement(String[] array) {
return array[1];
return array[1];
}

/**
* @param array array of String objects
* @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,31 +45,48 @@ 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;
List <String> arrayList = new ArrayList<String>(Arrays.asList(array));
return arrayList.contains(value);
}

/**
* @param array of String objects
* @return an array with identical contents in reverse order
*/ // TODO
public static String[] reverse(String[] array) {
return null;
String [] revArray = new String[array.length];
int newArray = 0;
for (int i = array.length - 1; i >= 0; i--) {
revArray[newArray] = array[i];
newArray++;
}
return revArray;
}

/**
* @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[] revArray = reverse(array);
return Arrays.equals(revArray, array);
}

/**
* @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;
String pangramic = Arrays.toString(array);
pangramic = pangramic.toLowerCase(Locale.ROOT);
if (pangramic.length() < 26) {
return false;
} else
for (char i = 'a'; i <= 'z'; i++) {
if (pangramic.indexOf(i) == -1)
return false;
}
return true;
}

/**
Expand All @@ -75,7 +95,9 @@ 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;
List <String> arrayList = new ArrayList<String>(Arrays.asList(array));
return Collections.frequency(arrayList,value); //collections holds different methods I can use

}

/**
Expand All @@ -84,23 +106,50 @@ 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> arrayList = new ArrayList<String>(Arrays.asList(array));
while (arrayList.contains(valueToRemove)) {
arrayList.remove(valueToRemove);
}
return arrayList.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;

List <String> arrayList = new ArrayList<String>();
String previousElement = "";
for (String element : array) {
if (element != previousElement) { //if the current element is not equal to the previous then add the element, this will pass over the elemets that are the same
arrayList.add(element);
}
previousElement = element;
}


return arrayList.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 lastIndex = 0;
List <String> arrayList = new ArrayList <String>();
arrayList.add(array[0]); //start at index 0
for (int i = 1; i < array.length; i++) { // this will start looping at the 1st index that does not have the same character
if (arrayList.get(lastIndex).contains(array[i])) { // this is saying if the last index has the current character
arrayList.set(lastIndex, (arrayList.get(lastIndex) + array[i])); // will add the duplicate letter to the previous letter
}
else { // if they are not the same, nothing with be packed together because we are missing duplicates
lastIndex++; // go to the next index and have that non duplicate letter start here
arrayList.add(array[i]);
}
}
return arrayList.toArray(new String[0]); // return string array
}


Expand Down