Skip to content
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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/FirstAssignment.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/aws.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 77 additions & 14 deletions StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@


import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.lang.String;
/**
* Created by Yang on 1/23/20.
*/
Expand All @@ -9,31 +11,30 @@ public class StringArrayUtils {
* @return first element of specified array
*/ // TODO
public static String getFirstElement(String[] array) {
return null;
return array[0];
}

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

/**
* @param array array of String objects
* @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,22 +43,34 @@ 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;
return Arrays.asList(array).contains(value);
}

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

/**
* @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) {
String[] reverseArray = new String[array.length];
int index=0;
for(int i=array.length-1;i>=0;--i){
reverseArray[index++]=array[i];
}
if(Arrays.equals(array,reverseArray))
return true;
return false;
}

Expand All @@ -66,7 +79,25 @@ public static boolean isPalindromic(String[] array) {
* @return true if each letter in the alphabet has been used in the array
*/ // TODO
public static boolean isPangramic(String[] array) {
return false;
boolean[] alphabetCheck= new boolean[26];
for(String s:array){
int index=0;
char[] chars = s.toCharArray();
for(char c:chars){
if ('A' <= c && c <= 'Z')
index = c - 'A';
else if ('a' <= c && c <= 'z')
index = c - 'a';
else
continue;
alphabetCheck[index]=true;
}

}
for (int i = 0; i <= 25; i++)
if (alphabetCheck[i] == false)
return false;
return true;
}

/**
Expand All @@ -75,7 +106,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;
Map<String, Integer> map = new HashMap<String, Integer>();
for(String s:array){
if(map.containsKey(s))
map.put(s,map.get(s)+1);
else
map.put(s,1);
}
return map.get(value);
}

/**
Expand All @@ -84,23 +122,48 @@ 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;
List<String> removedList = new ArrayList<String>(Arrays.asList(array));
removedList.remove(valueToRemove);
return removedList.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;
int index=1;
for(int i=1;i<array.length;i++){
if(array[i]!=array[index-1])
array[index++] = array[i];
}
String[] ans=new String[index];
for(int i=0;i<index;i++)
ans[i]=array[i];
return ans;
}

/**
* @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;

String tmp=array[0];
List<String> list=new ArrayList<String>();
for(int i=1;i<array.length;i++){

if(array[i]!=array[i-1]){
list.add(tmp);
tmp=array[i];
}
else
tmp+=array[i];

}
list.add(tmp);
String[] ans = list.toArray(new String[list.size()]);
return ans;
}


Expand Down