Skip to content

String Array utilities lab #61

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 4 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
110 changes: 98 additions & 12 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.zipcodewilmington;

import com.sun.org.apache.xpath.internal.operations.Bool;

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

/**
* Created by leon on 1/29/18.
*/
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,15 +31,21 @@ public static String getSecondElement(String[] array) {
* @return last element in specified array
*/ // TODO
public static String getLastElement(String[] array) {
return null;
String result = "";

result = array[array.length - 1];

return result;
}

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

/**
Expand All @@ -42,31 +54,59 @@ 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;
Boolean result = false;
for (String word : array) {
if (word == value) {
result = true;
}
}
return result;
}

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

/**
* @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;
Boolean result = true;
for (int i = 0; i < array.length; i++) {
if (!array[i].equals(array[array.length - i - 1])) {
result = false;
}
}
return result;
}

/**
* @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 check=String.join("",array).toLowerCase();
String[] letters={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
for(int i=0;i<letters.length;i++){
if(check.contains(letters[i])){
}
else
{
return false;
}
}
return true;
}

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

/**
Expand All @@ -84,24 +130,64 @@ 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;
ArrayList<String> tempArray = new ArrayList<String>();
for (int i = 0; i < array.length; i++) {
if (!array[i].equals(valueToRemove)) {
tempArray.add(array[i]);

}
}
String[] result = new String[tempArray.size()];
result = tempArray.toArray(result);
return result;
}

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

ArrayList<String> tempArrayList = new ArrayList<String>();
for (int i = 0; i < array.length; i++) {
tempArrayList.add(array[i]);
if (i > 0) {
if (array[i].equals(array[i - 1])) {
tempArrayList.remove(tempArrayList.size() - 1);
}
}
}
String[] result = new String[tempArrayList.size()];
result = tempArrayList.toArray(result);
return result;
}


/**
* @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;
}
ArrayList<String> tempArrayList = new ArrayList<String>();
//{"a", "a", "a", "b", "c", "c", "a", "a", "d"};
int j = 0;
tempArrayList.add(array[0]);

for(int i = 1; i < array.length; i++){
if(tempArrayList.get(j).contains(array[i]))
{
tempArrayList.set(j, (tempArrayList.get(j) + array[i]));
}
else
{
j++;
tempArrayList.add(array[i]);
}
}

}
String[] result = new String[tempArrayList.size()];
result = tempArrayList.toArray(result);

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public void testRemoveConsecutiveDuplicates3() {
Assert.assertEquals(actual, expected);
}

}
}