Lesson 1 Hacks
Hack and Homework for lesson 1.
public static void printPattern(int n) {
// Base case: If n is 1 or less, print a single asterisk and return
if (n <= 1) {
System.out.println("*");
return;
}
// Recursive case: Print n asterisks and call printPattern() with n-1
for (int i = 0; i < n; i++) {
System.out.print("*");
}
System.out.println();
printPattern(n - 1);
}
printPattern(10);
public class Country {
private String name;
private int size;
public Country(String name, int size) {
this.name = name;
this.size = size;
}
public String getName() {
return name;
}
public int getSize() {
return size;
}
@Override
public String toString() {
return name + " (" + size + ")";
}
}
import java.util.Comparator;
public class CountryComparator implements Comparator<Country> {
@Override
public int compare(Country c1, Country c2) {
return c2.getSize() - c1.getSize();
}
}
import java.util.ArrayList;
public class HeapSort {
public static void heapSort(ArrayList<Country> list, Comparator<Country> comparator) {
int n = list.size();
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(list, 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 = list.get(0);
list.set(0, list.get(i));
list.set(i, temp);
// call max heapify on the reduced heap
heapify(list, i, 0, comparator);
}
}
private static void heapify(ArrayList<Country> list, int n, int i, Comparator<Country> comparator) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left = 2*i + 1
int right = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (left < n && comparator.compare(list.get(left), list.get(largest)) > 0)
largest = left;
// If right child is larger than largest so far
if (right < n && comparator.compare(list.get(right), list.get(largest)) > 0)
largest = right;
// If largest is not root
if (largest != i) {
Country swap = list.get(i);
list.set(i, list.get(largest));
list.set(largest, swap);
// Recursively heapify the affected sub-tree
heapify(list, n, largest, comparator);
}
}
}
import java.util.Random;
public class HeapSortTester {
public static void main(String[] args) {
// Generate some sample data
ArrayList<Country> countries = generateSampleData(10);
System.out.println("Before sorting:");
printCountries(countries);
// Sort the list using Heap Sort with CountryComparator
Comparator<Country> comparator = new CountryComparator();
HeapSort.heapSort(countries, comparator);
System.out.println("After sorting:");
printCountries(countries);
}
private static ArrayList<Country> generateSampleData(int n) {
ArrayList<Country> countries = new ArrayList<>();
Random random = new Random();
String[] names = {"USA", "China", "Russia", "Australia", "Brazil", "Canada", "India", "Japan", "Mexico", "Germany"};
for (int i = 0; i < n; i++) {
int size = random.nextInt(1000) + 1;
int index = random.nextInt(names.length);
Country country = new Country(names[index], size);
countries.add(country);
}
return countries;
}
private static void printCountries(ArrayList<Country> countries) {
for (Country country : countries) {
System.out.println(country);
}
System.out.println();
}
}
HeapSortTester.main(null);
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<Integer> originalList = new ArrayList<>();
Collections.addAll(originalList, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println("Original list: " + originalList);
ArrayList<Integer> modifiedList = new ArrayList<>(originalList);
for (int i = modifiedList.size() - 1; i >= 0; i--) {
if (i % 2 == 0) {
modifiedList.remove(i);
}
}
System.out.println("Modified list: " + modifiedList);
ArrayList<Integer> reversedList = new ArrayList<>(modifiedList);
Collections.reverse(reversedList);
System.out.println("Reversed list: " + reversedList);
boolean sameElements = originalList.equals(reversedList);
System.out.println("Do the original list and the reversed modified list contain the same elements? " + sameElements);
}
}
ArrayListDemo.main(null);