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
1 change: 1 addition & 0 deletions GetSecondToLastElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public void testGetSecondToLastElement1() {
String[] array = {"the", "man", "of", "steel", "fights", "a", "never-ending", "battle", "for","truth"};
String expected = "for";
String actual = StringArrayUtils.getSecondToLastElement(array);
System.out.println(actual);
Assert.assertEquals(expected, actual);
}

Expand Down
2 changes: 1 addition & 1 deletion ReverseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created by super-yang on 03/05/2020.
*/

public class ReverseTest {
public class ReverseTest extends StringArrayUtils {

@Test
public void testReverse1() {
Expand Down
92 changes: 80 additions & 12 deletions StringArrayUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


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

/**
* @param array array of String objects
* @return second element in specified array
*/
public static String getSecondElement(String[] array) {
return null;
return array==null||array.length<1?null: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==null?null: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==null||array.length==1?null:array[array.length-2];
}

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

Expand All @@ -50,23 +53,50 @@ 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;
if(array==null) return null;
int i=0,j=array.length-1;
while(i<j){
String temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
return array;
}

/**
* @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;
if(array==null) return true;
int i=0,j=array.length-1;
while(i<j){
if(!array[i].equals(array[j])) return false;
i++;
j--;
}
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;
Set<Character> set = new HashSet();
for(int i=0;i<26;i++){
set.add((char)('a'+i));
}
for(String str:array){
str = str.toLowerCase();
for(int i=0;i<str.length();i++)
if(set.contains(str.charAt(i))){
set.remove(str.charAt(i));
}
}
return set.isEmpty()?true:false;
}

/**
Expand All @@ -75,7 +105,12 @@ 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 count = 0;
if(array==null) return count;
for(String str:array){
if(str.equals(value)) count++;
}
return count;
}

/**
Expand All @@ -84,23 +119,56 @@ 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;
if (array==null) return null;
int deleteIndex = -1;
for(int i=0;i<array.length;i++){
if(array[i].equals(valueToRemove)){
deleteIndex = i;
break;
}
}
while(deleteIndex!=-1&&deleteIndex<array.length-1){
array[deleteIndex] = array[deleteIndex+1];
deleteIndex++;
}
return Arrays.copyOfRange(array,0,array.length-1);
}

/**
* @param array array of chars
* @return array of Strings with consecutive duplicates removes
*/ // TODO
public static String[] removeConsecutiveDuplicates(String[] array) {
return null;
if(array==null) return null;
int i=0;
for(int j=0;j<array.length;j++){
if(!array[j].equals(array[i])){
array[++i] = array[j];
}
}
return Arrays.copyOfRange(array,0,i+1);
}

/**
* @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;
if(array==null) return null;
int i =0;
String before=array[0];
StringBuilder sb = new StringBuilder();
for(int j=0;j<array.length;j++){

if(!array[j].equals(before)){
before = array[j];
array[i++] = sb.toString();
sb.setLength(0);
}
sb.append(array[j]);
}
array[i++] = sb.toString();
return Arrays.copyOfRange(array,0,i);
}


Expand Down
Loading