Monday, March 31, 2014

Array1 - Codingbat



packagetest.codingbat;

public class Array1 {
      
       // return true if first and last element of array is 6
       public boolean firstLast6(int[] nums) {
             
              int length = nums.length;
              final int number = 6;
                    
              if (nums[0] == number || nums[length-1] == number)
                     return true;
              else
                     return false;
       }
      
       // return true if first and last element of array is same
       public booleansameFirstLast(int[] nums) {
             
              if (nums.length == 0)
                     return false;
              else if (nums[0] == nums[nums.length-1])
                     return true;
              else
                     return false;
       }
      
       // return an integer array with value pi (3.14)
       public int[] makePi() {
             
              int[] pi = new int[]{3,1,4};
              return pi;
       }
      
       // return true if first or last elements of both arrays are same
       public boolean commonEnd(int[] a, int[] b) {
             
              if(a[0] == b[0] || a[a.length-1] == b[b.length-1])
                     return true;
              else
                     return false;
       }
      
       // return sum of the elements
       public int sum3(int[] nums) {
             
              int sum = 0;
              for (int i = 0; i < nums.length; i++) {
                     sum = sum + nums[i];
              }
              return sum;
       }
      
       // rotate elements to the left
       public int[] rotateLeft3(int[] nums) {
               
              int temp = 0;
              temp = nums[0];
              for (int i = 0; i < nums.length; i++) {
                     if(i==nums.length-1) break;
                     nums[i] = nums[++i];
                     --i;
              }
              nums[nums.length-1] = temp;
              return nums;
       }

      
       // return elements in reverse order
       public int[] reverse3(int[] nums) {
                int temp = nums[0];
                nums[0] = nums[nums.length-1];
                nums[nums.length-1] = temp;
                return nums;
       }
      
       // find largest between first and last element and set all elements to that value
       public int[] maxEnd3(int[] nums) {
              int largest = 0;
              largest = (nums[0] > nums[nums.length-1]) ? nums[0] : nums[nums.length-1];
              for(int i = 0; i < nums.length; i++) {
                     nums[i] = largest;
              }
              return nums;
       }
      
       // 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 == 0 ) return 0;
              else if (nums.length == 1) return nums[0];
              else return nums[0] + nums[1];
               
       }
      
       // 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 newArray[] = new int[]{a[1],b[1]};
                return newArray;
       }
      
       // Given an int array, return a new array with double the length where its last element
       // is the same as the original array, and all the other elements are 0.
       // The original array will be length 1 or more.
       public int[] makeLast(int[] nums) {
                int length = nums.length;
                int newArray[] = new int[2*length];
                newArray[newArray.length-1] = nums[nums.length-1];
                return newArray;
       }






}

Thursday, March 20, 2014

Reducing IF's Using Array



public class TooMuchIf {


       public static voidmain(String[] args) {

              TooMuchIf tmi = new TooMuchIf();

              System.out.println(tmi.fightMath(3, 3));

              System.out.println(tmi.fightMathArray(3, 3));

       }

       public int fightMath(int one, int two) {

           int result = -1;

           if(one == 0 && two == 0) { result = 0; }

           else if(one == 0 && two == 1) { result = 0; }

           else if(one == 0 && two == 2) { result = 1; }

           else if(one == 0 && two == 3) { result = 2; }

           else if(one == 1 && two == 0) { result = 0; }

           else if(one == 1 && two == 1) { result = 0; }

           else if(one == 1 && two == 2) { result = 2; }

           else if(one == 1 && two == 3) { result = 1; }

           else if(one == 2 && two == 0) { result = 2; }

           else if(one == 2 && two == 1) { result = 1; }

           else if(one == 2 && two == 2) { result = 3; }

           else if(one == 2 && two == 3) { result = 3; }

           else if(one == 3 && two == 0) { result = 1; }

           else if(one == 3 && two == 1) { result = 2; }

           else if(one == 3 && two == 2) { result = 3; }

           else if(one == 3 && two == 3) { result = 3; }


           return result;

       }
      

       public intfightMathArray(int one, int two) {

             
              final int[][] result = new int[][] {{ 0, 0, 1, 2 }, { 0, 0, 2, 1 }, { 2, 1, 3, 3 }, { 1, 2, 3, 3 }};

              returnresult[one][two];

       }



}