Advertisement
binibiningtinamoran

Recursion

May 27th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.98 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class TestDriver {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.  
  8.         System.out.println("******TESTING Q WITHOUT U");
  9.         System.out.println("\nAll of these should be false. They do NOT contain a \"q\" that is not immediately followed by a \"u\"");
  10.         System.out.println("uuquqt: true " + qNotFollowedByU("uuquqt"));
  11.         //System.out.println("auquqt: true: " + qNotFollowedByU("auquqt"));
  12.         System.out.println("quota: true: " + qNotFollowedByU("quota"));
  13.         System.out.println("michael: false: " + qNotFollowedByU("michael"));
  14.         System.out.println("qqt: true: " + qNotFollowedByU("qqt"));
  15.         System.out.println("auquqtq: true " + qNotFollowedByU("auquqtq"));
  16.         System.out.println("hello: \t\tfalse: " + qNotFollowedByU("hello")); // no q at all- odd length
  17.         System.out.println("cats: \t\tfalse: " + qNotFollowedByU("cats")); // no q at all- even length
  18.         System.out.println("a: \t\tfalse: " + qNotFollowedByU("a")); // no q at all- single letter
  19.         System.out.println("quite: \t\tfalse: " + qNotFollowedByU("quite")); // q followed by u at the beginning
  20.         System.out.println("equal: \t\tfalse: " + qNotFollowedByU("equal")); // q followed by u in the middle- odd length
  21.         System.out.println("aqua: \t\tfalse: " + qNotFollowedByU("aqua"));    // q followed by u in the middle- even length
  22.         System.out.println("nonsensewordqu: false: " + qNotFollowedByU("nonsensewordqu")); // q followed by u at the end
  23.         System.out.println("QUOTE: \t\tfalse: " + qNotFollowedByU("QUOTE")); // q followed by u in caps
  24.         System.out.println("qu: \t\tfalse: " + qNotFollowedByU("qu")); // q followed by u and nothing else
  25.         System.out.println("\"\": \t\tfalse: " + qNotFollowedByU("")); // empty string 
  26.  
  27.         System.out.println("\nAll of these should be true. They DO contain a \"q\" that is not immediately followed by a \"u\"");
  28.         System.out.println("aaauqaqu: \ttrue: " + qNotFollowedByU("aaauqaqu"));
  29.         System.out.println("ququququq: true " + qNotFollowedByU("ququququq"));
  30.         System.out.println("qat: \t\ttrue: " + qNotFollowedByU("qat")); // q without u at the beginning
  31.         System.out.println("cinq: \t\ttrue: " + qNotFollowedByU("cinq")); // q without u at the end- even length
  32.         System.out.println("drinq: \t\ttrue: " + qNotFollowedByU("drinq")); // q without u at the end- odd length
  33.         System.out.println("nonsenseqword: \ttrue: " + qNotFollowedByU("nonsenseqword")); // q without u in the middle
  34.         System.out.println("aquaq: \t\ttrue: " + qNotFollowedByU("aquaq")); // q without u in a word that also has a "qu" before it
  35.         System.out.println("qaqu: \t\ttrue: " + qNotFollowedByU("qaqu")); // q without u in a word that also has a "qu" after it
  36.         System.out.println("qiteu: \t\ttrue: " + qNotFollowedByU("qiteu")); // q without u right after, but with a u later on
  37.         System.out.println("q: \t\ttrue: " + qNotFollowedByU("q")); // q all on its own- single letter
  38.         System.out.println("qq: \t\ttrue: " + qNotFollowedByU("qq")); // q all on its own- double letter
  39.         System.out.println("uq: \t\ttrue: " + qNotFollowedByU("uq")); // q with a u before it
  40.         System.out.println("Qtip: \t\ttrue: " + qNotFollowedByU("Qtip")); // q without a u in caps
  41.         System.out.println("Qutipq: "+ qNotFollowedByU("Qutipq")); // q without a u in caps
  42.  
  43.  
  44.  
  45.         System.out.println("\n******TESTING ARRAY REVERSER");
  46.         int[] array1 = {1, 2, 3, 4, 5};
  47.         System.out.println("Before reverse: " + Arrays.toString(array1));
  48.         System.out.println("After reverse should be");
  49.         printReverse(array1);
  50.         reverseArray(array1);
  51.         System.out.println(Arrays.toString(array1));
  52.         System.out.println("After reversing back should be");
  53.         printReverse(array1);
  54.         reverseArray(array1);
  55.         System.out.println(Arrays.toString(array1));
  56.  
  57.         int[] array2 = {1, 2, 3, 4, 5, 6};
  58.         System.out.println("\nBefore reverse: " + Arrays.toString(array2));
  59.         System.out.println("After reverse should be");
  60.         printReverse(array2);
  61.         reverseArray(array2);
  62.         System.out.println(Arrays.toString(array2));
  63.         System.out.println("After reversing back should be");
  64.         printReverse(array2);
  65.         reverseArray(array2);
  66.         System.out.println(Arrays.toString(array2));
  67.  
  68.         int[] array3 = {1, 2};
  69.         System.out.println("\nBefore reverse: " + Arrays.toString(array3));
  70.         System.out.println("After reverse should be");
  71.         printReverse(array3);
  72.         reverseArray(array3);
  73.         System.out.println(Arrays.toString(array3));
  74.         int[] array4 = {1};
  75.         System.out.println("\nBefore reverse: " + Arrays.toString(array4));
  76.         System.out.println("After reverse should be");
  77.         printReverse(array4);
  78.         reverseArray(array4);
  79.         System.out.println(Arrays.toString(array4));
  80.         int[] array5 = {};
  81.         System.out.println("\nBefore reverse: " + Arrays.toString(array5));
  82.         System.out.println("After reverse should be");
  83.         printReverse(array5);
  84.         reverseArray(array5);
  85.         System.out.println(Arrays.toString(array5));
  86.  
  87.         System.out.println("\n******TESTING EXTRA CREDIT COUNT POSITIVES");
  88.         BagInterface <Integer> numbersBag = new LinkedBag <Integer>();
  89.         numbersBag.add(2);
  90.         numbersBag.add(-1);
  91.         numbersBag.add(3);
  92.         numbersBag.add(5);
  93.         numbersBag.add(-2);
  94.         numbersBag.add(-4);
  95.         numbersBag.add(1);
  96.         System.out.println("The bag contains[1, -4, -2, 5, 3, -1, 2] \n\t\t" + Arrays.toString(numbersBag.toArray()));
  97.         System.out.println("There are 4 positives: " + countPositives(numbersBag));
  98.         System.out.println("The bag still contains  [1, -4, -2, 5, 3, -1, 2] \n\t\t\t" + Arrays.toString(numbersBag.toArray()));
  99.         numbersBag.clear();
  100.         numbersBag.add(-2);
  101.         numbersBag.add(-4);
  102.         numbersBag.add(-3);
  103.         System.out.println("The bag contains[-3, -4, -2] \n\t\t" + Arrays.toString(numbersBag.toArray()));
  104.         System.out.println("There are 0 positives: " + countPositives(numbersBag));
  105.         System.out.println("The bag still contains  [-3, -4, -2] \n\t\t\t" + Arrays.toString(numbersBag.toArray()));
  106.         numbersBag.clear();
  107.         numbersBag.add(2);
  108.         numbersBag.add(4);
  109.         numbersBag.add(3);
  110.         System.out.println("The bag contains[3, 4, 2] \n\t\t" + Arrays.toString(numbersBag.toArray()));
  111.         System.out.println("There are 3 positives: " + countPositives(numbersBag));
  112.         System.out.println("The bag still contains  [3, 4, 2] \n\t\t\t" + Arrays.toString(numbersBag.toArray()));
  113.         numbersBag.clear();
  114.         System.out.println("The bag contains[] \n\t\t" + Arrays.toString(numbersBag.toArray()));
  115.         System.out.println("There are 0 positives: " + countPositives(numbersBag));
  116.         System.out.println("The bag still contains  [] \n\t\t\t" + Arrays.toString(numbersBag.toArray()));
  117.         numbersBag.add(3);
  118.         System.out.println("The bag contains[3] \n\t\t" + Arrays.toString(numbersBag.toArray()));
  119.         System.out.println("There are 1 positives: " + countPositives(numbersBag));
  120.         System.out.println("The bag still contains  [3] \n\t\t\t" + Arrays.toString(numbersBag.toArray()));
  121.         numbersBag.clear();
  122.         numbersBag.add(-3);
  123.         System.out.println("The bag contains[-3] \n\t\t" + Arrays.toString(numbersBag.toArray()));
  124.         System.out.println("There are 0 positives: " + countPositives(numbersBag));
  125.         System.out.println("The bag still contains  [-3] \n\t\t\t" + Arrays.toString(numbersBag.toArray()));
  126.  
  127.  
  128.     }
  129.  
  130.     // First Solution
  131.     public static boolean qNotFollowedByU(String word) {
  132.         word = word.toLowerCase();
  133.         if (word.length() == 0) return false;
  134.         return qNotFollowedByUHelper(word, word.charAt(0), 1);
  135.     }
  136.  
  137.     private static boolean qNotFollowedByUHelper(String word, char previous, int startIndex) {
  138.         if (startIndex < word.length()) {
  139.             char newChar = word.charAt(startIndex);
  140.             if (previous == 'q' && newChar != 'u') {
  141.                 return true;
  142.             } else {
  143.                 return qNotFollowedByUHelper(word, newChar, startIndex+1);
  144.             }
  145.         } else if (previous == 'q') {
  146.             return true;
  147.         } else {
  148.             return false;
  149.         }
  150.     }
  151.    
  152.     public static void reverseArray(int[] array) {
  153.         //reverseArrayHelper(array,0);
  154.         reverseArrayHelper(array,0, array.length-1);
  155.     }
  156.     private static void reverseArrayHelper(int[] array, int start, int end) {
  157.         if (start == end || start > end) {
  158.             return;
  159.         } else {//start < end) {
  160.             int tempVal = array[start];
  161.             array[start] = array[end];
  162.             array[end] = tempVal;
  163.             reverseArrayHelper(array,start+1, end-1);
  164.         }
  165.     }
  166.     /*
  167.     private static void reverseArrayHelper(int[] array, int start, int end) {
  168.         if (array.length > 0) {
  169.             if (start == end) {
  170.                 System.out.println(array[start]);
  171.             } else {
  172.                 System.out.print(array[end] + ", ");
  173.                 reverseArrayHelper(array, start, end - 1);
  174.             }
  175.         } else {
  176.             return;
  177.  
  178.         }
  179.     }
  180.     */
  181.  
  182.     // a printing method for testing purposes
  183.     public static void printReverse(int[] array) {
  184.         System.out.print("[");
  185.         for (int i = array.length - 1; i > 0; i--) {
  186.             System.out.print(array[i] + ", ");
  187.         }
  188.         if (array.length > 0) {
  189.             System.out.print(array[0]);
  190.         }
  191.         System.out.println("]");
  192.     }
  193.  
  194.     public static int countPositives(BagInterface<Integer> bag) {
  195.         int count = 0;
  196.         Integer item = bag.remove();
  197.         if (item != null) {
  198.             boolean check = item > 0;
  199.             count = check ? 1 + countPositives(bag) : countPositives(bag);
  200.             bag.add(item);
  201.             return count;
  202.         } else {
  203.             return count;
  204.         }
  205.  
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement