Search This Blog

Saturday, August 17, 2013

Coding Bat Java Array-1 solutions 1 to 14


This post contains the solutions to Java Array-1 section of codingbat.com for problems 1 to 14. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners.Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2, java-string1.

1.       Problem Name : firstLast6 
Solution:
Hint: Return true if 6 appears as either the first or last element in the array.
Constraint: The array will be length 1 or more. 

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

2.       Problem Name : sameFirstLast 
Solution:
Hint: 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)
       {
                if(nums.length>1)
                       return((nums[0]==nums[nums.length-1]));
                else if(nums.length==1)
                       return true;
                else
                       return false;
       }

3.       Problem Name : makePi 
Solution:
Hint: Return an int array length 3 containing the first 3 digits of pi, {3, 1, and 4}. 

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

  
4.       Problem Name : commonEnd 
Solution:
Hint:  return true if they have the same first element or they have the same last element

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

5.       Problem Name : sum3 
Solution:
Hint:   return the sum of all the elements. 

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

6.       Problem Name : rotateLeft3 
Solution:
Hint:  return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.


public int[] rotateLeft3(int[] nums)
       {
                int i=0;int first=0;
                first=nums[0];
                for(i=1;i<nums.length;i++)
                       nums[i-1]=nums[i];
               
                nums[nums.length-1]=first;
               
                return nums;
               
       }

7.       Problem Name : reverse3 
Solution:
Hint: 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 tmp=0;
               
                tmp=nums[0];
                nums[0]=nums[2];
                nums[2]=tmp;
               
                return nums;
       }


8.       Problem Name : maxEnd3 
Solution:
Hint:   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 max=0;
               if(nums[0]>nums[nums.length-1])
                      max=nums[0];
               else
                      max=nums[2];
               
               nums[0]=max;
               nums[1]=max;
               nums[2]=max;
               return nums;
       }

9.       Problem Name : sum2
Solution:
Hint:   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)
       {
              int i=0,sum=0;
                if(nums.length>2)
                {
                for(i=0;i<2;i++)
                       sum+=nums[i];
                }
                else
                {
                for(i=0;i<nums.length;i++)
                       sum+=nums[i];
                }
                return sum;
       }

10.   Problem Name : middleWay 
Solution:
Hint:   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 = new int[2];
               
                c[0]=a[1];
                c[1]=b[1];
               
                return c;
       }

11.   Problem Name : makeEnds 
Solution:
Hint: 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[] res = new int[2];

              if(nums.length>1)
              {
               
                res[0]=nums[0];
                res[1]=nums[nums.length-1];
              }
             
              if(nums.length==1)
              {
                res[0]=nums[0];
                res[1]=nums[0];
               }
             
                 return res;

       }


12.   Problem Name : has23 
Solution:
Hint:  Given an int array length 2, return true if it contains a 2 or a 3. 

public boolean has23(int[] nums)
       {
              return(nums[0]==2||nums[1]==2||nums[0]==3||nums[1]==3);
       }

13.   Problem Name : no23 
Solution:
Hint:  Given an int array length 2, return true if it does not contain a 2 or 3. 

public boolean no23(int[] nums)
       {
              return(!(nums[0]==2||nums[1]==2||nums[0]==3||nums[1]==3));
       }


14.       Problem Name : makeLast 
Solution:
Hint: 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
Constraint: The original array will be length 1 or more.

   public int[] makeLast(int[] nums)
       {
                int[] a = new int[nums.length*2];
                a[a.length-1]=nums[nums.length-1];
               
                return a;
       }

Please check my posts for solutions to remaining problems of Array-1 section of codingbat.com.

Friday, August 16, 2013

Coding Bat Java String 1 solutions 26 to 33

This post contains the solutions to Java String-1 section of codingbat.com for problems 26 to 33. If you are new here, you can check my previous post learn-coding-computer-programming-for-beginners. Solutions to problems 1 to 18 are available in the link java-string1-part-1, java-string1-part2.Solutions to previous sections are also available check the links to java-warmup-1 , java-warmup-2.

26.       Problem Name : frontAgain 
Solution:
Hint: Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as with "edited". 
public boolean frontAgain(String str)
       {
                if(str.length()>2)
                {
                       if(str.substring(0,2).equals(str.substring(str.length()-2,str.length())))
                             return true;
                       else
                             return false;
                }
                else if(str.length()==2)
                       return true;
                else
                       return false;
       }

27.       Problem Name : minCat 
Solution:
Hint: Given two strings, append them together (known as "concatenation") and return the result
Constraint: However, if the strings are different lengths, omit chars from the longer string so it is the same length as the shorter string. So "Hello" and "Hi" yield "loHi". The strings may be any length. 
public String minCat(String a, String b)
       {
              if(a.length()>b.length())
                     return(a.substring(a.length()-b.length(),a.length())+b);
              else
                     return(a+b.substring(b.length()-a.length(),b.length()));

       }

28.       Problem Name : extraFront 
Solution:
Hint: Given a string, return a new string made of 3 copies of the first 2 chars of the original string.
Constraint: The string may be any length. If there are fewer than 2 chars, use whatever is there. 

       public String extraFront(String str)
       {
                String result="";int i=0;
                if(str.length()>2)
                {
                       for(i=0;i<3;i++)
                             result+=str.substring(0,2);
                }
                else
                {
                       for(i=0;i<3;i++)
                             result+=str.substring(0,str.length());
                }
                return result;
       }

29.       Problem Name : without2 
Solution:
Hint: Given a string, if a length 2 substring appears at both its beginning and end, return a string without the substring at the beginning, so "HelloHe" yields "lloHe".
Constraint: The substring may overlap with itself, so "Hi" yields "". Otherwise, return the original string unchanged. 
public String without2(String str)
       {
              int len=str.length();
                if(str.length()>2)
                {
                      if(str.substring(0,2).equals(str.substring(len-2,len)))
                            return(str.substring(2,len));
                      else
                            return str;
                }
                else if(str.length()==1)
                       return str;
                else
                       return "";
       }

30.       Problem Name : deFront 
Solution:
Hint: Given a string, return a version without the first 2 chars. except keep the first char if it is 'a' and keep the second char if it is 'b'.
Constraint: The string may be any length.
public String deFront(String str)
       {   
                int len=str.length();
                int flag=0;
                String result="";
               
                if(str.charAt(1)=='b')
                {
                       result=(str.charAt(1)+str.substring(2,len));
                       flag=1;
                }
               
                if(str.charAt(0)=='a')
                {
                  if(flag==1)
                  {
                     result=(str.charAt(0)+result);
                  }
                  else
                     {
                       result=str.charAt(0)+str.substring(2,len);
                     }
                  flag=1;
                }
               
                if(flag!=1)
               
                {
                result= str.substring(2,len);
                }
                return result;
       }

31.       Problem Name : startWord 
Solution:
Hint: Given a string and a second "word" string, we'll say that the word matches the string if it appears at the front of the string, except its first char does not need to match exactly.
Constraint: On a match, return the front of the string, or otherwise return the empty string. So, so with the string "hippo" the word "hi" returns "hi" and "xip" returns "hip". The word will be at least length 1. 
public String startWord(String str, String word)
       {
              int len=word.length();
              if(str.length()==0)
                     return new String("");
              if(len==1 ){
                     return str.substring(0, 1);
              }
              String str1="";

              if(str.length()>=word.length())
              {
               str1=str.substring(1,len);
              }
              else
              {
               str1="";
              }
              word=word.substring(1);
              if(word.equals(str1))
                     return str.substring(0, len);
              else
                     return new String("");

       }

32.       Problem Name : withoutX 
Solution:
Hint: Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged. 
public String withoutX(String str)
       {
                int len=str.length();
                String result="";
                if(len>2)
                {
                       if(str.charAt(0)=='x' && str.charAt(len-1)=='x')
                             result=str.substring(1,len-1);
                       else if(str.charAt(0)=='x' || str.charAt(len-1)=='x')
                       {
                             if(str.charAt(0)=='x')
                                    result=str.substring(1,len);
                             if(str.charAt(len-1)=='x')
                                    result=str.substring(0,len-1);
                       }
                       else
                             result=str;
               
                }
                else
                {
                if(!str.contains("x"))
                       result=str;
               
                }
                return result;
       }


33.       Problem Name : withoutX2 
Solution:
Hint: Given a string, if one or both of the first 2 chars is 'x', return the string without those 'x' chars, and otherwise return the string unchanged.
public String withoutX2(String str)
       {
                int len=str.length();
                String result="";
                if(str.length()>1)
                {
                       if(str.charAt(0)=='x' && str.charAt(1)=='x')
                       {
                             str=str.substring(1,1)+str.substring(2,str.length()); 
                             result=str;
                       }
                       else if(str.charAt(0)=='x')
                       {
                             str=str.substring(1,2)+str.substring(2,str.length()); 
                             result=str;
                       }
                       else if(str.charAt(1)=='x')
                       {
                             str=str.substring(0,1)+str.substring(2,str.length()); 
                             result=str;

                       }
                       else
                       {
                             result=str;
                       }
                }
                else
                {
                       result="";
                }

                  return result;
              }

Please check my posts for solutions to other codingbat.com sections. 

Labels