Skip to content

Passed all tests #56

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
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
98 changes: 88 additions & 10 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 java.awt.*;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;

/**
* Created by leon on 1/29/18.
*/
Expand All @@ -25,23 +31,30 @@ 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];
}

/**
* @param array array of String objects
* @param value value to check array for
* @return true if the array contains the specified `value`
*/ // TODO
*/ // Ask about:
// List<String> nameList = new ArrayList<>(Arrays.asList(array));
// return nameList.contains(value);
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 +63,44 @@ 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[] reversedArr = new String[array.length];
for(int i = 0; i < array.length; i++){
reversedArr[i] = array[array.length - (i+1)];
}
return reversedArr;
}

/**
* @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;
for(int i = 0; i < array.length; i++){
if(array[i] != array[array.length - (i+1)]){
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;
//Need a stringBuffer to hold all array strings together AND use .append
StringBuffer oneString = new StringBuffer();
for(String string: array){
oneString.append(string);
}
//Turn stringBuffer into String
String fullString = oneString.toString().toLowerCase();
//Delete everything thats not a letter
String lettersOnly = fullString.replaceAll("(.)(?=.*\\1)", "");
//Delete all repeating letters
String noRepeatChars = lettersOnly.replaceAll("[^a-z]", "");
//Return true if string has 26 letters.
return noRepeatChars.length() == 26;
}

/**
Expand All @@ -75,7 +109,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 count = 0;
for (String string: array) {
if(string.equals(value)){
count++;
}
}
return count;
}

/**
Expand All @@ -84,23 +124,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;
int index = 0;
for(String string: array){
if(string != valueToRemove){
array[index++] = string;
//index++;
}
}
String[] newArray = new String[index];
for(int i = 0; i < index; i++){
newArray[i] = array[i];
}
return newArray;
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
ArrayList<String> newArray = new ArrayList();
newArray.add(array[0]);
for(int i = 1; i < array.length; i++){
if(array[i] != array[i-1]){
newArray.add(array[i]);
}
}
return newArray.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 countOfchars = 1;
for(int i = 0; i < array.length-1; i++){
if(array[i] != array[i+1]){
countOfchars++;
}
}
String[] newArr = new String[countOfchars];
int index = 0;
String addMe = "";
addMe += array[0];
for(int i = 1; i < array.length; i++){
if(array[i] == array[i-1]){
addMe += array[i-1];
} else if (array[i] != array[i-1]){
newArr[index] = addMe;
addMe = array[i];
index++;
}
}
newArr[index] = addMe;
return newArr;
}


Expand Down