Assignment Two Solution

Q1-2


public class SetProc {

     // elements in a and b are sorted in ascending order
     public static void intersect(int[] a, int[] b)
     {
        int i = 0;
        int j = 0;
        // {I: intersection of a[0..i) and b[0..j) has been printed out,
        //     and the rest of elements are greater than a[0..i)
        //     and b[0..j). }

        while (i != a.length || j != b.length)
        {
            if (i == a.length || j == b.length) break;
            if (a[i] == b[j]) { System.out.print(a[i] + " "); ++i; ++j;}
            else if (a[i] < b[j]) ++i;
            else ++j;
        }

        System.out.println();

     }

     // elements in a and b are sorted in ascending order
     public static void union(int[] a, int[] b)
     {
        int i = 0;
        int j = 0;
        // {I: uion of a[0..i) and b[0..j) has been printed out,
        //     and the rest of elements are greater than a[0..i)
        //     and b[0..j). }

        while (i != a.length || j != b.length)
        {
            if (i == a.length)
            {
               System.out.print(b[j] + " ");
               ++j;
            }
            else if (j == b.length)
            {
               System.out.print(a[i] + " ");
               ++i;
            }
            else if (a[i] == b[j]) { System.out.print(a[i] + " "); ++i; ++j;}
            else if (a[i] < b[j]) { System.out.print(a[i] + " "); ++i;}
            else { System.out.print(b[j] + " "); ++j;}
        }

        System.out.println();

     }

     // elements in a and b are sorted in ascending order
     public static void diff(int[] a, int[] b)
     {
        int i = 0;
        int j = 0;
        // {I: Diff of a[0..i) and b[0..j) has been printed out,
        //     and the rest of elements are greater than a[0..i)
        //     and b[0..j). }

        while (i != a.length || j != b.length)
        {
            if (i == a.length)
               break;
            else if (j == b.length)
            {
               System.out.print(a[i] + " ");
               ++i;
            }
            else if (a[i] == b[j]) { ++i; ++j; }
            else if (a[i] < b[j]) { System.out.print(a[i] + " "); ++i;}
            else  ++j;
        }
        System.out.println();

     }

     public static void main(String[] args)
     {
         int[] a = {1, 3, 5};
         int[] b = {1, 5, 10};
         intersect(a, b);
         union(a, b);
         diff(a, b);
     }


 }

Q3

public class Robot {

     // b[i][j] == 1 if there is an obstacle at row i and col j.
     public static void printAllPaths(int[][] a, int[][]b, int r, int c)
     {
        if (r == a.length-1 && c == a[0].length-1)
        {
           for (int i = 0; i < a.length; ++i)
           {
             for (int j = 0; j < a[0].length; ++j)
             {
                if (b[i][j] == 1)
                   System.out.print("x" + " ");
                else
                   System.out.print(a[i][j] + " ");
             }
                System.out.println();
           }
           System.out.println();

           return;
        }


        if (r < a.length-1 && b[r+1][c] != 1)
        {
           a[r+1][c] = 1;
           printAllPaths(a, b, r+1, c);
           a[r+1][c] = 0;
        }

        if (c < a[0].length-1 && b[r][c+1] != 1)
        {
           a[r][c+1] = 1;
           printAllPaths(a, b, r, c+1);
           a[r][c+1] = 0;
        }

     }

     public static void main(String[] args)
     {
           int[][] a = new int[5][5];
           int[][] b = new int[5][5];
           b[2][2] = 1;
           b[3][3] = 1;
           a[0][0] = 1;
           printAllPaths(a, b, 0, 0);
     }

}

Q4


public class Telephone {

    public static void printPhoneString(char[] a, int n, String s,
                                         char[][] table)
    {
       if (n == s.length())  // make sure a.length == s.length()
       {
          for (int i = 0; i < a.length; ++i)
             System.out.print(a[i] + " ");
          System.out.println();
          return;
       }

       char c = s.charAt(n);
       for (int i = 0; i < table[c - '0'].length; ++i)
       {
            a[n] = table[c - '0'][i];
            printPhoneString(a, n+1, s, table);
       }
    }

    public static char[][] makeTable()
    {
       char[][] table = new char[10][];
       table[0] = new char[1];
       table[0][0] = ' ';
       table[1] = new char[1];
       table[1][0] = ' ';
       table[2] = new char[3];
       table[2][0] = 'a';  table[2][1] = 'b';  table[2][2] = 'c';
       table[3] = new char[3];
       table[3][0] = 'd';  table[3][1] = 'e';  table[3][2] = 'f';
       table[4] = new char[3];
       table[4][0] = 'g';  table[4][1] = 'h';  table[4][2] = 'i';
       table[5] = new char[3];
       table[5][0] = 'j';  table[5][1] = 'k';  table[5][2] = 'l';
       table[6] = new char[3];
       table[6][0] = 'm';  table[6][1] = 'n';  table[6][2] = 'o';
       table[7] = new char[4];
       table[7][0] = 'p';  table[7][1] = 'q';  table[7][2] = 'r'; table[7][3] = 's';
       table[8] = new char[3];
       table[8][0] = 't';  table[8][1] = 'u';  table[8][2] = 'v';
       table[9] = new char[4];
       table[9][0] = 'w';  table[9][1] = 'x';  table[9][2] = 'y'; table[9][3] = 'z';
       return table;
    }

    public static char[][] makeTable2()
    {
       char[][] table = { {' '},
                          {' '},
                          {'a', 'b', 'c'},
                          {'d', 'e', 'f'},
                          {'g', 'h', 'i'},
                          {'j', 'k', 'l'},
                          {'m', 'n', 'o'},
                          {'p', 'q', 'r', 's'},
                          {'t', 'u', 'v'},
                          {'w', 'x', 'y', 'z'} };
      return table;
    }



       public static void main(String[] args)
    {
       String s = "617552";
       char[][] table = makeTable2();
       char[] a = new char[s.length()];
       printPhoneString(a, 0, s, table);
    }

}

Q5

public class Reg {

     public static boolean match(String exp, String s)
     {
        boolean allstar = true;
        for (int i = 0; i < exp.length(); ++i)
        {
            if (exp.charAt(i) != '*') allstar = false;
        }

        if (allstar) return true;
        if (s.length() == 0 && exp.length() == 0) return true;
        if (s.length() == 0 || exp.length() == 0) return false;

        if (exp.charAt(0) == '.')
            return match(exp.substring(1), s.substring(1));
        else if (exp.charAt(0) == '*')
            return match(exp, s.substring(1)) || match(exp.substring(1), s);
        else
            return (s.charAt(0) == exp.charAt(0)) &&
                   match(exp.substring(1), s.substring(1));
     }

     public static void main(String[] args)
     {
        System.out.println(match("a*c", "ac"));
        System.out.println(match("*a*c*", "aabc"));
        System.out.println(match("*a*c*b", "aabc"));
     }
}