Skip to content

Commit a4bb07b

Browse files
Singh, ShubhamSingh, Shubham
Singh, Shubham
authored and
Singh, Shubham
committed
Add Problem1 Solution and Practice Strings
1 parent ba691f5 commit a4bb07b

File tree

6 files changed

+138
-72
lines changed

6 files changed

+138
-72
lines changed

.idea/workspace.xml

+103-68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

udemyproblems/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# UdemyProblems from the Udemy course: Java Puzzles to Eliminate Code Fear
1+
# Udemy Problems from the Udemy course: Java Puzzles to Eliminate Code Fear
22

33
This project covers problems given in the course: Java Puzzles to Eliminate Code Fear
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class PracticeStrings {
2+
3+
public static void main(String[] args) {
4+
String s = "Happy";
5+
System.out.println(s.charAt(1));
6+
7+
if(s.equalsIgnoreCase("happy")) {
8+
System.out.println("Happy match happy");
9+
}
10+
11+
s="Today Happy looks Happy.";
12+
System.out.println(s.indexOf("Happy"));
13+
System.out.println(s.indexOf("Happy", 0));
14+
System.out.println(s.indexOf("Happy", 7));
15+
16+
System.out.println(s.lastIndexOf("Happy"));
17+
}
18+
}

udemyproblems/src/main/java/Problem1.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
public class Problem1 {
22

33
/**
4-
Given a string of odd length, return the middle 3 characters from the string,
4+
Problem Category: Easy
5+
Given a string of odd length, return the middle 3 characters from the string,
56
so the string <b>"Monitor"</b> yields <b>"nit"</b>.
67
If the string length is less than 3, return the string as is. <br> <br>
78
@@ -13,6 +14,13 @@ public class Problem1 {
1314

1415
public static String middleThree(String str) {
1516
String result = str;
17+
18+
if(str.length()>=3) {
19+
int mid = str.length()/2;
20+
result = str.substring(mid-1, mid+2);
21+
return result;
22+
}
23+
1624
return result;
1725
}
1826

@@ -21,6 +29,11 @@ public static void main(String args[]){
2129
Problem1Test.runTests();
2230
}
2331

32+
/**
33+
* In below inner class are the test fixtures setup to test various
34+
* test cases, the objective of above problem's solution is to make
35+
* these test cases PASS.
36+
* */
2437
public static class Problem1Test {
2538
public static void runTests(){
2639

@@ -30,14 +43,14 @@ public static void runTests(){
3043
for(int i=0; i < params1.length; i++){
3144
String result = middleThree(params1[i]);
3245
if(result.equals(expected[i])) {
33-
System.out.println(printPassResult(params1[i], expected[i], result));
46+
System.out.println(printPassResult(params1[i], result));
3447
} else{
3548
System.out.println(printFailResult(params1[i], expected[i], result));
3649
}
3750
}
3851
}
3952

40-
private static String printPassResult(Object params1, Object expected, Object result){
53+
private static String printPassResult(Object params1, Object result){
4154
return "PASS: middleThree("+ params1.toString()+ ") -> " + result.toString();
4255
}
4356

Binary file not shown.
158 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)