Loading...
More Java Posts
public class Factorial{public static void main(String[] args){ final int NUM_FACTS = 100;for(int i = 0; i < NUM_FACTS; i++)System.out.println( i + "! is " + factorial(i));}public static int factorial(int n){ int result = 1;for(int i = 2; i <= n; i++)result *= i;return result;}}
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);elsewordCount.put(word, wordCount.get(word) + 1);}// show resultsfor(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.");}}}
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;}}
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 childif(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));}}
//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}}}}}}}
//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");}}}