Пример #1
0
 public static void main(String[] args) {
   String[] array = {
     "apple", "banana", "carrot", "ele", "duck", "papel", "tarroc", "cudk", "eel", "lee"
   };
   System.out.println(AssortedMethods.stringArrayToString(array));
   Arrays.sort(array, new AnagramComparator());
   System.out.println(AssortedMethods.stringArrayToString(array));
 }
Пример #2
0
  public static void main(String[] args) {

    int number = 59;
    System.out.println("Testing with number: " + number);

    // Get Bit
    System.out.println("Get Bit");
    System.out.println(AssortedMethods.toFullBinaryString(number));
    for (int i = 31; i >= 0; i--) {
      int res = getBit(number, i) ? 1 : 0;
      System.out.print(res);
    }

    // Update Bit
    System.out.println("\n\nUpdate Bit");
    int num1 = 1578; // arbitrary number
    for (int i = 31; i >= 0; i--) {
      int res = getBit(number, i) ? 1 : 0;
      num1 = updateBit(num1, i, res);
    }
    System.out.println(num1);

    // Set and Clear Bit
    System.out.println("\nSet and Clear Bit");
    int num2 = 1578; // arbitrary number
    for (int i = 31; i >= 0; i--) {
      if (getBit(number, i)) {
        num2 = setBit(num2, i);
      } else {
        num2 = clearBit(num2, i);
      }
    }
    System.out.println(num2);

    // Clear Bits MSB through i
    number = 13242352;
    System.out.println("\nClear bits MSB through 4");
    System.out.println(AssortedMethods.toFullBinaryString(number));
    int num3 = clearBitsMSBthroughI(number, 4);
    System.out.println(AssortedMethods.toFullBinaryString(num3));

    // Clear Bits i through 0
    System.out.println("\nClear bits 6 through 0");
    number = -1;
    System.out.println(AssortedMethods.toFullBinaryString(number));
    int num4 = clearBitsIthrough0(number, 2);
    System.out.println(AssortedMethods.toFullBinaryString(num4));
  }
Пример #3
0
 /** @param args */
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   String str = "abc d e f";
   char[] arr = new char[str.length() + 3 * 2 + 1];
   for (int i = 0; i < str.length(); i++) {
     arr[i] = str.charAt(i);
   }
   replaceSpace(arr, str.length());
   System.out.println("\"" + AssortedMethods.charArrayToString(arr) + "\"");
 }
Пример #4
0
  public static void main(String[] args) {
    MyQueue<Integer> my_queue = new MyQueue<Integer>();

    // Let's test our code against a "real" queue
    Queue<Integer> test_queue = new LinkedList<Integer>();

    for (int i = 0; i < 100; i++) {
      int choice = AssortedMethods.randomIntInRange(0, 10);
      if (choice <= 5) { // enqueue
        int element = AssortedMethods.randomIntInRange(1, 10);
        test_queue.add(element);
        my_queue.add(element);
        System.out.println("Enqueued " + element);
      } else if (test_queue.size() > 0) {
        int top1 = test_queue.remove();
        int top2 = my_queue.remove();
        if (top1 != top2) { // Check for error
          System.out.println("******* FAILURE - DIFFERENT TOPS: " + top1 + ", " + top2);
        }
        System.out.println("Dequeued " + top1);
      }

      if (test_queue.size() == my_queue.size()) {
        if (test_queue.size() > 0 && test_queue.peek() != my_queue.peek()) {
          System.out.println(
              "******* FAILURE - DIFFERENT TOPS: "
                  + test_queue.peek()
                  + ", "
                  + my_queue.peek()
                  + " ******");
        }
      } else {
        System.out.println("******* FAILURE - DIFFERENT SIZES ******");
      }
    }
  }
Пример #5
0
 public static void main(String[] args) {
   int[] cards = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   System.out.println(AssortedMethods.arrayToString(cards));
   shuffleArray(cards);
   System.out.println(AssortedMethods.arrayToString(cards));
 }
Пример #6
0
 public static void main(String[] args) {
   int[] nodes_flattened = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   TreeNode root = AssortedMethods.createTreeFromArray(nodes_flattened);
   ArrayList<LinkedList<TreeNode>> list = createLevelLinkedList(root);
   printResult(list);
 }