Wednesday, October 28, 2015

test 10 corrections


5)Consider this static method:

  public static String check( String[] a, String[] b )
  {
    int i;

    for ( i = 0 ; i < a.length && i < b.length ; i++ )
    {
      if ( a[ i ].equals( b[ i ] ) )
        return a[ i ];
    }

    return "None";
  }
If s and t are initialized arrays of Strings, and if the elements of both arrays are initialized, then which of the following best describes the value of check( s, t )?
  1. "None", if s and t have different lengths; otherwise, the index of the first element of s that is the same as the corresponding element of t.
  2. "None", if s and t have different lengths; otherwise, the first element of s that is the same as the corresponding element of t.
  3. The first element of s that is the same as the corresponding element of t, or "None" if s has no such element.
  4. The last element of s that is the same as the corresponding element of t, or "None" if s has no such element.
  5. An error, if s and t have different lengths; otherwise, the first element of s that is the same as the corresponding element of t, or "None", if s has no such element.

now in this one I thought there would be an error if the lengths were diffrent because the if statement would make an out of bounds error if it tryed to test outside of the bounds but apparently I was wrong.

9)Consider this static method:

  public static boolean checkIntArray( int[] a )
  {
    int i = 1;
    while ( i < a.length )
    {
      if ( a[ i - 1 ] > a[ i ] )
        return false;

      i++;
    }

    return true;
  }
checkIntArray( a ) returns the boolean true for exactly one of the following arrays a. Which one?
  1. a = { -1, -2, -3 }
  2. a = { 1, 11, 111, 2, 22, 222 }
  3. a = { 1, -2, 3, -4, 5 }
  4. a = { 5, 4, 3, 2, 1 }
  5. a = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }


the answer I got for this was 4 when in reality it was 5 because I did not notice that the if statement was > and believed that then the numbers had to be lesser than the last to get a true when they had to be greater than or equal to.

10)The static method atLongLast takes an array a of non-empty Strings and returns a String containing just the last character of the longest String in a. (If there are more than one equally longest Strings in a, use the one with the smallest index in a. If a has no elements, return the empty String "".) For example:

10)atLongLast( { "quite", "a", "short", "array" } )
has the value "e", and
atLongLast( { "has", "one", "humongously", "long", "string" } )
has the value "y". Which of the following are suitable definitions for atLongLast?

  public static String atLongLast( String[] a )
  {
    if ( a.length == 0 )
      return "";

    int maxIdx = 0;
    for ( int i = 1 ; i < a.length ; i++ )
    {
      if ( a[ maxIdx ].length() < a[ i ].length() )
        maxIdx = i;
    }
    return a[ maxIdx ].substring( a[ maxIdx ].length() - 1 );
  }

  public static String atLongLast( String[] a )
  {
    String s = "";
    for ( int i = 0 ; i < a.length ; i++ )
    {
      if ( s.length() < a[ i ].length() )
        s = a[ i ];
    }
    if ( s.equals( "" ) )
      return "";
    else
      return s.substring( s.length() - 1 );
  }

  public static String atLongLast( String[] a )
  {
    String s = "";
    int maxIdx = 0;
    for ( int i = 1 ; i < a.length ; i++ )
    {
      if ( a[ maxIdx ].length() < a[ i ].length() )
      {
        s = a[ i ].substring( a[ i ].length() - 1 );
        maxIdx = i;
      }
    }
    return s;
  }
  1. I only
  2. II only
  3. I and II only
  4. I and III only
  5. I, II, and III

Monday, October 26, 2015

test 9 corrections

2)Consider the following code fragment:
int[] r = { 9, 2, 4, 3, 7, 5, 0, 1 };
for ( int t : r )
{
  t = t + 1;
}
Value: 1 point.
Which of the following is a true statement?
  1. The code will execute and the elements of the array r will be unchanged.
  2. The code will execute and each element of the array r will be incremented.
  3. The code will execute and each element of the array r will be the sum of the previous elements.
  4. The code will execute and each element t of the array r will be the sum of the first t integers.
  5. The code will generate an error.

I got this wrong because I chose 2 believing that changing t would change the array that was incorrect. thus array r would not be changed therefore the answer would be 1.

 3)what is output by the following code fragment? (You may assume that the for-each loop references the array elements in index order.) String[] p = { "A", "B", "C", "D" };
String b = "";

for ( String q : p )
   b = q + b;

System.out.println( b )

7) I got this wrong because I forgot that q+b is diffrent from b+q and beleved that adding onto a string always happened at the end. in hindsight it was kind of a silly mistake the real answer I should have got was DCBA.

 What can be said about the value of d after the comment is replaced by an actual initialization statement and the resulting code is executed? String[] a;

// code to initialize a

boolean d = false;

for ( String b : a )
{
  for ( String c : a )
  {
    if ( b.equals( c ) )
      d = true;
  }
}
I got this wrong because I neglected to test the code if a had nothing in it and thus I missed that a is false if there is nothing in the list. believing that  anything I entered in for a would output true I answered A when in reality it was B (The value is true if a contains at least one element; otherwise it is false).

Tuesday, October 20, 2015

for loop test corrections

6)The array a has elements of data type int. The following code is intended to calculate the index of the first occurrence of the value that is least among all the elements of a, and to store that index in the variable minindex.
    int k;
    int minindex = 0;

    for ( k = 1 ; k < a.length ; k++ )
    {
      if ( condition )
        statement
    }
Which of the following replacements for condition and statement will cause the code to work as intended?


 condition statement
a[ k ] < minindex
minindex = a[ k ];
a[ k ] < a[ minindex ]
minindex = a[ minindex ];
a[ k ] < a[ minindex ]
minindex = k;
a[ k ] > minindex
minindex = a[ k ];
a[ k ] < minindex
minindex = k;
6) I got 7 wrong because I forgot that minindex stored an index and thus it had to refrence a to compare itself to a[k].

while loops test corrections

5)What is the value of g after the following code is executed?
    boolean p = false;
    int[] a = { 5, 4, 9, 2, 5, 3, 0, -1 };
    int g = 0;

    while ( !p && g < a.length ) 
    {
      p = ( p || (a[ g ] % 3 == 0) );
      g++;
    } 
  1. 0
  2. 2
  3. 3
  4. 5
  5. 8
5) 5 is incorrect because I did not notice that the g++ was after p would have been set to true so the answer would be one more than the index of the first number divisible by x.
9)What is the value of m after the following code is executed?
    int n = 314159;
    int m = 0;
    while ( n > 0 )
    {
      m = 10 * m + n % 10;
      n /= 10;
    }
  1. 31415.9
  2. 314159
  3. 317322713
  4. 951413
  5. The program enters an infinite loop and never completes.
9) I got 9 wrong because I did not notice n was an intiger so I thought that the idea that "the more you divide the closer it gets to zero but never truly hits zero" forgetting that because it is an int and because ints truncate and thus x/10 will become zero if x is a single digit number


10)Recall that, if s and t are Strings, then s.indexOf( t ) returns -1 if t is not a substring of s. However, if t is a substring of s, then s.indexOf( t ) returns the index in s of the first character of the first appearance within s of t. For example,
"abcbcdcde".indexOf( "cd" ) returns 4
"abcbcdcde".indexOf( "dcb" ) returns -1.
Assuming that s and t are Strings, which of the following code fragments are such that the value returned by s.indexOf( t ) before the code is executed is equal to the value of the int n after the code is executed?
    int n = -1;
    int tSize = t.length();
    boolean found = false;
    while ( !found && tSize <= s.length() )
    {
      n++;
      if ( t.equals( s.substring( 0, tSize ) ) )
        found = true;
      else
        s = s.substring( 1 );
    }
    if ( !found )
      n = -1;     
    int n = -1;
    int sSize = s.length();
    int tSize = t.length();
    boolean found = false;
    while ( !found && n + tSize + 1 <= sSize )
    {
      n++;
      if ( t.equals( s.substring( n, n + tSize ) ) )
        found = true;
    }
    if ( !found )
      n = -1;     
    int n = 0;
    int sSize = s.length();
    int tSize = t.length();
    boolean found = false;
    while ( !found && n + tSize <= sSize )
    {
      if ( t.equals( s.substring( n, n + tSize ) ) )
        found = true;

      n++;
    }
    if ( found )
      n--;
    else
      n = -1;
  1. I only
  2. II only
  3. III only
  4. I and II only
  5. I, II, and III
10)I got ten wrong because I just couldn't understand how the answers were trying to a accomplish their goals so I wrote my step by step write-up incorrectly and subsequently got the wrong answer.  

Thursday, October 15, 2015

coding bat for loops III: loop harder

1)Given a string, return a string made of the chars at indexes 0,1, 4,5, 8,9 ... so "kittens" yields "kien".

altPairs("kitten") → "kien"
altPairs("Chocolate") → "Chole"
altPairs("CodingHorror") → "Congrr

public String altPairs(String str) {
int a = 0;
String bricks = "";
for(int i = 0;i <str.length();i++){
  if(a == 0 || a == 1){
    bricks += str.substring(i,i+1);
  }else if(a == 3){
  a = -1;
  }
  a++;
}
return bricks;
}

2)Given an array of ints, return the number of times that two 6's are next to each other in the array. Also count instances where the second "6" is actually a 7.

array667({6, 6, 2}) → 1
array667({6, 6, 2, 6}) → 1
array667({6, 7, 2, 6}) → 1

public int array667(int[] nums) {
  int a = 0;
  for(int i = 0;i < nums.length -1; i++){
    if (nums[i] == 6 &&(nums[i+1] == 6 || nums[i+1] == 7)){
    a += 1;
    }
  }
  return a;
}

3)Given an array of ints, we'll say that a triple is a value appearing 3 times in a row in the array. Return true if the array does not contain any triples.

noTriples({1, 1, 2, 2, 1}) → true
noTriples({1, 1, 2, 2, 2, 1}) → false
noTriples({1, 1, 1, 2, 2, 2, 1}) → false

public boolean noTriples(int[] nums) {
  for(int i = 0; i < nums.length-2;i++){
    if (nums[i] == nums[i+1] && nums[i] == nums[i+2]){
      return false;
    }
  }
  return true;
}

4)

Tuesday, October 13, 2015

coding bat for loops 2: electric boogaloo

)1)Given a string, return a new string made of every other char starting with the first, so "Hello" yields "Hlo".

stringBits("Hello") → "Hlo"
stringBits("Hi") → "H"
stringBits("Heeololeo") → "Hello"

public String stringBits(String str) {
String a = "";
  for(int i = 0;i < str.length();i++){
    if(i == 0 || (i % 2)== 0){
      a += str.substring(i,i+1);
    }
  }
return a;
}

2)Given a non-empty string like "Code" return a string like "CCoCodCode".

stringSplosion("Code") → "CCoCodCode"
stringSplosion("abc") → "aababc"
stringSplosion("ab") → "aab"

public String stringSplosion(String str) {
  String a ="";
  for (int i = 0; i <= str.length();i++){
  a += str.substring(0,i);
  }
  return a;
}
 3)Given an array of ints, return the number of 9's in the array.

arrayCount9({1, 2, 9}) → 1
arrayCount9({1, 9, 9}) → 2
arrayCount9({1, 9, 9, 3, 9}) → 3

public int arrayCount9(int[] nums) {
int a = 0;
  for(int i = 0; i< nums.length;i++){
    if(nums[i] == 9){
    a ++;
    }
  }
  return a;
}
4)Given an array of ints, return true if one of the first 4 elements in the array is a 9. The array length may be less than 4.

arrayFront9({1, 2, 9, 3, 4}) → true
arrayFront9({1, 2, 3, 4, 9}) → false
arrayFront9({1, 2, 3, 4, 5}) → false

public boolean arrayFront9(int[] nums) {
  for( int i = 0;i < nums.length;i++){
    if(i <= 3 && nums[i] == 9){
     return true;
    }
  }
  return false;
}
5)Given an array of ints, return true if .. 1, 2, 3, .. appears in the array somewhere.

array123({1, 1, 2, 3, 1}) → true
array123({1, 1, 2, 4, 1}) → false
array123({1, 1, 2, 1, 2, 3}) → true

public boolean array123(int[] nums) {
  for(int i = 1;i < nums.length-1;i++){
    if(nums[i-1] ==  1 && nums[i] ==  2 && nums[i+1] ==  3){
      return true;
    }
  }
  return false;









Friday, October 9, 2015

coding bat for loops

1)Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.

 public int countEvens(int[] nums) {
int a = 0;
  for(int i = 0; i < nums.length; i++){
  if(nums[i]% 2 == 0){
    a ++;
    }
  }
  return a;
}

2)Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Note: the built-in Math.min(v1, v2) and Math.max(v1, v2) methods return the smaller or larger of two values.

 public int bigDiff(int[] nums) {
   int a = 0;
   int z = 1000000000;
   for(int i = 0; i < nums.length;i++){
     a = Math.max(a,nums[i]);
     z = Math.min(z,nums[i]);
   }
   return(a-z);
}

3) Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.

public int centeredAverage(int[] nums) {
   int a = 0;
   int z = 1000000000;
   int s = 0;
   for(int i = 0; i < nums.length;i++){
     a = Math.max(a,nums[i]);
     z = Math.min(z,nums[i]);
     s += nums[i];
   }
   return((s-a-z)/(nums.length-2));
}

4)eturn the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.

public int sum13(int[] nums) {
  int a = 0;
  for(int i = 0;i < nums.length;i++){
  if(nums[i] != 13 && (i == 0 || nums[i-1] != 13)){
    a += nums[i];
  }
  }
  return a;
}

5)Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.

public int sum67(int[] nums) {
  Boolean a = true;
  int b = 0;
  for(int i = 0 ; i < nums.length ; i++){
    if(nums[i] == 6){
      a = false;
    }else if(nums[i] == 7 && !a){
      a = true;
    }else if(a){
      b += nums[i];
    }
  }
  return b;
}

6)Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.

public boolean has22(int[] nums) {
  for(int i = 0;0 < nums.length;i++){
    if(nums[i] == 2 && i != 0 && nums[i-1] == 2){
      return true;
    }
  }
  return false;
 }

Thursday, October 8, 2015

coding bat while loops

1)Given a string and a non-negative int n, return a larger string that is n copies of the original string.

public String stringTimes(String str, int n) {
  String f = "";
  while(n > 0){
      f += str;
      n--;
  }
  return f;
}


2)Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;

public String frontTimes(String str, int n) {
  String f = "";
  if (str.length() > 3){
    while(n > 0){
      f += str.substring(0,3);
      n--;
    }
  }else {
    while(n > 0){
      f += str;
      n--;
    }
  }
  return f;
}

3)Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx"

int countXX(String str) {
  int xx = 0;
  int y = 0;
  while(y <= str.length()-2){
     if("xx".equals(str.substring(y,y+2))){
     xx += 1;
     }
     y++;
  }
  return xx;
}

AP compsci test 6 errors

1) this was incorrect because I forgot that int truncate therefore y does not equal 1.5 as I had thought but actually equals 1

5)as I did with 1 I again forgot that ints truncate and therefore x/y would almost always equal one so x/y >1 is not a good indicator for greater or less than

6)I did not not notice that all of the statements were ifs therefore I believed that the program would terminate after the first if that was triggered.

7)in this one I beveled that you could set a new variable in a block with the same names as one outside of a block. I needless to say I was wrong.

Tuesday, October 6, 2015

codingbat logic-2

1)We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks

errors in array test

5)
What is the output from this program?
    double[] w = new double[ 4 ];

    w[ 0 ] = 5.0;
    w[ 1 ] = (2 * w[ 0 ]);
    w[ 2 ] = (2 * w[ 1 ]);
    w[ 3 ] = (2 * w[ 2 ]);

    System.out.println( w[ 2 ] + w[ 3 ] );

I got 5 wrong because of an off by one error. I answered 30 because I thought the array position system was 1,2,3,4 instead of 0,1,2,3. if I had done the problem correctly I would have gotten 60 instead.

9)
After the following code executes, what is the value of a[ 3 ][ 2 ]?
    int[][] a = 
        { 
          { 1, 2, 3, 4 },
          { 5, 6, 7, 8 },
          { 9, 10, 11, 12 },
          { 13, 14, 15, 16 }
        }; 
In this one I mixed up the coordinate system and thought of it mathematically thinking 3 was the column number and that 2 was the row number. in actuality 3 is the row number and 2 is the column number therefore the answer should have been 10.   

Monday, October 5, 2015

The first ten problems in Codingbat arrays 1

coding bat arrays 1

1)Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more:

public boolean firstLast6(int[] nums) {
  return(nums[0] == 6|| nums[nums.length-1] == 6);
}

2)Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal. 

public boolean sameFirstLast(int[] nums) {
  return(nums.length > 0 && nums[0]==nums[nums.length - 1] );
}

3)Return an int array length 3 containing the first 3 digits of pi, {3, 1, 4}. 

public int[] makePi() {
  int[] pi ={3,1,4};
  return pi;
}

4)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. 

public boolean commonEnd(int[] a, int[] b) {
  return(a[a.length -1] == b[b.length -1]||a[0] == b[0]);
}

5)Given an array of ints length 3, return the sum of all the elements. 

public int sum3(int[] nums) {
  return(nums[0]+nums[1]+nums[2]);

}

6)Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.

public int[] rotateLeft3(int[] nums) {
int[] a = {nums[1],nums[2],nums[0]};
  return(a);
}

7)Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}. 


public int[] reverse3(int[] nums) {
  int[] a ={nums[2],nums[1],nums[0]};
  return (a);
}

8)Given an array of ints length 3, figure out which is larger between the first and last elements in the array, and set all the other elements to be that value. Return the changed array. 

public int[] maxEnd3(int[] nums) {
int a = nums[0];
int b = nums[nums.length-1];
  if(nums[0]>nums[nums.length-1]){
  nums[1] = a;
  nums[2] = a;
  }else{
  nums[1] = b;
  nums[0] = b;
  }
  return nums;
}

9)Given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.

public int sum2(int[] nums) {
  if(nums.length >= 2){
  return(nums[1]+nums[0]);
  }else if(nums.length == 1){
  return(nums[0]);
  }else{
  return 0;
  }
}

10)Given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements. 

public int[] middleWay(int[] a, int[] b) {
  int[] c = {a[1],b[1]}; 
  return c;
}
11)Given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.

public int[] makeEnds(int[] nums) {
  int[] ends = {nums[0],nums[nums.length-1]};
  return ends;
}