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

Completed lab #72

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
112 changes: 100 additions & 12 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,56 @@
package com.zipcodewilmington;

import java.util.Arrays;

/**
* Created by leon on 1/29/18.
*/
public class StringArrayUtils {
/**
* @param array array of String objects
* @return first element of specified array
*/ // TODO
public static String getFirstElement(String[] array) {
return array[0];
String result = null;
if (array != null && array.length >= 1) {
result = array[0];
}
return result;
}

/**
* @param array array of String objects
* @return second element in specified array
*/
public static String getSecondElement(String[] array) {
return array[1];
String result = null;
if (array != null && array.length >= 2) {
result = array[1];
}
return result;
}

/**
* @param array array of String objects
* @return last element in specified array
*/ // TODO
public static String getLastElement(String[] array) {
return null;
String result = null;
if (array != null && array.length > 0) {
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 = null;
if (array != null && array.length >= 2) {
result = array[array.length - 2];
}
return result;
}

/**
Expand All @@ -42,6 +59,13 @@ 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++){
String s = array[i];
if(array[i].contains(value)){
return true;
}
}

return false;
}

Expand All @@ -50,32 +74,70 @@ 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;
StringBuilder reverse = new StringBuilder();
int len = array.length;
for(int i = len ; i>0 ; i--){
reverse.append(array[i-1]).append(" ");
}
String[] reversed = reverse.toString().split(" ");
return reversed;
}

/**
* @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;
int len = array.length;
for(int i =0; i< (len/2);i++){
if(array[i] != array[len-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;

boolean[] used = new boolean[26];
for(int i = 0; i < array.length; i++){
String word = array[i].toUpperCase();
for (int j = 0; j < word.length(); j++) {

if (word.charAt(j) >= 'A' && word.charAt(j) <= 'Z') {
int foundLetterIndex = word.charAt(j) - 'A';
used[foundLetterIndex] = true;
}
}
}

for (int k = 0; k < used.length; k++) {
if (!used[k]) {
return false;
}
}
return true;
}



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

/**
Expand All @@ -84,23 +146,49 @@ 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[] result = new String[array.length-1];
int resultIndex = 0;
for(int i = 0; i < array.length; i++){
if(!array[i].equals(valueToRemove)){
result[resultIndex++] = array[i];
}
}
return result;
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
StringBuilder s = new StringBuilder();
for (int i = 0; i < array.length - 1; i++) {
if (!array[i].equals(array[i + 1])) {
s.append(array[i]).append(" ");
}
}
s.append(array[array.length-1]);
return s.toString().split(" ");
}

/**
* @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;
StringBuilder s= new StringBuilder();
for(int i = 0; i< array.length-1; i++){

if(array[i].equals(array[i+1])){
s.append(array[i]);
}else
{
s.append(array[i]);
s.append(" ");
}
}
s.append(array[array.length-1]);
return s.toString().split(" ");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public void testRemoveConsecutiveDuplicates1() {





@Test
public void testRemoveConsecutiveDuplicates2() {
String[] array = {"aba", "aba", "baa", "bab", "bba", "zzz", "bba", "bba", "bba", "bbb", "bbb"};
Expand Down