Skip to main content

Puzzle 1

Feb 6, 2021Daedalus
Loading...

More Java Posts

Me

Feb 6, 2021Daedalus

0 likes • 0 views

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...");
}
}
//██╗  ░█████╗░███╗░░░███╗  ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗
//██║  ██╔══██╗████╗░████║  ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝
//██║  ███████║██╔████╔██║  ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░
//██║  ██╔══██║██║╚██╔╝██║  ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗
//██║  ██║░░██║██║░╚═╝░██║  ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝
//╚═╝  ╚═╝░░╚═╝╚═╝░░░░░╚═╝  ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░

merge sort

Nov 18, 2022AustinLeath

0 likes • 0 views

import static java.lang.System.*;
import java.util.Arrays;
public class MergeSort{
private static int passCount;
public static void mergeSort(int[] list)
{
passCount=0;
mergeSort(list, 0, list.length);
}
private static void mergeSort(int[] list, int front, int back) //O( Log N )
{
int mid = (front+back)/2;
if(mid==front) return;
mergeSort(list, front, mid);
mergeSort(list, mid, back);
merge(list, front, back);
}
private static void merge(int[] list, int front, int back) //O(N)
{
int dif = back-front;
int[] temp = new int[dif];
int beg = front, mid = (front+back)/2;
int saveMid = mid;
int spot = 0;
while(beg < saveMid && mid < back) {
if(list[beg] < list[mid]) {
temp[spot++] = list[beg++];
} else {
temp[spot++] = list[mid++];
}
}
while(beg < saveMid)
temp[spot++] = list[beg++];
while(mid < back)
temp[spot++] = list[mid++];
for(int i = 0; i < back-front; i++) {
list[front+i] = temp[i];
}
System.out.println("pass " + passCount++ + " " + Arrays.toString(list) + "\n");
}
public static void main(String args[])
{
mergeSort(new Comparable[]{ 9, 5, 3, 2 });
System.out.println("\n");
mergeSort(new Comparable[]{ 19, 52, 3, 2, 7, 21 });
System.out.println("\n");
mergeSort(new Comparable[]{ 68, 66, 11, 2, 42, 31});
System.out.println("\n");
}
}

Word Counter

Nov 19, 2022CodeCatch

0 likes • 0 views

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class SimpleWordCounter {
public static void main(String[] args) {
try {
File f = new File("ciaFactBook2008.txt");
Scanner sc;
sc = new Scanner(f);
// sc.useDelimiter("[^a-zA-Z']+");
Map<String, Integer> wordCount = new TreeMap<String, Integer>();
while(sc.hasNext()) {
String word = sc.next();
if(!wordCount.containsKey(word))
wordCount.put(word, 1);
else
wordCount.put(word, wordCount.get(word) + 1);
}
// show results
for(String word : wordCount.keySet())
System.out.println(word + " " + wordCount.get(word));
System.out.println(wordCount.size());
}
catch(IOException e) {
System.out.println("Unable to read from file.");
}
}
}

Heap sort

Nov 19, 2022CodeCatch

0 likes • 0 views

import java.util.*;
public class HeapSortMain {
public static void buildheap(int []arr) {
/*
* As last non leaf node will be at (arr.length-1)/2
* so we will start from this location for heapifying the elements
* */
for(int i=(arr.length-1)/2; i>=0; i--){
heapify(arr,i,arr.length-1);
}
}
public static void heapify(int[] arr, int i,int size) {
int left = 2*i+1;
int right = 2*i+2;
int max;
if(left <= size && arr[left] > arr[i]){
max=left;
} else {
max=i;
}
if(right <= size && arr[right] > arr[max]) {
max=right;
}
// If max is not current node, exchange it with max of left and right child
if(max!=i) {
exchange(arr,i, max);
heapify(arr, max,size);
}
}
public static void exchange(int[] arr,int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public static int[] heapSort(int[] arr) {
buildheap(arr);
int sizeOfHeap=arr.length-1;
for(int i=sizeOfHeap; i>0; i--) {
exchange(arr,0, i);
sizeOfHeap=sizeOfHeap-1;
heapify(arr, 0,sizeOfHeap);
}
return arr;
}
public static void main(String[] args) {
int[] arr={1,10,16,19,3,5};
System.out.println("Before Heap Sort : ");
System.out.println(Arrays.toString(arr));
arr=heapSort(arr);
System.out.println("=====================");
System.out.println("After Heap Sort : ");
System.out.println(Arrays.toString(arr));
}
}

Send Email from Java

Nov 19, 2022CodeCatch

0 likes • 3 views

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
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);
}