Thursday, March 5, 2015

Thursday, March 5th

I went up to the Genius Bar with Tommy today. No customers.

Apart from that, it was a regular class. I felt like today I got a lot done, yet did not complete a lot of problems. Since I was in the library, I didn't want to go all the way downstairs to see Mr. Daly every single time I had a problem. So I worked a lot, but only completed about four problems. All the other problems were almost completely solved - there were just a few outcomes and few glitches in the code that I wasn't able to fix, so I couldn't finish the entire problem. But I am sure that next class I will go over these small problems with Mr. Daly.
------------------------------------------------------------------------------------------------------------

Here is today's problem:


Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}. 

has77({1, 7, 7}) → true
has77({1, 7, 1, 7}) → true
has77({1, 7, 1, 1, 7}) → false

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

public boolean has77(int[] nums) {
  
  
  
  for (int i=0;i<nums.length-2;i++)
// we want the loop to check until nums.length-2, because we need check for two digits up until the last digit.
  {
   if (nums.length<2)
   {
    return false;
// if the string is less than length 2, then there can't possibly be two 7s.
   }
   
   if (nums[nums.length-2] == 7 && nums[nums.length-1]==7)
   {
     return true;
//if the last two digits are 7, then return true.
   }
   
   if (nums[i]==7 && nums[i+1]==7)
   {
     return true;
// if one number is 7, and the number next to it is 7, then return true;
   }
  
   if (nums[i]==7 && nums[i+2]==7)
   {
     return true;
// if a number is 7, and the number 2 spaces after it is also 7, then return true.
   }
   
  }
  
  return false;
// if none of the above conditions are true, then return false.

}
------------------------------------------------------------------------------------------------------------

Next class, moving forward with Array 2.

No comments:

Post a Comment