Today was a regular class. It was the last class before I left to Paris. I worked as usual, but towards the second half of class ( as usual), I was getting frustrated because I wasn't able to solve many problems. Nevertheless, I kept working; but something different this time. I did some String 2 problems. Very simple ones, but still problems that needed to be solved. So I worked on those problems for a bit.
But on the topic of Array 2, I do get very frustrated sometimes and lose my patience. The problems are getting harder and harder, but Mr. Daly reminded me that this is about the AP level, so I should be OK if I keep practicing. Also, I will be taking the AP Computer Science course next year.
String 2 problem for today:
----------------------------------------------------------------------------------------------------------------------------------
Return true if the string "cat" and "dog" appear the same number of times in the given string.
catDog("catdog") → true
catDog("catcat") → false
catDog("1cat1cadodog") → true
public boolean catDog(String str) {
int sumcat =0;
int sumdog =0;
// create sums for each word
for (int i=0;i<str.length()-2;i++)
{
if (str.substring(i,i+3).equals("cat"))
{
sumcat=sumcat+1;
// if there is the word "cat" in the string, then add 1 to the cat sum.
}
if (str.substring(i,i+3).equals("dog"))
{
sumdog=sumdog+1;
// if there is the word "dog" in the string, then add 1 to the dog sum.
}
}
if (sumcat == sumdog)
{
return true;
// if there is the word "dog" as many times as the word "cat" in the string, then return true.
}
return false;
if there is not the same amount of "dog" and "cat", then return false.
}
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
Monday, March 16th
I wasn't in school today, so I wasn't in class. I was still in Paris. However, that didn't mean I didn't work. I did an Array 2 problem. I didn't have much time, so I did what I could.
Here it is:
----------------------------------------------------------------------------------------------------------------------------------
Return true if the array contains, somewhere, three increasing adjacent numbers like .... 4, 5, 6, ... or 23, 24, 25. tripleUp({1, 4, 5, 6, 2}) → true tripleUp({1, 2, 3}) → true tripleUp({1, 2, 4}) → false |
public boolean tripleUp(int[] nums) {
for (int i=0;i<nums.length-2;i++)
{
if (nums[i+1]==nums[i]+1 && nums[i+2]==nums[i+1]+1)
{
return true;
// if there are 3 increasing numbers that come one after the other, return true.
}
}
return false;
// if there are not 3 increasing numbers, or they are not all one after the other, then return false.
}
Coming back to class first thing tomorrow in the morning. Excited !
No comments:
Post a Comment