Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

just committing some WIP

None of this is actually for a project, its just my way of working on
coding exercises to keep in touch and see if they are good potential
interview exercises
  • Loading branch information...
commit 246e384a355fa698158713b9166d6be14bbb7c5e 1 parent 4da089f
@gformich authored
View
BIN  .DS_Store
Binary file not shown
View
BIN  src/.DS_Store
Binary file not shown
View
BIN  src/com/.DS_Store
Binary file not shown
View
BIN  src/com/gregformichelli/.DS_Store
Binary file not shown
View
16 src/com/gregformichelli/hacking/AVLTree.java
@@ -0,0 +1,16 @@
+package com.gregformichelli.hacking;
+
+/**
+ * Just a quick coding exercise to get my hands dirty and stay fresh
+ *
+ * This isn't meant to be revolutionary, merely implementing something that already
+ * has been done before quite a bit.
+ *
+ * @author gformich
+ *
+ */
+public class AVLTree {
+
+ // TODO - complete coding this up
+
+}
View
91 src/com/gregformichelli/hacking/AVLTreeNode.java
@@ -0,0 +1,91 @@
+package com.gregformichelli.hacking;
+
+/**
+ * A node in my tree
+ *
+ * @author gformich
+ *
+ */
+public class AVLTreeNode <T> {
+
+
+ private Comparable<? super T> data;
+ private AVLTreeNode <T> parent;
+ private AVLTreeNode <T> left;
+ private AVLTreeNode <T> right;
+ private int height = 0;
+
+
+
+ public AVLTreeNode (Comparable<? super T> data, AVLTreeNode <T> parent,
+ AVLTreeNode <T> left, AVLTreeNode <T> right, int height) {
+ this.data = data;
+ this.parent = parent;
+ this.left = left;
+ this.right = right;
+ this.height = height;
+ }
+
+
+
+ public AVLTreeNode <T> getParent() {
+ return parent;
+ }
+
+
+
+ public void setParent(AVLTreeNode <T> parent) {
+ this.parent = parent;
+ }
+
+
+
+ public AVLTreeNode <T> getLeft() {
+ return left;
+ }
+
+
+
+ public void setLeft(AVLTreeNode <T> left) {
+ this.left = left;
+ }
+
+
+
+ public AVLTreeNode <T> getRight() {
+ return right;
+ }
+
+
+
+ public void setRight(AVLTreeNode <T> right) {
+ this.right = right;
+ }
+
+
+
+ public int getHeight() {
+ return height;
+ }
+
+
+
+ public void setHeight(int height) {
+ this.height = height;
+ }
+
+
+ @Override
+ public String toString() {
+ return "AVLTreeNode [data=" + data + ", height=" + height + "]";
+ }
+
+
+
+ public boolean isLeaf() {
+ return (this.height == 0);
+ }
+
+
+
+}
View
37 src/com/gregformichelli/hacking/Grade.java
@@ -0,0 +1,37 @@
+package com.gregformichelli.hacking;
+
+
+// nested static class for simple example
+public class Grade {
+
+ enum PrimaryGrade { A,B,C,D,F, UNKNOWN; }
+ enum GradeSubType {MINUS, PLUS, NONE};
+
+ PrimaryGrade grade = PrimaryGrade.UNKNOWN;
+ GradeSubType subtype = GradeSubType.NONE;
+
+ Grade(PrimaryGrade primary, GradeSubType type) {
+ this.grade = primary;
+ this.subtype = type;
+ }
+
+ Grade(PrimaryGrade primary) {
+ this.grade = primary;
+ }
+
+ String toPrimaryGradeString() {
+ return grade.toString();
+ }
+
+ public String toString() {
+ String subGrade = "";
+ if (subtype == GradeSubType.MINUS ) {
+ subGrade = "-";
+ }
+ else if( subtype == GradeSubType.PLUS) {
+ subGrade = "+";
+ }
+ return grade.toString() + subGrade;
+ }
+
+}
View
44 src/com/gregformichelli/hacking/InterviewQuestions.java
@@ -2,12 +2,14 @@
import java.nio.ByteOrder;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
+import java.util.regex.Pattern;
public class InterviewQuestions {
@@ -267,11 +269,53 @@ public static boolean isPowerOf2(int num) {
return result;
}
+ /**
+ * Uses a little trick from Hackers delight
+ * @param num
+ * @return
+ */
public static boolean isPowerOf2_binary(int num) {
if(num == 0) return false;
num = Math.abs(num);
return ( (num & (num - 1)) == 0);
}
+
+ /**
+ * Kind of weird for Java since strings are immutable and I am trying a C style algorithm
+ * just going through some interview stuff, and doing the in place thing
+ *
+ * @param str
+ * @param toRemove
+ * @return
+ */
+ public static String removeCharsFromString(String str, Character[] toRemove) {
+
+ // construct a hashset of removal characters
+ Set<Character> charsToRemove = new HashSet<Character>(Arrays.asList(toRemove));
+
+ // go through the character array in place
+ // omit any characters in the remove set
+ int dest = 0;
+ char[] strContent = str.toCharArray();
+ for(char c : strContent) {
+ if(! charsToRemove.contains(c)) {
+ strContent[dest++] = c;
+ }
+ }
+
+ return new String(strContent, 0, dest);
+ }
+
+ // Normal Java character removal
+ public static String removeCharsEasy(String str, Character[] removes) {
+ StringBuilder sb = new StringBuilder();
+ for(Character c: removes) {
+ sb.append(c);
+ }
+ String regex = String.format("[%s]", sb.toString());
+ return str.replaceAll(regex, "");
+ }
+
}
View
11 src/com/gregformichelli/hacking/InterviewQuestionsTest.java
@@ -181,5 +181,16 @@ public void testIsPowerOf2_bin() {
assertFalse(InterviewQuestions.isPowerOf2_binary(num));
}
}
+
+ @Test
+ public void testRemoveCharsFromString() throws Exception {
+
+ String s = "abcdefghzz23";
+ Character [] remove = {'z', '2'};
+ String result = InterviewQuestions.removeCharsFromString(s, remove);
+ assertEquals(result, "abcdefgh3");
+ result = InterviewQuestions.removeCharsEasy(s, remove);
+ assertEquals(result, "abcdefgh3");
+ }
}
View
49 src/com/gregformichelli/hacking/SimpleHisto.java
@@ -0,0 +1,49 @@
+package com.gregformichelli.hacking;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.gregformichelli.hacking.SimpleHistogram.Grade;
+
+/**
+ * Quick main program for testing out a simple histogram
+ *
+ */
+public class SimpleHisto {
+
+ // inner class for a simple object to hold grades for a set of papers
+ class Paper {
+ Grade gradeReceived;
+ // Question do we need to capture any other attributes
+ // other than grade?
+ }
+
+ public static void main( String [] argv) throws Exception {
+
+ SimpleHistogram histo = new SimpleHistogram();
+
+ // Create a set of Grades for papers
+ List<Grade> grades = getGrades(30);
+ for(Grade g: grades){
+ histo.addGrade(g);
+ }
+
+ System.out.println(histo.toString());
+ }
+
+ public static List<Grade> getGrades(int num) {
+
+ List<Grade> paperGrades = new ArrayList<Grade>(num);
+ for(int i=0; i<num; i++) {
+ paperGrades.add(getRandomGrade());
+ }
+
+ return paperGrades;
+ }
+
+ private static Grade getRandomGrade() {
+ int g = (int) ((Math.random()*10) % Grade.values().length);
+ return Grade.values()[g];
+ }
+
+}
View
55 src/com/gregformichelli/hacking/SimpleHistogram.java
@@ -0,0 +1,55 @@
+package com.gregformichelli.hacking;
+
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.TreeMap;
+
+/**
+ * Simple histogram for storing grades and counts
+ *
+ */
+public class SimpleHistogram {
+
+ private Map<Grade, Integer> dataMap = new TreeMap<Grade, Integer>();
+
+ enum Grade {A,B,C,D,F; }
+
+ public void addGrade(Grade g) {
+ if(dataMap.containsKey(g)) {
+ int count = dataMap.get(g);
+ dataMap.put(g, ++count);
+ }
+ else {
+ dataMap.put(g, 1);
+ }
+ }
+
+ public int getGradeCount(Grade g) {
+ int count = 0;
+ if(dataMap.containsKey(g)) {
+ count = dataMap.get(g);
+ }
+ return count;
+ }
+
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for(Entry<Grade, Integer> e: dataMap.entrySet()) {
+ sb.append( String.format("%s | %s", e.getKey(),
+ formatCount('*', e.getValue())) );
+ sb.append("\n");
+ }
+ return sb.toString();
+ }
+
+
+ String formatCount(char c, Integer value) {
+
+ StringBuilder sb = new StringBuilder();
+ for(int i=0; i<value; i++) {
+ sb.append(c);
+ }
+ return sb.toString();
+ }
+
+}
View
76 src/com/gregformichelli/hacking/SimpleHistogramTest.java
@@ -0,0 +1,76 @@
+package com.gregformichelli.hacking;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.gregformichelli.hacking.SimpleHistogram.Grade;
+
+/**
+ * Unit tests for simple histogram methods
+ *
+ */
+public class SimpleHistogramTest {
+
+ @Test
+ public void testFormatCount() {
+ SimpleHistogram sh = new SimpleHistogram();
+
+ String result = sh.formatCount('*', 6);
+ assertEquals("******", result);
+
+ result = sh.formatCount('*', 0);
+ assertEquals("", result);
+
+ result = sh.formatCount('#', 4);
+ assertEquals("####", result);
+
+ result = sh.formatCount('*', -2);
+ assertEquals("", result);
+ }
+
+ @Test
+ public void testAddGrade_and_getCount() {
+ SimpleHistogram sh = new SimpleHistogram();
+
+ assertTrue(sh.getGradeCount(Grade.B) == 0);
+
+ sh.addGrade(Grade.B);
+ assertTrue(sh.getGradeCount(Grade.B) == 1);
+
+ sh.addGrade(Grade.B);
+ assertTrue(sh.getGradeCount(Grade.B) == 2);
+
+ sh.addGrade(Grade.B);
+ assertTrue(sh.getGradeCount(Grade.B) == 3);
+
+ sh.addGrade(Grade.A);
+ assertTrue(sh.getGradeCount(Grade.A) == 1);
+ assertTrue(sh.getGradeCount(Grade.B) == 3);
+ assertTrue(sh.getGradeCount(Grade.F) == 0);
+ }
+
+ @Test
+ public void testToString() {
+ SimpleHistogram sh = new SimpleHistogram();
+ int count = 1;
+ for(Grade g : Grade.values()) {
+ addToHist(g, sh, count++);
+ }
+ String expected = "A | *\n" +
+ "B | **\n" +
+ "C | ***\n" +
+ "D | ****\n" +
+ "F | *****\n";
+ assertEquals(expected, sh.toString() );
+
+ }
+
+ private void addToHist(Grade g, SimpleHistogram sh, int count) {
+ for(int i=0; i<count; i++) {
+ sh.addGrade(g);
+ }
+ }
+
+}
View
88 src/com/gregformichelli/hacking/TowerOfHanoi.java
@@ -0,0 +1,88 @@
+package com.gregformichelli.hacking;
+
+
+/**
+ * Quick exercise using the classic Tower of Hanoi puzzle
+ *
+ * @author gformich
+ *
+ */
+public class TowerOfHanoi {
+
+ //private Map<Peg, Stack<Disc>> gameBoard = new HashMap<Peg, Stack<Disc>>();
+ private int height;
+ private int moves = 0;
+ private Peg start, dest;
+
+ // simple data object for the discs
+ class Disc {
+ int size;
+ String id;
+ Peg pegLocation;
+ }
+
+ public enum Peg {
+ A, B, C;
+ }
+
+ Disc[] discs;
+
+ public TowerOfHanoi(int height, Peg startingPeg, Peg destinationPeg) {
+
+ this.height = height;
+ this.start = startingPeg;
+ this.dest = destinationPeg;
+
+ discs = new Disc[height];
+
+ // largest disc has highest ordinal
+ // is on bottom
+ for(int i=0; i<height; i++) {
+ Disc d = new Disc();
+ d.size = i;
+ d.pegLocation = startingPeg;
+ d.id = "id_" + i;
+ discs[i] = d;
+ }
+ }
+
+ public void solve() {
+ Peg with = null;
+ for(Peg p: Peg.values()) {
+ if(p != start && p != dest) {
+ with = p;
+ }
+ }
+ hanoi(height, start, dest, with);
+ System.out.println("Finished");
+ }
+
+ private void hanoi(int h, Peg from, Peg to, Peg with) {
+
+ if (h >= 1) {
+ // recursive approach
+ // move the tower minus the largest disk to the "with" peg
+ hanoi(h - 1, from, with, to);
+ // move the largest disc to the destination
+ moveDisc(discs[h - 1], from, to);
+ // move the tower minus largest from "with" to destination peg
+ hanoi(h - 1, with, to, from);
+ }
+
+ }
+
+ private void moveDisc(Disc d, Peg from, Peg to) {
+
+ if( d.pegLocation != from ) {
+ System.out.println("Illegal move!!");
+ return;
+ }
+
+ moves++;
+ String move = String.format("Moving disc %s from Peg %s to Peg %s \n", d.id, from, to);
+ System.out.println(move);
+ d.pegLocation = to;
+ }
+
+}
+
View
11 src/com/gregformichelli/hacking/TowerRunner.java
@@ -0,0 +1,11 @@
+package com.gregformichelli.hacking;
+
+public class TowerRunner {
+
+ public static void main(String [] argv) {
+
+ TowerOfHanoi t = new TowerOfHanoi(7, TowerOfHanoi.Peg.A, TowerOfHanoi.Peg.C);
+ t.solve();
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.