Recursion, Sorting, Arraylist Hacks

Test Prep // AP CSA

  • toc:true - badges: true
  • comments: true
  • categories: [jupyter,testprep]
  • image: images/chart-preview.png

Recursion Hack

public void drawLine(int n) {
    if (n!=0) {
        int count = 0;
        for (int i = 1; i <= n; i++) {
            System.out.print("*");
        }
        count++;
        System.out.println();
        drawLine(n-count);
}
   
  
}
drawLine(10);
**********
*********
********
*******
******
*****
****
***
**
*

Sorting Hacks

class Country implements Comparable<Country> { 
    private int size;
    private String name;

    public Country(int size, String name){
        this.size = size;
        this.name = name;
    }

    public int getSize(){
        return size;
    }

    public String getName(){
        return name;
    }

    @Override
    public int compareTo(Country c2){
        return this.size - c2.size;
    }

    @Override
    public String toString(){
        return "Name: " + name + " |" + " Size: " + size;
    }

    public static void largeToSmall(ArrayList<Country> arrList){
        for (int i = 0; i < (arrList.size() - 1); i++){
            int largest = i;
            for (int k = i+1; k < arrList.size(); k++){
                if (arrList.get(k).compareTo(arrList.get(largest)) > 0){
                    largest = k;
                }
            }
            Country temp = arrList.get(largest);
            arrList.set(largest, arrList.get(i));
            arrList.set(i, temp);
        }
    }

    public static void main(String[] args){
        ArrayList<Country> arrList = new ArrayList<Country>();
        arrList.add(new Country(331, "USA"));
        arrList.add(new Country(56, "England"));
        arrList.add(new Country(143, "Russia"));
        arrList.add(new Country(126, "Japan"));
        arrList.add(new Country(59, "Italy"));

        largeToSmall(arrList);

        for (int i = 0; i < arrList.size(); i++){
            System.out.println(arrList.get(i));
        }
    }
}

Country.main(null);
Name: USA | Size: 331
Name: Russia | Size: 143
Name: Japan | Size: 126
Name: Italy | Size: 59
Name: England | Size: 56

ArrayList Hacks

import java.util.ArrayList;

class arrayListHacks {

    public static Boolean compareReverse(ArrayList<Integer> arrList1, ArrayList<Integer> arrList2){
        int count = 0;
        int endTerm = arrList2.size() - 1;
        for (int i = 0; i < arrList1.size() -1; i++){
            if (arrList1.get(i) == arrList2.get(endTerm)){
                count++;
                endTerm--;
            }
        }
        if (count == arrList1.size()-1){
            return true;
        }
        else {
            return false;
        }
    }

    public static ArrayList<Integer> removeAlternating(ArrayList<Integer> arrList){
        //only need to increment by one because deleting an element moves every element up by one
        for (int i = 1; i < (arrList.size() - 1); i++){
            arrList.remove(i);
        }

        return arrList;
    } 


    public static void main(String[] args){
       ArrayList<Integer> arrListA = new ArrayList<Integer>(
       Arrays.asList(1, 2, 3, 4, 5));

       ArrayList<Integer> arrListB = new ArrayList<Integer>(
       Arrays.asList(5, 4, 3, 2, 1));

       ArrayList<Integer> arrListC = new ArrayList<Integer>(
       Arrays.asList(1, 3, 5, 7, 9));


       System.out.println(compareReverse(arrListA, arrListB));
       System.out.println(compareReverse(arrListA, arrListC));

       System.out.println(removeAlternating(arrListC));

    }
}

arrayListHacks.main(null);
true
false
[1, 5, 9]