Hack #1: Recursion Hack

public void drawLine(int n) {
    for (int i = 1; i <= n; i++) { // iterates through i
        System.out.print("*"); // prints "*" n times
         
    }

    if (n > 0) { // only goes when when n is greater than 0 so that it doesn't overflow
        System.out.println();
        drawLine(n - 1);
    }
}
drawLine(10);
**********
*********
********
*******
******
*****
****
***
**
*

Hack 2: Array List Hack

  • Test if two arrays are in reverse order
public class Reverse {
    
    public boolean compareArrayLists(ArrayList<Integer> list1, ArrayList<Integer> list2){
        if (list1.size() != list2.size()) {
            return false;
        }
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i) != list2.get(list2.size()- i - 1)){
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args){

        Reverse reverse = new Reverse();

        ArrayList<Integer> list1 = new ArrayList<Integer>();

        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);

        ArrayList<Integer> list2 = new ArrayList<Integer>();

        list2.add(5);
        list2.add(4);
        list2.add(3);
        list2.add(2);
        list2.add(1);

        System.out.println(list1);
        System.out.println(list2);

        System.out.print(reverse.compareArrayLists(list1, list2));
    }

   
// for (int i = 0; i < list1.size(); i++) {
//     int j = 5;
//     list2.set(list.size() - j, i);
//     j--;
//     if (list1.get(i) == list2.get(i)) {
//         System.out.println(i);
//     }
// }
}
Reverse.main(null);
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
true

Hack #3: Array List Hack

  • Overwrite all the elements in an arraylist with the alphabet
ArrayList<Integer> list1 = new ArrayList<Integer>();

        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        list1.add(5);

System.out.println(list1);

for (int i = 1; i < list1.size(); i++){
    list1.remove(i);
}

System.out.println(list1);
[1, 2, 3, 4, 5]
[1, 3, 5]

Hack #4: Sorting Hack

  • Selection Sort
public class Country {
    public String name;
    private long population;

    // constructor
    public Country(String name, long population) {
        this.name = name;
        this.population = population;
    }

    // getPop method to be able to compare two countries
    public long getPop() {
        return this.population;
    }

    // compare country population sizes
    public int compareCountry(Country c) {
        if (this.getPop() > c.getPop()) {
            return 1;
        }
        else if (this.getPop() < c.getPop()) {
            return -1;
        }
        else {
            return 0;
        }
    }

    // toString method for printing
    public String toString() {
        String string = "Name: " + this.name + "; Population: " + this.population;
        return string;
    }
}

public class SelectionSort {
    public static void sort(Country[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            int min_idx = i;
            for (int j = i+1; j < arr.length; j++) {
                // use compare country, only set min_idx if -1 (smaller pop)
                if (arr[j].compareCountry(arr[min_idx]) == -1)
                    min_idx = j;
            }
            // assign temp country to swap
            Country temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }
    public static void main(String[] args) {
        Country[] arr = {
            new Country("United States", 331900000),
            new Country("South Africa", 1463000),
            new Country("Berlin", 5346686),
            new Country("Egypt", 1730000),
            new Country("Cuba", 1435000)
        };
        SelectionSort.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

SelectionSort.main(null);
Name: Cuba; Population: 1435000
Name: South Africa; Population: 1463000
Name: Egypt; Population: 1730000
Name: Berlin; Population: 5346686
Name: United States; Population: 331900000

Hack #5: Extra Credit Heap Sort

  • Selection Sort
import java.util.ArrayList;
import java.util.Comparator;

public class HeapSort {

    public static void sort(ArrayList<Country> countries, Comparator<Country> comparator) {
        int n = countries.size();

        // Build heap (rearrange array)
        for (int i = n / 2 - 1; i >= 0; i--)
            heapify(countries, n, i, comparator);

        // One by one extract an element from heap
        for (int i = n - 1; i >= 0; i--) {
            // Move current root to end
            Country temp = countries.get(0);
            countries.set(0, countries.get(i));
            countries.set(i, temp);

            // call max heapify on the reduced heap
            heapify(countries, i, 0, comparator);
        }
    }

    // To heapify a subtree rooted with node i which is
    // an index in the ArrayList. n is size of heap
    static void heapify(ArrayList<Country> countries, int n, int i, Comparator<Country> comparator) {
        int largest = i; // Initialize largest as root
        int l = 2 * i + 1; // left = 2*i + 1
        int r = 2 * i + 2; // right = 2*i + 2

        // If left child is larger than root
        if (l < n && comparator.compare(countries.get(l), countries.get(largest)) > 0)
            largest = l;

        // If right child is larger than largest so far
        if (r < n && comparator.compare(countries.get(r), countries.get(largest)) > 0)
            largest = r;

        // If largest is not root
        if (largest != i) {
            Country swap = countries.get(i);
            countries.set(i, countries.get(largest));
            countries.set(largest, swap);

            // Recursively heapify the affected sub-tree
            heapify(countries, n, largest, comparator);
        }
    }
    public static void main(String[] args) {
        ArrayList<Country> countries = new ArrayList<>();
        countries.add(new Country("United States", 329064917));
        countries.add(new Country("Indonesia", 270625568));
        countries.add(new Country("Brazil", 211049527));
        countries.add(new Country("Russia", 144373535));
        countries.add(new Country("Mexico", 127575529));

        Comparator<Country> comparator = Comparator.comparingLong(Country::getPopulation).reversed();
        HeapSort.sort(countries, comparator);

        for (Country country : countries) {
            System.out.println(country.getName() + " - " + country.getPopulation());
        }
    }
}

class Country {
    private String name;
    private long population;

    public Country(String name, long population) {
        this.name = name;
        this.population = population;
    }

    public String getName() {
        return name;
    }

    public long getPopulation() {
        return population;
    }
}

HeapSort.main(null);
United States - 329064917
Indonesia - 270625568
Brazil - 211049527
Russia - 144373535
Mexico - 127575529