Permalink
Browse files

initial check in

  • Loading branch information...
0 parents commit 4da089fee37019726c122aaaff3a83292e72fa1b @gformich committed Jan 11, 2012
Showing with 1,988 additions and 0 deletions.
  1. +7 −0 .classpath
  2. +1 −0 .gitignore
  3. +17 −0 .project
  4. +12 −0 .settings/org.eclipse.jdt.core.prefs
  5. +146 −0 src/com/gregformichelli/hacking/BinarySearchTree.java
  6. +45 −0 src/com/gregformichelli/hacking/BinarySearchTreeTest.java
  7. +59 −0 src/com/gregformichelli/hacking/BitTwiddling.java
  8. +40 −0 src/com/gregformichelli/hacking/BitTwiddlingTest.java
  9. +69 −0 src/com/gregformichelli/hacking/CCDetector.java
  10. +32 −0 src/com/gregformichelli/hacking/CCDetectorTest.java
  11. +45 −0 src/com/gregformichelli/hacking/CardValidation.java
  12. +28 −0 src/com/gregformichelli/hacking/CardValidationTest.java
  13. +126 −0 src/com/gregformichelli/hacking/CheckNumberFormatter.java
  14. +106 −0 src/com/gregformichelli/hacking/CheckNumberFormatterTest.java
  15. +160 −0 src/com/gregformichelli/hacking/Factorial.java
  16. +109 −0 src/com/gregformichelli/hacking/FactorialTest.java
  17. +18 −0 src/com/gregformichelli/hacking/IntNode.java
  18. +277 −0 src/com/gregformichelli/hacking/InterviewQuestions.java
  19. +185 −0 src/com/gregformichelli/hacking/InterviewQuestionsTest.java
  20. +26 −0 src/com/gregformichelli/hacking/ListNode.java
  21. +29 −0 src/com/gregformichelli/hacking/ListUtils.java
  22. +21 −0 src/com/gregformichelli/hacking/Node.java
  23. +68 −0 src/com/gregformichelli/hacking/Palindrome.java
  24. +119 −0 src/com/gregformichelli/hacking/SinglyLinkedList.java
  25. +82 −0 src/com/gregformichelli/hacking/SinglyLinkedListTest.java
  26. +68 −0 src/com/gregformichelli/hacking/Solution.java
  27. +67 −0 src/com/gregformichelli/hacking/TextFunctions.java
  28. +26 −0 src/com/gregformichelli/hacking/TextFunctionsTest.java
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
@@ -0,0 +1 @@
+*.class
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>hackingAround</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,12 @@
+#Sat Aug 06 14:25:29 EDT 2011
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
@@ -0,0 +1,146 @@
+package com.gregformichelli.hacking;
+
+public class BinarySearchTree {
+
+ private TreeNode root;
+
+ // static nested class so as not to have
+ // access to the enclosing class
+ private static class TreeNode {
+
+ TreeNode right;
+ TreeNode left;
+ int data;
+
+ public TreeNode(int theData) {
+ right = null;
+ left = null;
+ data = theData;
+ }
+
+ }
+
+ /**
+ * Constructor for a new BST
+ * @param theRoot
+ */
+ public BinarySearchTree(int data) {
+ insertNode(data);
+ }
+
+ public BinarySearchTree() {
+ }
+
+ public int getRoot() {
+ if (root == null) {
+ throw new NullPointerException("BST hasn't been initialized, empty.");
+ }
+ return root.data;
+ }
+
+ public void insertNode(int data) {
+ root = insertNode(root, data);
+ }
+
+ /**
+ * Walks down the current BST recursively and inserts
+ * a new node where it belongs
+ *
+ * @param data
+ * @param node
+ * @return
+ */
+ public TreeNode insertNode(TreeNode node, int data) {
+ if(node == null) {
+ node = new TreeNode(data);
+ }
+ else {
+ if(data <= node.data) {
+ node.left = insertNode(node.left, data);
+ }
+ else {
+ node.right = insertNode(node.right, data);
+ }
+ }
+ return node;
+ }
+
+ /**
+ * Starts at root by default
+ * and calls the recursive hasValue
+ *
+ */
+ public boolean hasValue(int theVal) {
+ return hasValue(root, theVal);
+ }
+
+ /**
+ * recursively search the BST
+ */
+ public boolean hasValue(TreeNode startNode, int dataToFind) {
+
+ if(startNode == null) {
+ return false;
+ }
+
+ if(dataToFind == startNode.data) {
+ return true;
+ }
+
+ if(dataToFind < startNode.data) {
+ return hasValue(startNode.left, dataToFind);
+ }
+ else {
+ return hasValue(startNode.right, dataToFind);
+ }
+
+ }
+
+ /**
+ * This counts the BST, its an exercise in traversal
+ * we can keep a counter
+ *
+ * @return
+ */
+ public int getNumberOfNodes() {
+ return( size(root));
+ }
+
+ /**
+ * Recursive count of left and right
+ *
+ * @param node
+ * @return
+ */
+ public int size(TreeNode node) {
+ if(node == null) {
+ // convergent case
+ return 0;
+ }
+ else {
+ // accounts for the current node
+ return size(node.left) + 1 + size(node.right);
+ }
+ }
+
+ public int getDepth() {
+ return getDepth(root);
+ }
+
+ public int getDepth(TreeNode node) {
+ if(node == null) {
+ // convergent case
+ return 0;
+ }
+ else {
+ int leftDepth = getDepth(node.left);
+ int rightDepth = getDepth(node.right);
+ // accounts for the current node, hence +1
+ int maxDepth = (leftDepth > rightDepth) ? leftDepth : rightDepth;
+ return maxDepth + 1;
+ }
+ }
+
+
+
+}
@@ -0,0 +1,45 @@
+package com.gregformichelli.hacking;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class BinarySearchTreeTest {
+
+ @Test
+ public void testConstructor() {
+ BinarySearchTree bst = new BinarySearchTree(3);
+ assertEquals(3, bst.getRoot());
+
+ bst = new BinarySearchTree();
+ try {
+ bst.getRoot();
+ fail("Empty bst should not have a root value");
+ }
+ catch(NullPointerException npe) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testInsert() {
+ BinarySearchTree bst = new BinarySearchTree();
+ bst.insertNode(5);
+ bst.insertNode(8);
+ bst.insertNode(9);
+
+ // verify it is
+ // 5
+ // \
+ // 8
+ // \
+ // 9
+ assertEquals(5, bst.getRoot());
+ assertTrue(bst.hasValue(5));
+ assertTrue(bst.hasValue(9));
+
+ assertEquals(3, bst.getNumberOfNodes());
+ assertEquals(3, bst.getDepth());
+ }
+
+}
@@ -0,0 +1,59 @@
+package com.gregformichelli.hacking;
+
+public class BitTwiddling {
+
+ public static int reverseBits(int foo) {
+
+ // won't work for signed ints
+ if (foo < 0) {
+ return -1;
+ }
+
+ int v = foo; // input bits to be reversed
+ int r = v & 1; // r will be reversed bits of v; first get LSB of v
+ // extra shift needed at end already got lsb
+ // plus don't shift sign bit
+ int s = Integer.SIZE - 2;
+
+ // right shift v each iteration
+ // left shift r
+ for (v >>= 1; v>0; v >>= 1)
+ {
+ r <<= 1;
+ r |= v & 1;
+ s--;
+ }
+ String binValReversed = Integer.toString(r, 2);
+ System.out.println(binValReversed);
+ r <<= s; // shift when v's highest bits are zero
+
+ return r;
+ }
+
+
+ public static boolean isBinaryPalindrome(int val) {
+
+ String binVal = Integer.toString(val, 2);
+
+ System.out.println(binVal);
+
+ return Palindrome.isPalindrone(binVal);
+ }
+
+ /**
+ * Returns true for even without modulus operator
+ *
+ * @param val
+ * @return
+ */
+ public static boolean isEvenNumber(int val) {
+
+ // right now zero counts as even
+ int num = val;
+ return ( (num & 1) == 0);
+ }
+
+ public static boolean isOddNumber(int val) {
+ return ! (isEvenNumber(val));
+ }
+}
@@ -0,0 +1,40 @@
+package com.gregformichelli.hacking;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class BitTwiddlingTest {
+
+ @Test
+ public void testIsBinaryPalindrome() {
+ int val = Integer.parseInt("1001", 2);
+ assertTrue(BitTwiddling.isBinaryPalindrome(val));
+
+ val = Integer.parseInt("1101011", 2);
+ assertTrue(BitTwiddling.isBinaryPalindrome(val));
+ }
+
+ @Test
+ public void testReverseBits() throws Exception {
+
+ int val = Integer.parseInt("1101011", 2);
+ int reverseVal = BitTwiddling.reverseBits(val);
+ String binValReversed = Integer.toString(reverseVal, 2);
+ System.out.println(binValReversed);
+
+ }
+
+ @Test
+ public void testIsEvenNumber() {
+
+ for(int i=2; i<= 18; i+=2) {
+ assertTrue(BitTwiddling.isEvenNumber(i));
+ }
+
+ assertTrue(BitTwiddling.isEvenNumber(-4));
+
+ assertTrue(BitTwiddling.isEvenNumber(0));
+ }
+
+}
Oops, something went wrong.

0 comments on commit 4da089f

Please sign in to comment.