diff --git a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Combinations.java b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Combinations.java new file mode 100644 index 0000000..95be97a --- /dev/null +++ b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/Combinations.java @@ -0,0 +1,145 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.mp5_calebfontenot; + +/** + * + * @author caleb + */ + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +class Combinations +{ + + private static void findCombinations(String[] A, int i, int k, + Set> subarrays, + List out) + { + if (A.length == 0 || k > A.length) + { + return; + } + + // base case: combination size is `k` + if (k == 0) + { + subarrays.add(new ArrayList<>(out)); + return; + } + + // start from the next index till the last index + for (int j = i; j < A.length; j++) + { + // add current element `A[j]` to the solution and recur for next index + // `j+1` with one less element `k-1` + out.add(A[j]); + findCombinations(A, j + 1, k - 1, subarrays, out); + out.remove(out.size() - 1); // backtrack + } + } + + private static Set> findCombinations(String[] A, int k) + { + Set> subarrays = new HashSet<>(); + findCombinations(A, 0, k, subarrays, new ArrayList<>()); + return subarrays; + } + + private static Set> findAllCombinations(String[] A) + { + Set> subarrays = new HashSet<>(); + for (int k = 1; k <= A.length; ++k) + { + findCombinations(A, 0, k, subarrays, new ArrayList<>()); + } + return subarrays; + } +/** Finds all distinct combinations of all sizes for elements of array. + * + * @param A the elements to find their combinations + * @return all distinct combinations of the elements, sorted by length. ascending order. + */ + public static ArrayList allCombinations(String[] A) + { + Set> set = findAllCombinations(A); + ArrayList all = new ArrayList(); + Iterator it = set.iterator(); + while (it.hasNext()) + { + List list = (List) it.next(); + String s1 = ""; + for (String s2 : list) + { + s1 += s2; + } + all.add(s1); + } + Collections.sort(all, new Comparator(){ + @Override + public int compare(String o1, String o2) + { + return o1.length() - o2.length(); + } + }); + return all; + } +/** Finds all distinct combinations of all sizes for chars of the String. + * + * @param A the characters to find their combinations. + * @return all distinct combinations of the characters sorted by length, ascending order. + */ + public static ArrayList allCombinations(String a) + { + String[] A = new String[a.length()]; + for (int i = 0; i < A.length; ++i) + { + A[i] = Character.toString(a.charAt(i)); + } + Set> set = findAllCombinations(A); + ArrayList all = new ArrayList(); + Iterator it = set.iterator(); + while (it.hasNext()) + { + List list = (List) it.next(); + String s1 = ""; + for (String s2 : list) + { + s1 += s2; + } + all.add(s1); + } + + Collections.sort(all, new Comparator(){ + @Override + public int compare(String o1, String o2) + { + return o1.length() - o2.length(); + } + }); + return all; + } + + public static void main(String[] args) + { + String[] A = + { + "1", "2", "3", "4" + }; + int k = 2; + + // process elements from left to right + System.out.println(allCombinations(A)); + System.out.println(allCombinations("1234")); + + } +} + diff --git a/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java new file mode 100644 index 0000000..b9a7490 --- /dev/null +++ b/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/ShuffleArrayList.java @@ -0,0 +1,67 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.calebfontenot.mp5_calebfontenot; + +import java.util.*; + +/** + * + * @author caleb + */ +public class ShuffleArrayList { + + public static void shuffle(ArrayList list) + { + ArrayList selectedIndicies = new ArrayList(); + for (int i = 0; i < list.size(); ++i) { + Random rng = new Random(); + Number random1; + do { + random1 = rng.nextInt(list.size()); + } while (selectedIndicies.contains(random1)); + selectedIndicies.add((int) random1); + System.out.println(random1 + ", " + i); + Number temp = list.get((int) random1); + list.set(i, temp); + } + } + + public static String checkForDuplicates(ArrayList list) + { + // Ensure Array does not include repeat numbers. + boolean repeatNumber = false; + for (int i = 0; i < list.size(); ++i) { + for (int j = 0; j < list.size(); ++j) { + if (i != j) { + //System.out.println("Checking " + list.get(i) + " and " + list.get(j)); + if (list.get(i) == list.get(j)) { + repeatNumber = true; + } + } + } + } + if (repeatNumber) { + return "Numbers repeat in ArrayList."; + } else { + return "Numbers do not repeat in ArrayList."; + } + } + + public static void main(String[] args) + { + final int ARRAY_SIZE = 50; + ArrayList list = new ArrayList<>(); + for (int i = 0; i < ARRAY_SIZE; ++i) { + list.add(i + 1); // Fill ArrayList with sequential numbers. + } + + System.out.println(list); + System.out.println(checkForDuplicates(list)); + shuffle(list); + System.out.println(list); + System.out.println(checkForDuplicates(list)); + } + +}