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 Complete #70

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
94 changes: 85 additions & 9 deletions src/main/java/com/zipcodewilmington/StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.zipcodewilmington;

import com.sun.tools.javac.util.ArrayUtils;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

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

/**
Expand All @@ -42,6 +49,11 @@ 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(String val: array){
if (val.equals(value)){
return true;
}
}
return false;
}

Expand All @@ -50,23 +62,62 @@ 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;
ArrayList<String> arrayL = new ArrayList<String>();
Collections.addAll(arrayL,array);
Collections.reverse(arrayL);
String[] arr = new String[array.length];
return arrayL.toArray(arr);
}

/**
* @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;
return Arrays.equals(array,StringArrayUtils.reverse(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;
//an algo which goes thru the string[] and takes out stuff as it sees it
String[] abc = "abcdefghijklmnopqrstuvwxyz".split("");
ArrayList<String> atLarge = new ArrayList<String>();
Collections.addAll(atLarge, abc);
ArrayList<String> found = new ArrayList<String>();
int count = 0;
boolean wasFound = false;
for(int i = 0; i < array.length; i++){
String[] cur = array[i].split("");
for(int j = 0; j < array[i].length(); j++){
wasFound = false;
//& ignores stuff already found
if(! found.isEmpty()) {
for(int l = 0; l < found.size(); l++) {
if(found.get(l).equals(cur[j])){
l = found.size();
wasFound = true;
}
}
}
if(j < cur.length && !wasFound) {
for (int k = 0; k < atLarge.size(); k++) {
if(atLarge.isEmpty()){
return true;
}
if (atLarge.get(k).equalsIgnoreCase(cur[j])) {
found.add(atLarge.get(k));
atLarge.remove(k);
k = atLarge.size();
}

}
}
}
}
return atLarge.isEmpty();
}

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

/**
Expand All @@ -84,23 +142,41 @@ 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> strings = new ArrayList<String>();
Collections.addAll(strings, array);
strings.remove(valueToRemove);
return strings.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;
ArrayList<String> strings = new ArrayList<String>();
Collections.addAll(strings, array);
for(int i = 0; i < strings.size(); i++){
if(i != 0 && strings.get(i).equals(strings.get(i - 1))){
strings.remove(i--);
}
}
return strings.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;
ArrayList<String> strings = new ArrayList<String>();
Collections.addAll(strings, array);
for(int i = 0; i < strings.size(); i++){
while(i != strings.size() -1 && strings.get(i).charAt(0) == (strings.get(i+1).charAt(0))){
strings.set(i, strings.get(i) + strings.get(i+1));
strings.remove(i+1);
}
}
return strings.toArray(new String[0]);
}


Expand Down