Skip to content

Commit 77a0823

Browse files
hyphenSplit solution:
if str len less than two: return str -if charAt zero equals charAt one -return charAt zero - hyphenSplit substring at one -return charAt zero plus hyphSplit substring at one
1 parent 3c2effc commit 77a0823

7 files changed

Lines changed: 136 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>assignment_18</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package assignment_18;
2+
3+
import ignore.TestingUtils;
4+
5+
public class App {
6+
7+
8+
/**
9+
*
10+
11+
Given a string, compute recursively a new string where identical chars
12+
that are adjacent in the original string are separated from each other by a "-".
13+
14+
<br>
15+
<br>
16+
17+
* <b>EXPECTATIONS:</b><br>
18+
repeatHyphen("hello") <b>---></b> hel-lo<br>
19+
repeatHyphen("xxyy") <b>---></b> x-xy-y <br>
20+
repeatHyphen("aaaa") <b>---></b> a-a-a-a <br>
21+
*/
22+
23+
public static String hyphenSplit(String str) {
24+
25+
// complete the body of this method
26+
if(str.length() < 2) {
27+
return str;
28+
}
29+
30+
if(str.charAt(0) == str.charAt(1)) {
31+
return str.charAt(0) + "-" + hyphenSplit(str.substring(1));
32+
}
33+
34+
return str.charAt(0) + hyphenSplit(str.substring(1));
35+
}
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
//----------------------STARTING POINT OF PROGRAM. IGNORE BELOW --------------------//
50+
public static void main(String args[]){
51+
TestingUtils.runTests();
52+
53+
}
54+
}
55+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ignore;
2+
3+
import assignment_18.App;
4+
5+
public class TestingUtils {
6+
public static void runTests(){
7+
8+
9+
String[] params1 = { "hello", "xxyy", "aaaa", "aaab", "aa", "a", "",
10+
"noadjacent", "abba", "abbba"};
11+
12+
String[] expected = { "hel-lo", "x-xy-y", "a-a-a-a", "a-a-ab", "a-a", "a", "",
13+
"noadjacent", "ab-ba", "ab-b-ba"};
14+
15+
for(int i=0; i < params1.length; i++){
16+
String result = App.hyphenSplit(params1[i]);
17+
if(result.equals(expected[i])) {
18+
System.out.println(printPassResult(params1[i], expected[i], result));
19+
} else{
20+
System.out.println(printFailResult(params1[i], expected[i], result));
21+
}
22+
}
23+
}
24+
25+
private static String printPassResult(Object params1, Object expected, Object result){
26+
return "PASS: hyphenSplit("+ params1.toString()+ ") -> " + result.toString();
27+
}
28+
29+
30+
private static String printFailResult(Object params1, Object expected, Object result){
31+
String ret = "**********************" + "\n";
32+
ret += "FAIL: hyphenSplit("+ params1.toString()+ ") -> " + result.toString()
33+
+ " Expected: "+ expected.toString();
34+
ret += "\n" + "**********************";
35+
return ret;
36+
}
37+
}

0 commit comments

Comments
 (0)