Skip to content

Commit

Permalink
deploy: 0407b48
Browse files Browse the repository at this point in the history
  • Loading branch information
kiannp44 committed Apr 27, 2023
1 parent 5cca410 commit 2d6939e
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 135 deletions.
10 changes: 5 additions & 5 deletions assets/css/style.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/search-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

,"post1": {
"title": "Code Lingo Hacks",
"content": "public void drawLine(int n) { if (n != 0) { for (int i = 1; i <= n; i++) { System.out.print("*"); } System.out.println(); drawLine(n - 1); } } drawLine(10); . ********** ********* ******** ******* ****** ***** **** *** ** * . public class Country implements Comparable<Country> { private int size; private int population; public Country(int size, int population) { this.size = size; this.population = population; } public int getSize() { return size; } public int getPopulation() { return population; } @Override public int compareTo(Country o) { return this.size - o.size; } @Override public boolean equals(Object obj) { if (obj instanceof Country) { Country other = (Country) obj; return this.size == other.size && this.population == other.population; } return false; } @Override public String toString() { return "Country{" + "size=" + size + ", population=" + population + '}'; } } . public class SelectionSortBackwards { public void sort(ArrayList<Country> arr) { for (int i = 0; i < arr.size()-1; i++) { int max_idx = i; for (int j = i+1; j < arr.size(); j++) { if (arr.get(j).compareTo(arr.get(max_idx)) > 0) max_idx = j; } Country temp = arr.get(max_idx); arr.set(max_idx, arr.get(i)); arr.set(i, temp); } // for (int i = arr.length-1; i > 0; i--) { // int max_idx = i; // for (int j = i-1; j >= 0; j--) { // if (arr[j].compareTo(arr[max_idx]) > 0) // max_idx = j; // } // Country temp = arr[max_idx]; // arr[max_idx] = arr[i]; // arr[i] = temp; // } } public static void main() { SelectionSortBackwards ob = new SelectionSortBackwards(); ArrayList<Country> arr = new ArrayList<Country>(); arr.add(new Country(1, 1)); arr.add(new Country(7, 7)); arr.add(new Country(23, 23)); arr.add(new Country(6, 6)); arr.add(new Country(4, 4)); arr.add(new Country(6, 6)); arr.add(new Country(1, 1)); ob.sort(arr); for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i) + " "); } } } SelectionSortBackwards.main(); . Country{size=23, population=23} Country{size=7, population=7} Country{size=6, population=6} Country{size=6, population=6} Country{size=4, population=4} Country{size=1, population=1} Country{size=1, population=1} . // create a class with a heapSort() and heapify() method public class HeapSort { public void sort(ArrayList<Country> arr) { int n = arr.size(); // Build heap (rearrange array) for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); // One by one extract an element from heap for (int i=n-1; i>=0; i--) { // Move current root to end Country temp = arr.get(0); arr.set(0, arr.get(i)); arr.set(i, temp); // call max heapify on the reduced heap heapify(arr, i, 0); } } void heapify(ArrayList<Country> arr, int n, int i) { 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 && arr.get(l).compareTo(arr.get(largest)) > 0) largest = l; // If right child is larger than largest so far if (r < n && arr.get(r).compareTo(arr.get(largest)) > 0) largest = r; // If largest is not root if (largest != i) { Country swap = arr.get(i); arr.set(i, arr.get(largest)); arr.set(largest, swap); // Recursively heapify the affected sub-tree heapify(arr, n, largest); } } public static void main() { HeapSort ob = new HeapSort(); ArrayList<Country> arr = new ArrayList<Country>(); arr.add(new Country(1, 1)); arr.add(new Country(7, 7)); arr.add(new Country(23, 23)); arr.add(new Country(6, 6)); arr.add(new Country(4, 4)); arr.add(new Country(6, 6)); arr.add(new Country(1, 1)); ob.sort(arr); for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i) + " "); } } } HeapSort.main() . Country{size=1, population=1} Country{size=1, population=1} Country{size=4, population=4} Country{size=6, population=6} Country{size=6, population=6} Country{size=7, population=7} Country{size=23, population=23} .",
"content": "public void drawLine(int stars) { for (int i = 1; i <= stars; i++) { System.out.print("*"); } if(stars>0){ System.out.println(); drawLine(stars - 1); } } drawLine(10); . ********** ********* ******** ******* ****** ***** **** *** ** * . public class Country implements Comparable<Country> { private int size; private int population; public Country(int size, int population) { this.size = size; this.population = population; } public int getSize() { return size; } public int getPopulation() { return population; } @Override public int compareTo(Country o) { return this.size - o.size; } @Override public boolean equals(Object obj) { if (obj instanceof Country) { Country other = (Country) obj; return this.size == other.size && this.population == other.population; } return false; } @Override public String toString() { return "Country{" + "size=" + size + ", population=" + population + '}'; } } . Write a insertion or selection sort program that sorts an ArrayList in decreasing order so that the largest country is at the beginning of the array (Create your own Country class with size). Use a Comparator . public class Country { int size; String countryName; public Country(String countryName, int size){ this.size = size; this.countryName = countryName; } public int getSize(){ return this.size; } // compare country population sizes public int compareCountry(Country country) { if (this.getSize() < country.getSize()) { return 0; } else if (this.getSize() > country.getSize()){ return 1; } else{ return -1; } } public String toString() { String string = "country: " + this.countryName + " Size: " + this.size; 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("Argentina", 2780400 ), new Country("India", 3287260), new Country("Australia", 7741220 ), new Country("Brazil", 8515770 ), new Country("US", 9525067)}; SelectionSort.sort(arr); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } } SelectionSort.main(null); . country: US Size: 9525067 country: Brazil Size: 8515770 country: Australia Size: 7741220 country: India Size: 3287260 country: Argentina Size: 2780400 . Test if two arraylists contain the same elements in reverse order . public static void main(String []args){ ArrayList<String> list1=new ArrayList<String>(); list1.add("Ronaldo");//Adding object in arraylist list1.add("Messi"); list1.add("Neymar"); System.out.println(list1); ArrayList<String> list2=new ArrayList<String>(); list2.add("Kian"); list2.add("Brian"); list2.add("Arman"); System.out.println(list2); if(list1.equals(list2)) { System.out.println("is equal"); } else{ System.out.println("not equal"); } } main(null); . [Ronaldo, Messi, Neymar] [Kian, Brian, Arman] not equal . Remove every other element from an arraylist . public static void main(String []args){ ArrayList<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); list.add(6); ArrayList<Integer> remove = new ArrayList<Integer>(); int count = 2; for (int i = 0; i < list.size(); i++) { if (!(i % count == 0)) { remove.add(list.get(i)); } } list.removeAll(remove); System.out.print(list); } main(null); . [1, 3, 5] . Bonus Hack . import java.util.ArrayList; import java.util.Comparator; 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; } public String toString() { return name + " (" + size + ")"; } public static void heapSort(ArrayList<Country> countries, Comparator<Country> comparator) { int n = countries.size(); // Build max heap for (int i = n / 2 - 1; i >= 0; i--) heapify(countries, n, i, comparator); // Extract elements from heap in decreasing order 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); // Heapify reduced heap heapify(countries, i, 0, comparator); } } public static void heapify(ArrayList<Country> countries, int n, int i, Comparator<Country> comparator) { int largest = i; // Initialize largest as root int left = 2 * i + 1; int right = 2 * i + 2; // If left child is larger than root if (left < n && comparator.compare(countries.get(left), countries.get(largest)) > 0) largest = left; // If right child is larger than largest so far if (right < n && comparator.compare(countries.get(right), countries.get(largest)) > 0) largest = right; // 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("USA", 9833520)); countries.add(new Country("China", 9596961)); countries.add(new Country("India", 3287263)); countries.add(new Country("Brazil", 8515767)); countries.add(new Country("Australia", 7692024)); System.out.println("Before sorting:"); System.out.println(countries); heapSort(countries, Comparator.comparing(Country::getSize).reversed()); System.out.println("After sorting:"); System.out.println(countries); } } .",
"url": "https://kiannp44.github.io/KianFastPages/collegeboard/2023/04/26/CodeLingo.html",
"relUrl": "/collegeboard/2023/04/26/CodeLingo.html",
"date": " • Apr 26, 2023"
Expand Down
Loading

0 comments on commit 2d6939e

Please sign in to comment.