Thursday, April 30, 2015

Thursday, April 30th

Today was a regular class. I went up to the Genius Bar with Tommy, but unfortunately there were no customers.

I worked as usual on string 2. Today was a particularly good working day for me. I did one problem that I got on (almost) the first try, and I was the happiest person on earth. I was also happy because it proved that I was understanding the logic. I got to half of String 2 today. Only half of the problems left !

Today I used once again the Java string oracle documentation. It is SUPER useful. I learned a method, that will most likely help me out greatly in the future. It is the str.contain() method.

In one of the problems, I tried to use another method called math.Max(), but CodingBat didn't let me use it. It didn't let me, because it was a static int method, and for some reason CodingBat doesn't like that.

Apart from that, I don't really have anything special to say. This is today's problem:
----------------------------------------------------------------------------------------------------------------------------------

Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().

prefixAgain("abXYabc", 1) → true
prefixAgain("abXYabc", 2) → true
prefixAgain("abXYabc", 3) → false


public boolean prefixAgain(String str, int n) {

 
  for (int i =0;i<str.length()-n;i++)
  {
 
  if (str.substring(n).contains(str.substring(0,n)))
  {
  return true;
// if the string contains the prefix after the first time that it appears, return true;
 
  }
 
  }
 
  return false;
// if the prefix does not reappear, then return false;
}

Looking forward to continuing with strings.

Tuesday, April 28, 2015

Tuesday, April 28th

Today was a very split up class. It was separated into 4 different parts, in which I did different things.

In the first part of class, Tommy told me that he was starting Array 1, but he had no idea what to do. So, since I had completed it already and it was easy to me now, I explained to him all that I knew about Arrays, to get him going. And it seemed like I was a good teacher, since he flew through all the problems after that.

In the second part of class, I was working on the problem where I had to use indexOf. It was called getSandwich (I will show it) The thing is, that I was very confused, and when I called Mr. Daly over, we were both just as confused. We tried to solve it, but just couldn't figure it out. So we went to Mr. Daly's computer, and looked at his solution. It turned out that he had tried to use a method which was rather interesting : he used the IndexOf, but backwards ! This will all be explained in the problem. However, after looking at his problem and understanding how he did it, Mr. Daly introduced me to the Java library, which consists of all the possible methods in Java. It turns out that Mr. Daly from a few years ago could have saved himself a lot of time, because there is actually a method that does indexOf from backwards in just one line of code, and he could have used that instead of the 5 or so that he wrote.
----------------------------------------------------------------------------------------------------------------------------------
A sandwich is two pieces of bread with something in between. Return the string that is between the first and last appearance of "bread" in the given string, or return the empty string "" if there are not two pieces of bread. 

getSandwich("breadjambread") → "jam"
getSandwich("xxbreadjambreadyy") → "jam"
getSandwich("xxbreadyy") → ""



public String getSandwich(String str) {

  if (str.length()<11)
  {
   return "";
// if the string length is less than 11, then there can't be two "bread" and something in between, so return
// an empty string.
  }
 
  int first = str.indexOf("bread");
  int last = str.lastIndexOf("bread");
// create two integers, each marking the first appearance of "bread" and the last in the string str.
 

  return str.substring(first+5,last);
// return the part of the string which is after the first "bread", and before the last one.

}

----------------------------------------------------------------------------------------------------------------------------------


In the third part of class, Matan really wanted to show me his work that he was doing on GarageBand, so I went outside with him for a few minutes so he could show me his music. It was an interesting mix of sounds that he put together, and surprisingly, it actually sounded good.


And finally, during the last part of class, I got back to working on my own problems. I got started on another problem, and then encountered another problem (deja-vu). But that's another story, that I'll deal with next class.












Friday, April 24, 2015

Friday, April 24th

Today was a regular class. We were supposed to have class Thursday, but because of Independence Day we didn't have school Thursday, so our class was pushed to Friday. Nothing special about class besides that though.

I kept working on String 2, and I realised that I will most likely finish String 2 before the quarter ends. This is because there aren't as many string problems as array problems. Of course, I could be wrong in saying that, because they might turn out to be very hard. But if I keep working at the pace that I am now, I should finish all the problems before the year ends.

I am still getting used to strings again. I was working on a problem for the whole class, and it was only at the end of class when I told Mr. Daly that I was very confused about all of these problems, that he explained to me a very important concept in Strings. Less of a concept, more of a method in coding. The indexOf method. indexOf tells you when a word starts in the string. For example, if my string is "hellomynameis" and I code str.indexOf("my");     the computer will tell me at what part of the string the word my comes in. So in that case, I would get the number 4.

This method will come in handy a lot in my String 2 problems, because they often ask me to do things that require taking a part of a string and doing something with it. Up until now, I was confused how to. But from now on, I'm sure that this method will help me.

However, I won't be showing an indexOf problem today. I will be showing one for next class's blog.
Here is today's problem:
----------------------------------------------------------------------------------------------------------------------------------
Return true if the given string contains a "bob" string, but where the middle 'o' char can be any char.

bobThere("abcbob") → true
bobThere("b9b") → true
bobThere("bac") → false















Tuesday, April 21, 2015

Tuesday, April 21st

Regular class today. I went up to the Genius Bar with Tommy. No customers, though. 

I started working on String 2, and it was a bit weird for me. It was the first time that I had done Strings in almost over a quarter ! And so when I worked with them, I wasn't used to it. I had forgotten how to format strings. For example, when you want to say the length of the array you would say : nums.length.
But for a string, you would have to say : str.length(). You always have to add parameters to strings, so when I would want to take i, I would have to write str.substring(i), instead of nums[i]. In my opinion, writing arrays is much simpler than strings. I think that it will take me a bit of time to get used to the format of strings.

But either way, I still managed to complete a few problems. I had to go see Mr. Daly. And the problem that we had was, for the first time, a problem with the actual code, and not just the formatting of the code. It took us about 20 minutes to finally solve it.

But that's not the problem that I'll be showing today. 

Here is today's problem:
----------------------------------------------------------------------------------------------------------------------------------
Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2





Looking forward to continuing with Strings tomorrow.




Sunday, April 19, 2015

Friday, April 17th

Today was a very very short day. We ended school at 11:00, so our class was not even an hour long. We had 40 minutes of class, but I managed to get a lot done.

I finished array 2 ! I only had two problems to do, one that I looked over with Mr. Daly which took us a while to solve, and another which was very similar to the first one.

The problem over which I looked at with Mr. Daly, was one that we already looked at, and thought that we had solved but we actually hadn't. We had the code all correct, but we spent almost 20 minutes simply looking at a code that was missing a bracket. There was one line of code that was supposed to be part of an else if statement, but we were missing the bracket. It was very frustrating, but it taught us the lesson that when we write any sort of if statement or loop, to always put the brackets before anything else so that we can avoid mistakes such as these that can waste your time.

Here was the final problem of Array 2:

----------------------------------------------------------------------------------------------------------------------------------

Return an array that contains the exact same numbers as the given array, but rearranged so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. You may modify and return the given array, or make a new array.

evenOdd({1, 0, 1, 0, 0, 1, 1}) → {0, 0, 0, 1, 1, 1, 1}
evenOdd({3, 3, 2}) → {2, 3, 3}
evenOdd({2, 2, 2}) → {2, 2, 2}




When I told Mr. Daly that I had completed Array 2, he gave me a quick test to write by hand, one of the problems that I did a while back in Array 2. I completed it, and that marked the end of class. 

Next class, String 2 !

Thursday, April 16, 2015

Wednesday, April 16th

I am writing my blog a day late, because I was very jet lagged. I let Mr. Daly know about this.

Today we had class last block of the day, and it was my turn to go up to the genius bar with Tommy. Although, like most days, we didn't receive any customers, Preston did come up from the classroom with a question to ask me (I forget about what exactly).

It was a good day. I completed what was probably the hardest problem in Array 2: the FizzBuzz problem. However, I needed to go down to the classroom and ask for Mr. Daly's help to complete it. Without him, there's no way I could've done it.

It was very complicated for me in the sense that it used almost everything that I learned throughout Array 2, and it included a String array, which I had never worked with before, so I didn't know how they worked.

I am actually looking forward to going back to strings so that the problems will be easier to solve for the most part until I reach the end - I'm tired of such complex problems.

Here it is (it's a long one):

----------------------------------------------------------------------------------------------------------------------------------
This is slightly more difficult version of the famous FizzBuzz problem which is sometimes given as a first problem for job interviews. (See also: FizzBuzz Code.) Consider the series of numbers beginning at start and running up to but not including end, so for example start=1 and end=5 gives the series 1, 2, 3, 4. Return a new String[] array containing the string form of these numbers, except for multiples of 3, use "Fizz" instead of the number, for multiples of 5 use "Buzz", and for multiples of both 3 and 5 use "FizzBuzz". In Java, String.valueOf(xxx) will make the String form of an int or other type. This version is a little more complicated than the usual version since you have to allocate and index into an array instead of just printing, and we vary the start/end instead of just always doing 1..100.

fizzBuzz(1, 6) → {"1", "2", "Fizz", "4", "Buzz"}
fizzBuzz(1, 8) → {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7"}
fizzBuzz(1, 11) → {"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz"}


---------------------------------------------------------------------------------------------------------------------------------

On Friday, we have a short class, but its a great opportunity to finish Array 2 finally and move on, which is what I plan on doing.

Monday, April 13, 2015

Monday, April 13th

Today was our first day back from school from Spring Break. We had class first thing in the morning.

We got to class, and spent about 10 minutes reviewing how the blogs were and how they are important and are included in Mr. Daly's final assessment of us for the quarter.

After that, I got to work on my final quarter's module : CodingBat. Like the two previous quarters, I will be working on CodingBat. By the end of the year, I will have finished level 2 of CodingBat, which includes Array 2 and String 2.

I have only 2 more problems to solve in Array 2, but they are very complex for me so they take me a while. For example, today, I only completed two problems.

It might also maybe be because of the fact that I just came back from holiday that I worked slowly today. I guess that after a two week break, my brain isn't back into it yet. I did feel like I needed to refresh my skills, as they were a tiny bit rusty. When compiling, I forgot many small details that I never would've forgotten before the break, such as colons, parentheses, and brackets. Small things like those often stopped me from compiling today as I forgot a bit where to put them.

But besides that, by next class I should be back into the rhythm like I always am. It's good to be back. Next class hopefully I finish Array 2 and move on to String 2.

Here's today's problem (which I did search up a little help for)

----------------------------------------------------------------------------------------------------------------------------------

Return a version of the given array where each zero value in the array is replaced by the largest odd value to the right of the zero in the array. If there is no odd value to the right of the zero, leave the zero as a zero.

zeroMax({0, 5, 0, 3}) → {5, 5, 3, 3}
zeroMax({0, 4, 0, 3}) → {3, 4, 3, 3}
zeroMax({0, 1, 0}) → {1, 1, 0}