CodingBat problems
Strangely enough, doing CodingBat problems was both challenging and easy for me. The first problems were easy, yet I spent a while on them. When I was doing the first substring problem I kept misspelling length for lenght, until I realized I had a typo.
It’s been a few years since I last practiced Java, and I normally use C++. Because of that, I had to search on Google for a lot of concepts like things like strings, lists, and maps. Now I know that I need more practice, not just in Java, but also in thinking through problems.
Some questions were confusing, and I had to read them a lot of times. I tried to to think of a logical way to tackle it first, but I failed often, so I would write code and modify it until it worked.
One example is the xyzThere problem. The goal was to check if it "xyz" appears in the string, but not if it’s right after a period ".". It took me many tries, but this code finally worked:
public boolean xyzThere(String str) {
for (int i = 0; i < str.length() - 2; i++) {
if (str.substring(i, i + 3).equals("xyz")) {
if (i == 0 || str.charAt(i - 1) != '.') {
return true;
}
}
}
return false;
}
Comments
Post a Comment