-
Notifications
You must be signed in to change notification settings - Fork 6
Update my implementation #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
eb8fdb7
9d7a00a
e89702b
85ed405
262cdf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| language: java | ||
| jdk: openjdk17 | ||
| jdk: openjdk15 | ||
| jobs: | ||
| include: | ||
| - script: mvn clean verify package | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,101 @@ | ||
| import java.util.*; | ||
| public class StringManipulation implements StringManipulationInterface { | ||
| private String pString; | ||
|
|
||
| @Override | ||
| public String getString() { | ||
| return null; | ||
| return pString; | ||
| } | ||
|
|
||
| @Override | ||
| public void setString(String string) { | ||
| this.pString = string; | ||
| } | ||
|
|
||
| @Override | ||
| public int count() { | ||
| return 0; | ||
| if(pString == null || pString.isEmpty()){ | ||
| return 0; | ||
| } | ||
|
|
||
| String[] words = pString.split("\\s+"); //split string to words using space | ||
| return words.length; | ||
| } | ||
|
|
||
| @Override | ||
| public String removeNthCharacter(int n, boolean maintainSpacing) { | ||
| return null; | ||
| String theString = getString(); | ||
|
|
||
| if(n > theString.length()){ | ||
| throw new IndexOutOfBoundsException("N cannot be larger than the length of Sting"); | ||
| } | ||
|
|
||
| if(n <= 0){ | ||
| throw new IllegalArgumentException("Input n cannot be 0 or negative"); | ||
| } | ||
|
|
||
| StringBuilder sb = new StringBuilder(theString); | ||
|
|
||
| if(maintainSpacing){ //replace the nth letter with a space | ||
| for(int i = n-1; i < theString.length(); i += n){ | ||
| sb.setCharAt(i, ' '); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| else{ | ||
| if(n == 1){ //return empty string | ||
| return ""; | ||
| } | ||
| else{ //delete nth letter without spacing | ||
| for(int i = n-1; i < theString.length(); i += n){ | ||
| while (i < sb.length()){ | ||
| sb.deleteCharAt(i); | ||
| i += n - 1; | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getSubStrings(int startWord, int endWord) { | ||
| return null; | ||
| public String[] getSubStrings(int startWord, int endWord){ | ||
| if(startWord <= 0 || endWord <= 0 || startWord > endWord){ | ||
| throw new IllegalArgumentException("Invalid startWord or endWord"); | ||
| } | ||
|
|
||
| String[] words = getString().split("\\s+"); //split string to words using space | ||
|
|
||
| if(words.length < endWord){ | ||
| throw new IndexOutOfBoundsException("String has less than 'endWord' words"); | ||
| } | ||
| //returns a copy of array that only include startWord to endWord | ||
| return Arrays.copyOfRange(words, startWord - 1, endWord); | ||
| } | ||
|
|
||
| @Override | ||
| public String restoreString(int[] indices) { | ||
| return null; | ||
| } | ||
| public String restoreString(int[] indices){ | ||
| if(indices == null || this.pString == null) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May also want to check for string and indices[] with length zero as a precaution |
||
| throw new IllegalArgumentException("There are no input"); | ||
| } | ||
|
|
||
| if(indices.length == 0 || this.pString.length() == 0){ | ||
| throw new IllegalArgumentException("The indices or String is 0"); | ||
| } | ||
|
|
||
| if(indices.length != this.pString.length()) { | ||
| throw new IllegalArgumentException("String length and indices length are not equal"); | ||
| } | ||
|
|
||
| char[] shuffledString = new char[indices.length]; //creating new char array of length indices | ||
|
|
||
| for(int i = 0; i < indices.length; i++) { //for each index in indices | ||
| if(indices[i] < 0 || indices[i] >= this.pString.length()) { | ||
| throw new IndexOutOfBoundsException("The indices " + indices[i] + " is out of bounds"); | ||
| } | ||
| shuffledString[i] = this.pString.charAt(indices[i]); //shuffles string in the order of indices | ||
| } | ||
|
|
||
| return new String(shuffledString); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,17 +28,23 @@ public void testCount1() { | |
|
|
||
| @Test | ||
| public void testCount2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Testing for string"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(3, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("Testing testing"); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(2, length); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCount4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString(""); | ||
| int length = manipulatedstring.count(); | ||
| assertEquals(0, length); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -49,101 +55,148 @@ public void testRemoveNthCharacter1() { | |
|
|
||
| @Test | ||
| public void testRemoveNthCharacter2() { | ||
| manipulatedstring.setString("I'd b3tt3r put s0me d161ts in this 5tr1n6, right?"); | ||
| assertEquals("I' b tt r ut s0 e 16 ts in th s tr n6 r gh ?", manipulatedstring.removeNthCharacter(3, true)); | ||
| manipulatedstring.setString("Hi this is the message"); | ||
| assertEquals("H h s i h e s g ", manipulatedstring.removeNthCharacter(2, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This message should have less character deleted"); | ||
| assertEquals("This me sage sh uld hav less c aracter deleted", manipulatedstring.removeNthCharacter(8, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This message should have only the last character deleted"); | ||
| assertEquals("This message should have only the last character delete", manipulatedstring.removeNthCharacter(56, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This message should be unreadable"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend adding a test that checks to see what happens when N is a value larger than the length of the string
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes I did that now |
||
| assertThrows( | ||
| IndexOutOfBoundsException.class, () -> { | ||
| assertEquals("", manipulatedstring.removeNthCharacter(100, false)); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This message is meant to be deleted"); | ||
| assertEquals("", manipulatedstring.removeNthCharacter(1, false)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveNthCharacter7() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("The space"); | ||
| assertEquals(" ", manipulatedstring.removeNthCharacter(1, true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings1() { | ||
| manipulatedstring.setString("This is my string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(3, 4); | ||
|
|
||
| assertEquals(sStings[0], "my"); | ||
| assertEquals(sStings[1], "string"); | ||
| assertEquals("my", sStings[0]); | ||
| assertEquals("string", sStings[1]); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings2() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This change is requested by Caleb"); | ||
|
|
||
|
|
||
| assertThrows( | ||
| IllegalArgumentException.class, () -> { | ||
| String [] sStings = manipulatedstring.getSubStrings(6, 1); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings3() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This is a odd string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 3); | ||
|
|
||
| assertEquals("This", sStings[0]); | ||
| assertEquals("a", sStings[2]); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings4() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This string is a very long string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(2, 4); | ||
|
|
||
| assertEquals("string", sStings[0]); | ||
| assertEquals("a", sStings[2]); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings5() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This sting will return the first and last string"); | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 9); | ||
|
|
||
| assertEquals("This", sStings[0]); | ||
| assertEquals("string", sStings[8]); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGeSubStrings6() { | ||
| fail("Not yet implemented"); | ||
| manipulatedstring.setString("This string is the last test string"); | ||
|
carector marked this conversation as resolved.
|
||
| assertThrows( | ||
| IndexOutOfBoundsException.class, () -> { | ||
| String [] sStings = manipulatedstring.getSubStrings(1, 100); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString1() | ||
| { | ||
| public void testRestoreString1() { | ||
| manipulatedstring.setString("art"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| array = new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals(restoreString, "rat"); | ||
| assertEquals("rat", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString2() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString2() { | ||
| manipulatedstring.setString("UnitTest"); | ||
| int [] array; | ||
| array = new int[]{4,5,6,7,0,1,2,3}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("TestUnit", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString3() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString3() { | ||
| manipulatedstring.setString("eat"); | ||
| int [] array; | ||
| array = new int[]{2,0,1}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("tea", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString4() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString4() { | ||
| manipulatedstring.setString("cat"); | ||
| int [] array; | ||
| array=new int[]{1,0,2}; | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| assertEquals("act", restoreString); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRestoreString5() | ||
| { | ||
| fail("Not yet implemented"); | ||
|
|
||
| public void testRestoreString5() { | ||
| manipulatedstring.setString(""); | ||
| int [] array; | ||
| array=new int[]{2,1,0}; | ||
| assertThrows( | ||
| IllegalArgumentException.class, () -> { | ||
| String restoreString = manipulatedstring.restoreString(array); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea using a StringBuilder! I hadn't thought of that in my implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I forgot to add a exception when n is larger than the length of string lol