Monday, January 12, 2015

Monday, January 12th

Today was a very short day. We had school until 11:00. I came into class and went straight to work: string and array problems. I asked for Mr. Daly's help for some problems, but mostly arrays because I am starting to get the hang of strings. Besides that, there was nothing else new or interesting, so I'll get straight to the problem.


Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more. 

commonEnd({1, 2, 3}, {7, 3}) → true
commonEnd({1, 2, 3}, {7, 3, 2}) → false
commonEnd({1, 2, 3}, {1, 3}) → true

public boolean commonEnd(int[] a, int[] b) {
 
  if (a[0]==b[0])
// if the first value of a or b ([0]) is equal to the other, then return true.
  {
    return true;
  }
 
  if (a[a.length-1] == b[b.length-1])
  {
    return true;
// if the last value of a or b ([str.length-1]) is equal to the other, then return true.
  }

  return false;
// if none of the above conditions are true, then return false.
}




When I first did this problem, I one issue. I created two new arrays a and b, and that kind of overrode what the computer had already given me : two arrays a and b with set values.

No comments:

Post a Comment