Mobile and tablet view is currently under construction.
To see my website, load it on a laptop or desktop computer
with a wide enough screen
Human-Computer Interaction & Information Visualization
public class Greedy { public static void main(String[] args) { int[] stock = new int[]{30, 25, 27, 32, 31, 29}; highestProfit(stock); } public static void highestProfit(int[] stock) { if (stock == null || stock.length == 0 || stock.length == 1) { return; } int profit = stock[1] - stock[0]; int min = stock[0]; for (int i = 1; i < stock.length; i++) { int tempProfit = stock[i] - min; profit = Math.max(profit, tempProfit); min = Math.min(min, stock[i]); } System.out.println("Highest profit would be $" + profit); } }
import java.util.Scanner; public class Fibonacci { static long[] memoization; public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); memoization = new long[num + 1]; long result = findFibonacci(num); System.out.print("Fibonacci for number: "); System.out.println(num + " = " + result); } public static long findFibonacci(int num) { if (num <= 0) { return 0; } else { long fib = memoization[num]; if (fib == 0) { if (num == 1) { memoization[num] = 1; } else { memoization[num] = findFibonacci(num - 1) + findFibonacci(num - 2); } fib = memoization[num]; } return fib; } } }
import java.util.Scanner; public class Palindrome { public static void main(String[] args) { Scanner in = new Scanner(System.in).useDelimiter("\\n"); String test = in.next(); System.out.print("Is \"" + test + "\" a palindrome? "); System.out.println(isPalindrome(test)); } public static boolean isPalindrome(String str) { if (str == null || str.length() == 0) { return false; } else if (str.length() == 1) { return true; } else if (str.charAt(0) == str.charAt(str.length() - 1)) { str = str.substring(1, str.length() - 1); if (str.length() > 0) { return isPalindrome(str); } return true; } return false; } }
public class Factorial { public static void main(String[] args) { int num = 6; long iter = factorialIter(num); long recurse = factorialRecurse(num); System.out.println(iter + " " + recurse); } public static long factorialIter(long num) { if (num == 0 || num == 1) { return 1; } long factorial = 1; for (int i = 2; i <= num; i++) { factorial *= i; } return factorial; } public static long factorialRecurse(long num) { if (num == 0 || num == 1) { return 1; } return num * factorialRecurse(num - 1); } }
public class Swap { public static void main(String[] args) { int a = 5, b = 10; System.out.println("a = " + a + " : b = " + b); swapFirst(a, b); swapSecond(a, b); swapThird(a, b); } public static void swapFirst(int a, int b) { a = a + b; // 5 + 10 = 15 b = a - b; // 15 - 10 = 5 a = a - b; // 15 - 5 = 10 System.out.println("a = " + a + " : b = " + b); } public static void swapSecond(int a, int b) { a = a * b; // 5 * 10 = 50 b = a / b; // 50 / 10 = 5 a = a / b; // 50 / 5 = 10 System.out.println("a = " + a + " : b = " + b); } public static void swapThird(int a, int b) { a = a ^ b; // 00000101 ^ 00001010 = 00001111 = 15 b = a ^ b; // 00001111 ^ 00001010 = 00000101 = 5 a = a ^ b; // 00001111 ^ 00000101 = 00001010 = 10 System.out.println("a = " + a + " : b = " + b); } }
public class Permutation { public static void main(String[] args) { permute("apple"); } public static void permute(String str) { permute("", str); } public static void permute(String prefix, String str) { int length = str.length(); if (length == 0) { System.out.println(prefix); } else { for (int i = 0; i < length; i++) { String newPrefix = prefix + str.charAt(i); String newStr = str.substring(0, i) + str.substring(i + 1); permute(newPrefix, newStr); } } } }
import java.util.Arrays; public class BinarySearch { public static void main(String[] args) { int[] nums = new int[]{5, 9, 2, 4, 15, 22, 3, 1}; int searchFor = 9; Arrays.sort(nums); int index1 = Arrays.binarySearch(nums, searchFor); if (index1 > -1) { System.out.println("Found " + searchFor + " at index " + index1); } int index2 = binarySearch(nums, searchFor); if (index2 > -1) { System.out.println("Found " + searchFor + " at index " + index2); } } public static int binarySearch(int[] array, int target) { return binarySearch(array, target, 0, array.length); } // Start index is inclusive // End index is exclusive public static int binarySearch( int[] array, int target, int start, int end) { if (start < 0 || end > array.length || start >= end || array[start] > target || array[end - 1] < target) { return -1; } while (true) { int middle = (int) Math.floor((start + end) / 2.0F); if (array[middle] == target) { return middle; } else if (array[middle] > target) { end = middle; } else if (array[middle] < target) { start = middle + 1; } if (start >= end) { return -1; } } } }
import java.util.List; import java.util.ArrayList; public class ReverseWords { public static void main(String[] args) { String sentence = "This is a sentence"; reverseWithSplit(sentence); reverseWithoutSplit(sentence); } public static void reverseWithSplit(String sentence) { String[] words = sentence.split("\\s"); StringBuilder sBuilder = new StringBuilder(sentence.length()); for (int i = words.length - 1; i >= 0; i--) { sBuilder.append(words[i]); if (i > 0) { sBuilder.append(" "); } } System.out.println(sBuilder.toString()); } public static void reverseWithoutSplit(String sentence) { StringBuilder sBuilder = new StringBuilder(sentence.length()); List<String> wordsArray = new ArrayList<String>(); int prevSpace = 0; for (int i = 0; i < sentence.length(); i++) { String charString = sentence.substring(i, i + 1); if (charString.matches("\\s")) { wordsArray.add(sentence.substring(prevSpace, i)); prevSpace = i + 1; } else if (i == sentence.length() - 1) { wordsArray.add(sentence.substring(prevSpace, i + 1)); } } for (int i = wordsArray.size() - 1; i >= 0; i--) { sBuilder.append(wordsArray.get(i)); if (i > 0) { sBuilder.append(" "); } } System.out.println(sBuilder.toString()); } }
import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Iterator; public class Duplicates { public static void main(String[] args) { Integer[] array = new Integer[]{1, 6, 3, 4, 2, 4, 5}; int length = array.length; Arrays.sort(array); for (int i = 0; i < length - 1; i++) { if (array[i] == array[i + 1]) { System.out.println("Duplicate found"); } } List<Integer> list = Arrays.asList(array); HashSet<Integer> set = new HashSet<Integer>(list); if (set.size() != length) { System.out.println("Duplicate found"); } HashSet<Integer> set2 = new HashSet<Integer>(length); for (Integer num : array) { if (!set2.add(num)) { System.out.println("Duplicate found"); } } } }
public void reverseList(LinkedList list) { if (list.head == null) { return; } else { Node cur = list.head; Node newHead = null; while (true) { if (cur == null) { break; } Node temp = newHead; newHead = cur; cur = cur.next; newHead.next = temp; } list.tail = list.head; list.head = newHead; } }
public class ExtractBits { public static void main(String[] args) { int num = 0b11101000; int i = 2; int j = 6; int n = j - i; // = 6 - 2 = 4 int mask = (1 << n) - 1; // = 10000 - 1 = 01111 System.out.println(Integer.toBinaryString(num)); num = num >>> i; // = 11101000 >>> 2 = 111010 num = num & mask; // = 111010 & 01111 = 1010 System.out.println(Integer.toBinaryString(num)); } }
import java.util.Scanner; public class IsolateBit { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); System.out.println(Integer.toBinaryString(num)); num = num & ~(num - 1); System.out.println(Integer.toBinaryString(num)); } }
import java.util.Scanner; public class UnsetBit { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); System.out.println(Integer.toBinaryString(num)); num = num & (num - 1); System.out.println(Integer.toBinaryString(num)); } }