Loading...
More Java Posts
//sample code to write 100 random ints to a file, 1 per lineimport 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");}}}
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);}
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}}
public class UserService {private UserRepository userRepository;public UserService(UserRepository userRepository) {this.userRepository = userRepository;}public User getUserById(long id) {try {return userRepository.getUserById(id);} catch (Exception e) {// Log the exception hereSystem.out.println("An error occurred while fetching the user: " + e.getMessage());return null;}}}public class UserRepository {public User getUserById(long id) throws Exception {// Database query to fetch the userif (id < 0) {throw new Exception("Invalid user id");}// Return the user objectreturn new User(id, "John Doe");}}public class User {private long id;private String name;public User(long id, String name) {this.id = id;this.name = name;}public long getId() {return id;}public String getName() {return name;}}
public class Daedalus extends Athenian{private Story story;public ArrayList<Skill> skills = new ArrayList<Skill>();public ArrayList<Athenian> children = new ArrayList<Athenian>();public Daedalus(Story story, ArrayList<Skill> inherentSkills){if(story != null)System.out.println("ERROR No one is created with a story.");skills.add(inherentSkills);}public Momento advanceStory(Scene s){System.err.println("ERROR Don't know how to proceed...");}}//██╗ ░█████╗░███╗░░░███╗ ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗//██║ ██╔══██╗████╗░████║ ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝//██║ ███████║██╔████╔██║ ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░//██║ ██╔══██║██║╚██╔╝██║ ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗//██║ ██║░░██║██║░╚═╝░██║ ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝//╚═╝ ╚═╝░░╚═╝╚═╝░░░░░╚═╝ ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░
//Leif Messinger//Solves wordle//Needs a list of wordle words, newline separated and 5 characters eachimport 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 wordsfinal 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 characterfor(int possibleWord = possibleWords.size() - 1; possibleWord >= 0; --possibleWord){ //Has to be backwards for removing entries, also has to be int not unsignedif(possibleWords.get(possibleWord).charAt(i) != chosenWord.charAt(i)){possibleWords.remove(possibleWord);}}}}System.out.println("Is it misplaced?");//Stores a count of every letter misplacedshort[] misplacedCounts = new short[('z'-'a')+1]; //Praise be the garbage collectorfor(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 unsignedint 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 charactersif(count < misplacedCounts[(chosenWord.charAt(i) - 'a') - 1]){possibleWords.remove(possibleWord);}}//Also filter out words where that letter doesn't belong therefor(int possibleWord = possibleWords.size() - 1; possibleWord >= 0; --possibleWord){ //Has to be backwards for removing entries, also has to be int not unsignedif(possibleWords.get(possibleWord).charAt(i) == chosenWord.charAt(i)){possibleWords.remove(possibleWord);continue; //Breaks the current word and moves on in the loop}}}}}}}