Log in to view this post.

Recent Posts
Recent Comments
Archives
Categories
Meta
Log in to view this post.
Log in to view this post.
This is my solution to a programming challenge found at reddit.com/r/dailyprogrammer. This problem can be found at Roll the Dies.
Josh
public class Easy { public static void main(String[] args) { for (int i : rollDice("4d6")) { // example data System.out.print(i + " "); } } private static int[] rollDice(String s) { String diceData[] = s.split("d"); int[] result = new int[Integer.parseInt(diceData[0])]; for (int i = 0; i < result.length; i++) { result[i] = (int)(Math.random() * Integer.parseInt(diceData[1]) + 1); } return result; } }
This is my solution to a programming challenge found at reddit.com/r/dailyprogrammer. This problem can be found at longest two character substring. My solution was to step through each position of the string and place whatever character that position holds into a temporary Set. A Set will only contain unique items so if I end up with more than two I know my substring holds more than two unique characters.
Josh
import java.util.HashSet; import java.util.Set; public class DailyProgrammer129 { public static void main(String[] args) { String s = args[0]; String longest = ""; for (int i = 0; i < s.length(); i++) { Set<Character> tempSet = new HashSet<Character>(); for (int j = i; j < s.length(); j++) { tempSet.add(s.charAt(j)); if (tempSet.size() <= 2 && s.substring(i,j+1).length() > longest.length()) { longest = s.substring(i,j+1); } } } System.out.println(longest); } }
A basic implementation of a merge sort algorithm.
Running a test file of 100000 randomly ordered integers through this algorithm takes about 4.54 seconds on my system. I will be using the same test file for comparison against other sorting algorithms.
Josh
/** * A very basic Merge Sort class implemented from a pseudocode * algorithm found on Wikipedia * * This class will only sort an int[]. * * @author Joshua Weise * @see http://en.wikipedia.org/wiki/Merge_sort#Topdown_implementation */ public class MergeSort { // array of numbers private int[] data; public MergeSort(int[] data) { this.data = data; } /** * starts the recursive mergeSort method */ public void sort() { data = mergeSort(data); } /** * Recursively splits the initial data array and calls * the merge method to sort and recombine the data * @param m numbers to sort * @return new sorted array */ private int[] mergeSort(int[] m) { if (m.length <= 1) { return m; } int[] left = new int[m.length / 2]; int[] right = new int[m.length  left.length]; int middle = m.length / 2; for (int i = 0; i < middle; i++) { left[i] = m[i]; } for (int i = 0; i < right.length; i++) { right[i] = m[i + left.length]; } left = mergeSort(left); right = mergeSort(right); return merge(left, right); } /** * Merges two number arrays while also putting them in order * @param left numbers to sort and merge * @param right numbers to sort and merge * @return new sorted and merged array */ private int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int x = 0; while (left.length > 0  right.length > 0) { if (left.length > 0 && right.length > 0) { if (left[0] <= right[0]) { result[x] = left[0]; x++; left = shiftLeft(left); } else { result[x] = right[0]; x++; right = shiftLeft(right); } } else if (left.length > 0) { result[x] = left[0]; x++; left = shiftLeft(left); } else if (right.length > 0) { result[x] = right[0]; x++; right = shiftLeft(right); } } return result; } /** * removes the leftmost array item by returning a new array * with everything but that item * @param x array of numbers * @return new array of numbers */ private int[] shiftLeft(int[] x) { int[] y = new int[x.length  1]; for (int i = 1; i < x.length; i++) { y[i1] = x[i]; } return y; } // return data public int[] getData() { return data; } }
A simple example of the selection sort algorithm.
Running a test file of 100000 randomly ordered integers through this algorithm takes about 4.72 seconds on my system. I will be using the same test file for comparison against other sorting algorithms.
Josh
/** * A very basic Selection Sort class. * * This class will only sort an int[]. * * @author Joshua Weise */ public class SelectionSort { // array of numbers private int[] data; public SelectionSort(int[] data) { this.data = data; } // selection sort data public void sort() { // move right through our array for (int i = 0; i < data.length; i++) { // assign the first location as our assumed smallest number int smallest = i; // loop through the rest of array checking for smaller numbers for (int j = 1+i; j < data.length; j++) { if (data[j] < data[smallest]) { smallest = j; } } // swap number locations swap(i, smallest); } } // swaps data in place at locations a and b in the array private void swap(int a, int b) { int z = data[a]; data[a] = data[b]; data[b] = z; } // return data public int[] getData() { return data; } }
A Java example of the insertion sort algorithm.
Running a test file of 100000 randomly ordered integers through this algorithm takes about 1.53 seconds on my system. I will be using the same test file for comparison against other sorting algorithms.
Josh
/** * A very basic Insertion Sort class. * * This class will only sort an int[]. * * @author Joshua Weise */ public class InsertionSort { // array of numbers private int[] data; public InsertionSort(int[] data) { this.data = data; } // Insertion sort data public void sort() { for (int i = 1; i < data.length; i++) { // the value we are moving left int y = data[i]; // the array location we could move our value into int z = i; // loop until we find the farthest left we can move our value into // moving the other values out of the way right as we go while (z > 0 && y < data[z1]) { data[z] = data[z  1] ; z; } // put our original value into its new position data[z] = y; } } // return data public int[] getData() { return data; } }