Skip to main content

Heap sort

0 likes • Nov 19, 2022 • 0 views
Java
Loading...

More Java Posts

Quine-McCluskey Tabular Method

CHS
0 likes • Feb 3, 2021 • 0 views
Java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] variables = setVariables(in);
int[] minTerms = setMinTerms(in);
String[] binaryMinTerms = decimalToBinary(variables, minTerms);
ArrayList<Group> groups = computeSuccessiveGroups(minTerms, binaryMinTerms);
displayInput(variables, minTerms);
printPIs(groups, variables);
}
private static char[] setVariables(Scanner in) {
System.out.print("Enter the number of variables: ");
int numVariables = in.nextInt();
char[] variables = new char[numVariables];
System.out.print("Enter each variable separated by spaces: ");
for(int i = 0; i < numVariables; i++) {
variables[i] = in.next().charAt(0);
}
return variables;
}
private static int[] setMinTerms(Scanner in) {
System.out.print("Enter the number of min terms: ");
int numMinTerms = in.nextInt();
int[] minTerms = new int[numMinTerms];
System.out.print("Enter each min term separated by spaces: ");
for(int i = 0; i < numMinTerms; i++) {
minTerms[i] = in.nextInt();
}
return minTerms;
}
private static String[] decimalToBinary(char[] variables, int[] minTerms) {
String[] binaryMinTerms = new String[minTerms.length];
for(int i = 0; i < minTerms.length; i++) {
binaryMinTerms[i] = Integer.toBinaryString(minTerms[i]);
if(binaryMinTerms[i].length() < variables.length) {
String zeros = "";
int len = binaryMinTerms[i].length();
while(len < variables.length) {
zeros = zeros.concat("0");
len++;
}
binaryMinTerms[i] = zeros + binaryMinTerms[i];
}
}
return binaryMinTerms;
}
private static void displayInput(char[] variables, int[] minTerms) {
System.out.print("Function: f(");
for(int i = 0; i < variables.length; i++) {
if(i == variables.length-1)
System.out.print(variables[i]);
else
System.out.print(variables[i] + ", ");
}
System.out.print(") = SIGMAm(");
for(int i = 0; i < minTerms.length; i++) {
if(i == minTerms.length-1)
System.out.print(minTerms[i]);
else
System.out.print(minTerms[i] + ", ");
}
System.out.println(")");
}
private static ArrayList<Group> computeSuccessiveGroups(int[] minTerms, String[] binaryMinTerms) {
ArrayList<Group> groups = new ArrayList<Group>();
for(int i = 0; i < minTerms.length; i++)
groups.add(new Group(Integer.toString(minTerms[i]), binaryMinTerms[i]));
int sizeBeforeRemoval = 0, currentSize;
while(sizeBeforeRemoval != groups.size()) {
currentSize = groups.size();
for(int i = 0; i < currentSize; i++) {
for(int j = i+1; j < currentSize; j++) {
if(differences(groups.get(i).binary, groups.get(j).binary) == 1) {
String combinedBin = combineBinary(groups.get(i).binary, groups.get(j).binary);
groups.get(i).grouped = true;
groups.get(j).grouped = true;
if(isUniqueBin(groups, combinedBin)) {
String newTuple = groups.get(i).decimal + "," + groups.get(j).decimal;
groups.add(new Group(newTuple, combinedBin));
}
else break;
}
}
}
sizeBeforeRemoval = groups.size();
Iterator<Group> iter = groups.iterator();
while(iter.hasNext()) {
Group next = iter.next();
if(next.grouped)
iter.remove();
}
}
return groups;
}
private static int differences(String a, String b) {
int diff = 0;
for(int i = 0; i < a.length(); i++)
if(a.charAt(i) != b.charAt(i))
diff++;
return diff;
}
private static String combineBinary(String a, String b) {
String combined = "";
for(int i = 0; i < a.length(); i++) {
if(a.charAt(i) != b.charAt(i))
combined = combined.concat("-");
else
combined = combined.concat(Character.toString(a.charAt(i)));
}
return combined;
}
private static boolean isUniqueBin(ArrayList<Group> groups, String s) {
for(int i = 0; i < groups.size(); i++)
if(groups.get(i).binary.equals(s))
return false;
return true;
}
private static void printPIs(ArrayList<Group> groups, char[] variables) {
System.out.println("Prime Implicants: ");
for(int i = 0; i < groups.size(); i++)
System.out.println(groups.get(i).decimal + " | " + binaryToMinTerm(variables, groups.get(i).binary));
}
private static String binaryToMinTerm(char[] variables, String s) {
String minTerm = "";
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) != '-') {
if(s.charAt(i) == '0')
minTerm = minTerm.concat(Character.toString(variables[i]) + "'");
else
minTerm = minTerm.concat(Character.toString(variables[i]));
}
}
return minTerm;
}
}

Random Integers -> file

0 likes • Nov 19, 2022 • 0 views
Java
//sample code to write 100 random ints to a file, 1 per line
import java.io.PrintStream;
import java.io.IOException;
import java.io.File;
import java.util.Random;
public class WriteToFile {
public static void main(String[] args) {
try {
PrintStream writer = new PrintStream(new File("randInts.txt"));
Random r = new Random();
final int LIMIT = 100;
for (int i = 0; i < LIMIT; i++) {
writer.println(r.nextInt());
}
writer.close();
} catch (IOException e) {
System.out.println("An error occured while trying to write to the file");
}
}
}

WordleSolver

0 likes • Feb 1, 2022 • 1 view
Java
//Leif Messinger
//Solves wordle
//Needs a list of wordle words, newline separated and 5 characters each
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
class WordleSolver {
public static void printPointer(int position){
for(int i = 0; i < position; ++i){
System.out.print(" ");
}
System.out.print("^");
}
public static void main(String[] args) throws FileNotFoundException{
if(args.length < 1) return;
File dictionary = new File(args[0]);
Scanner sc = new Scanner(dictionary);
ArrayList<String> possibleWords = new ArrayList<String>();
while(sc.hasNextLine()){
possibleWords.add(sc.nextLine());
}
Scanner input = new Scanner(System.in);
while(possibleWords.size() > 0){
//Choose a word out of the possible words
final int randomIndex = (int)(Math.random() * possibleWords.size());
String chosenWord = possibleWords.get(randomIndex);
possibleWords.remove(randomIndex);
System.out.println("Is it correct?");
for(int i = 0; i < chosenWord.length(); ++i){
System.out.println(chosenWord);
printPointer(i);
System.out.println(" y/n?");
if(input.nextLine().toLowerCase().contains("y")){
//Filter by correct character
for(int possibleWord = possibleWords.size() - 1; possibleWord >= 0; --possibleWord){ //Has to be backwards for removing entries, also has to be int not unsigned
if(possibleWords.get(possibleWord).charAt(i) != chosenWord.charAt(i)){
possibleWords.remove(possibleWord);
}
}
}
}
System.out.println("Is it misplaced?");
//Stores a count of every letter misplaced
short[] misplacedCounts = new short[('z'-'a')+1]; //Praise be the garbage collector
for(int i = 0; i < chosenWord.length(); ++i){
System.out.println(chosenWord);
printPointer(i);
System.out.println(" y/n?");
if(input.nextLine().toLowerCase().contains("y")){
//Filter to make sure there's at least as many letters as needed
++misplacedCounts[(chosenWord.charAt(i) - 'a') - 1];
for(int possibleWord = possibleWords.size() - 1; possibleWord >= 0; --possibleWord){ //Has to be backwards for removing entries, also has to be int not unsigned
int count = 0;
for(int j = 0; j < possibleWords.get(possibleWord).length(); ++j){
if(possibleWords.get(possibleWord).charAt(j) == chosenWord.charAt(i))
++count;
}
//If there's not enough of the character that we need, then we remove that from the list of possible characters
if(count < misplacedCounts[(chosenWord.charAt(i) - 'a') - 1]){
possibleWords.remove(possibleWord);
}
}
//Also filter out words where that letter doesn't belong there
for(int possibleWord = possibleWords.size() - 1; possibleWord >= 0; --possibleWord){ //Has to be backwards for removing entries, also has to be int not unsigned
if(possibleWords.get(possibleWord).charAt(i) == chosenWord.charAt(i)){
possibleWords.remove(possibleWord);
continue; //Breaks the current word and moves on in the loop
}
}
}
}
}
}
}

6x6 Covariance Matrix Conversion from ENU to ECEF

0 likes • Jan 28, 2023 • 40 views
Java
public static void enu2ecefCov(GMatrix ecefCov, GMatrix enuCov, LLA refLLA) {
double lambda = refLLA.longitude;
double phi = refLLA.latitude;
GMatrix R = new GMatrix(6, 6);
GMatrix Rt = new GMatrix(6, 6);
GMatrix tmp1 = new GMatrix(6, 6);
GMatrix tmp2 = new GMatrix(6, 6);
R.setElement(0, 0, -Math.sin(lambda));
R.setElement(0, 1, -Math.sin(phi)*Math.cos(lambda));
R.setElement(0, 2, Math.cos(phi)*Math.cos(lambda));
R.setElement(1, 0, Math.cos(lambda));
R.setElement(1, 1, -Math.sin(phi)*Math.sin(lambda));
R.setElement(1, 2, Math.cos(phi)*Math.sin(lambda));
R.setElement(2, 0, 0);
R.setElement(2, 1, Math.cos(phi));
R.setElement(2, 2, Math.sin(phi));
R.setElement(3, 3, -Math.sin(lambda));
R.setElement(3, 4, -Math.sin(phi)*Math.cos(lambda));
R.setElement(3, 5, Math.cos(phi)*Math.cos(lambda));
R.setElement(4, 3, Math.cos(lambda));
R.setElement(4, 4, -Math.sin(phi)*Math.sin(lambda));
R.setElement(4, 5, Math.cos(phi)*Math.sin(lambda));
R.setElement(5, 3, 0);
R.setElement(5, 4, Math.cos(phi));
R.setElement(5, 5, Math.sin(phi));
Rt.transpose(R);
tmp1.mul(enuCov, R);
ecefCov.mul(Rt, tmp1);
}

longest sequence

0 likes • Oct 7, 2023 • 5 views
Java
import java.util.Arrays;
public class LongestIncreasingSubsequence {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
Arrays.fill(dp, 1); // Initialize all elements in dp array to 1, as each element itself is a valid subsequence of length 1.
int maxLength = 1; // Initialize the maximum length to 1, considering a single element as the sequence.
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1); // If nums[i] is greater than nums[j], update the longest subsequence length ending at i.
}
}
maxLength = Math.max(maxLength, dp[i]); // Update the maximum length if needed.
}
return maxLength;
}
public static void main(String[] args) {
LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};
int result = lis.lengthOfLIS(nums);
System.out.println("Length of the Longest Increasing Subsequence: " + result); // Output: 4
}
}

LeetCode #21: Merge Two Sorted Lists

0 likes • Oct 15, 2022 • 0 views
Java
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode handler = head;
while(l1 != null && l2 != null) {
if (l1.val <= l2.val) {
handler.next = l1;
l1 = l1.next;
} else {
handler.next = l2;
l2 = l2.next;
}
handler = handler.next;
}
if (l1 != null) {
handler.next = l1;
} else if (l2 != null) {
handler.next = l2;
}
return head.next;
}
}