Java Warmup-1 of codingbat.com solutions to problems 15 to 31. If you are new here please visit the previous post codingbat-java-warmup-1-solutions-1-to-15.
16. Problem Name : startHi
Solution:
Hint:
If string length is less than 2 return false as it cannot be “hi” .If first two
character are equal to hi return true, else false.
public boolean startHi(String str) {
//
First test if the string is not at least length 2
//
(so the substring() below does not go past the end).
if (str.length() < 2) return false;
//
Pull out the string of the first two chars
String firstTwo = str.substring(0, 2);
//
Test if it is equal to "hi"
return(firstTwo.equals("hi"));
}
|
17. Problem Name : icyHot
Solution:
Hint: return true if
one is less than 0 and the other is greater than 100.
public boolean icyHot(int temp1, int temp2) {
return ((temp1 < 0
&& temp2 > 100) || (temp1 > 100 && temp2 < 0));
}
|
18. Problem Name : in1020
Solution:
Hint:
check whether a is in between 10 and 20 OR b is in between 10 and 20
public boolean in1020(int a, int b) {
return ((a >= 10 && a <= 20) || (b >= 10
&& b <= 20));
}
|
19. Problem Name : hasTeen
Solution:
Hint:
check whether a OR b OR c is within range 13 to 19 inclusive
public boolean hasTeen(int a, int b, int c) {
//
Here it is written as one big expression,
//
vs. a series of if-statements.
return (a>=13 && a<=19) ||
(b>=13 && b<=19) ||
(c>=13 && c<=19);
}
|
20. Problem Name : loneTeen
Solution:
Hint:
one of a OR b should be within the range 13 to 19 inclusive.
public boolean loneTeen(int a, int b) {
//
Store teen-ness in boolean local vars first. Boolean local
//
vars like this are a little rare, but here they work great.
boolean aTeen = (a >= 13 && a <= 19);
boolean bTeen = (b >= 13 && b <= 19);
return (aTeen && !bTeen) || (!aTeen &&
bTeen);
//
Translation: one or the other, but not both.
//
Alternately could use the Java xor operator, but it's obscure.
}
}
|
21. Problem Name : delDel
Solution:
Hint:
check if “del” is present in string if present use substrings to delete it else
return as such.
public String
delDel(String str) {
if (str.length()>=4 && str.substring(1, 4).equals("del")) {
// First char + rest of string starting
at 4
return str.substring(0, 1) +
str.substring(4);
}
//
Otherwise return the original string.
return str;
}
|
22. Problem Name : mixStart
Solution:
Hint:
ignore first character string, take the next two character string if equals “ix”
return true else false.
public boolean mixStart(String
str) {
if(str.length()<3) return false;
String mix = str.substring(1,3);
return (mix.equals("ix"));
}
|
23. Problem Name : startOz
Solution:
Hint: return a
string made of the first 2 chars (if present), however include first char only
if it is 'o' and include the second only if it is 'z', so
"ozymandias" yields "oz"
public String startOz(String str) {
String result = "";
if (str.length() >= 1 && str.charAt(0)=='o') {
result = result + str.charAt(0);
}
if (str.length() >= 2 && str.charAt(1)=='z') {
result = result + str.charAt(1);
}
return result;
}
|
24. Problem Name : intMax
Solution:
Hint:
return the largest of the three integer values.
public int intMax(int a, int b, int c) {
if(a>b && a>c)
return a;
else if(b>a && b>c)
return b;
else
return c;
}
|
25. Problem Name : close10
Solution:
Hint:
value is nearest to the value 10, or return 0 in the event of a tie.
public int close10(int a, int b) {
int aDiff = Math.abs(a
- 10);
int bDiff = Math.abs(b - 10);
if (aDiff < bDiff) {
return a;
}
if (bDiff < aDiff) {
return b;
}
return 0; // i.e. aDiff ==
bDiff
}
|
26. Problem Name : in3050
Solution:
Hint: return
true if they are both in the range 30..40 inclusive, or they are both in the
range 40..50 inclusive
public boolean in3050(int a, int b) {
int inthirty=0;
int infourty=0;
if((a>=30 && a<=40)&&(b>=30 &&
b<=40)||((a>=40 && a<=50)&&(b>=40 &&
b<=50)))
{
return true;
}
return false;
}
|
27. Problem Name : max1020
Solution:
Hint:
make a as larger by swapping .Check if is within range 10...20 return true. If
a is not within range it checks b is within 10...20 return true. If it is also
not true it reaches return 0.
public int max1020(int a, int b) {
//
First make it so the bigger value is in a
if (b > a) {
int temp = a;
a = b;
b = temp;
}
//
Knowing a is bigger, just check a first
if (a >= 10 && a <= 20) return a;
if (b >= 10 && b <= 20) return b;
return 0;
}
|
28. Problem Name : stringE
Solution:
Hint: Return true if
the given string contains between 1 and 3 'e' chars.
public boolean stringE(String
str) {
int count = 0;
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == 'e') count++;
// alternately: str.substring(i,
i+1).equals("e")
}
return (count >= 1 && count <= 3);
}
|
29. Problem Name : lastDigit
Solution:
Hint:
number mod 10 return the digit in ones position. So 17 % 10 is 7.
public boolean lastDigit(int a, int b) {
return (a%10==b%10);
}
|
30. Problem Name : endUp
Solution:
Hint:
use substring to cut the last three character strings and remaining strings. Return
front + back(uppercased)[use String method string.toUpperCase()].
public String
endUp(String str) {
if (str.length() <= 3) return str.toUpperCase();
int cut = str.length() - 3;
String front = str.substring(0, cut);
String back =
str.substring(cut); // this takes
from cut to the end
return front + back.toUpperCase();
}
|
31. Problem Name : everyNth
Solution:
Hint:
use .charAt(index) function to access character. Use for loop increment to do
the trick(i+n).
public String
everyNth(String str, int n) {
int len=str.length();
String result="";
for(int i=0;(i)< len;i=i+n)
{
result=result+str.charAt(i);
}
return result;
}
|
You
have just completed the Java > Warm up 1 session. Great! 5 more sections to go.
No comments:
Post a Comment