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

lab done #52

Open
wants to merge 7 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>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
Expand Down
138 changes: 113 additions & 25 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,107 +1,195 @@
package com.zipcodewilmington;

import java.util.*;
import java.util.stream.IntStream;

import static java.lang.System.*;

/**
* Created by leon on 1/29/18.
*/
public class StringArrayUtils {
/**
* @param array array of String objects
* @return first element of specified array
*/ // TODO
*/ // WORKING
public static String getFirstElement(String[] array) {
return array[0];
}

/**
* @param array array of String objects
* @return second element in specified array
*/
*/ //WORKING
public static String getSecondElement(String[] array) {
return array[1];
}

/**
* @param array array of String objects
* @return last element in specified array
*/ // TODO
*/
//WORKING
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
*/
//WORKING
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
*/ // WORKING
public static boolean contains(String[] array, String value) {
return false;
boolean presentOrNot = false;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(value)) {
return true;
} else {
presentOrNot = false;
}
}
return presentOrNot;
}

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

/**
* @param array array of String objects
* @return true if the order of the array is the same backwards and forwards
*/ // TODO
*/ // WORKING
public static boolean isPalindromic(String[] array) {
return false;
Boolean revOrNot = false;
for (int i = 0; i < array.length / 2; i++) {
if (!array[i].equalsIgnoreCase((array[array.length - i - 1]))) {
revOrNot = false;
} else {
revOrNot = true;
}
}
return revOrNot;
}

/**
* @param array array of String objects
* @return true if each letter in the alphabet has been used in the array
*/ // TODO
*/ // WORKING
public static boolean isPangramic(String[] array) {
return false;
boolean isOrIsnt = false;
boolean[] mark = new boolean[26];
String concatStr = "";
for (int i = 0; i < array.length; i++) {
concatStr = concatStr.concat(array[i]);
}
concatStr = concatStr.toUpperCase();
int index = 0;
for (int i = 0; i < concatStr.length(); i++) {
if ('A' <= concatStr.charAt(i) && concatStr.charAt(i) <= 'Z') {
index = concatStr.charAt(i) - 'A';
} else {
continue;
}
mark[index] = true;
}
for (int i = 0; i <= 25; i++) {
if (mark[i] == false) {
isOrIsnt = false;
} else {
isOrIsnt = true;
}
}
return isOrIsnt;
}

/**
* @param array array of String objects
* @param value value to check array for
* @return number of occurrences the specified `value` has occurred
*/ // TODO
*/ // WORKING
public static int getNumberOfOccurrences(String[] array, String value) {
return 0;
int numOfTimes = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].equalsIgnoreCase(value)) {
numOfTimes++;
}
}
return numOfTimes;
}

/**
* @param array array of String objects
* @param valueToRemove value to remove from array
* @return array with identical contents excluding values of `value`
*/ // TODO
*/ // WORKING
public static String[] removeValue(String[] array, String valueToRemove) {
return null;
ArrayList<String> output = new ArrayList<>();
int counter = 0;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(valueToRemove)) {
continue;
} else {
output.add(array[i]);
counter++;
}
}
String[] outArr = output.toArray(new String[counter]);
return outArr;
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
*/ // WORKING
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
ArrayList<String> strList = new ArrayList<>();
strList.add(array[0]);
for (int i = 1; i < array.length; i++) {
if (!array[i - 1].equals(array[i])) {
strList.add(array[i]);
}
}
String[] outStrArr = strList.toArray(new String[0]);
return outStrArr;
}

/**
* @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
*/ // WORKING
public static String[] packConsecutiveDuplicates(String[] array) {
return null;
ArrayList<String> midList = new ArrayList<>();
String strHolder = "";
int counter = 0;
int compare = counter + 1;
while (counter < array.length) {
strHolder = strHolder.concat(array[counter]);
while (compare != array.length && array[counter].equals(array[compare])) {
strHolder = strHolder.concat(array[compare]);
compare++;
}
midList.add(strHolder);
strHolder = "";
counter = compare;
compare++;
}
String[] outArr = midList.toArray(new String[0]);
return outArr;
}


}