Skip to content

Instantly share code, notes, and snippets.

@montycheese
montycheese / ReverseInt.php
Last active August 29, 2015 14:20
Reversing an integer in PHP with pure tail recursion
function reverseInt($n){
return reverseIntAcc(0, $n);
}
function reverseIntAcc($acc, $n){
if ($n == 0){
return $acc;
}
$acc *= 10;
return reverseIntAcc($acc + ($n % 10) , floor($n / 10));
@montycheese
montycheese / decisionTree.js
Created July 2, 2015 04:16
Customer service decision tree in javascript
<script>
$(document).ready( function () {
$('#customerTripsTable').DataTable();
$('#customerNotesTable').DataTable();
} );
//reset all children nodes in tree if the parent is clicked
document.getElementById("contacttype").onclick = destroyChildren;
//listener for contact type dropdown
document.getElementById("contacttype").onchange = function() {
var selected = document.getElementById("contacttype").value;
@montycheese
montycheese / knightsTour.cpp
Created August 20, 2015 20:00
Knights tour iterative solution
#include <iostream>
#define B_SIZE 8
using std::cout;
using std::cin;
using std::endl;
bool checkBounds(int currentRow, int currentCol, int horiz, int vert);
int move(int currentRow, int currentCol, int * newCoordinates, int step);
void recordMove(int row, int col);
@montycheese
montycheese / IntersectingNodes.java
Created September 10, 2015 04:23
Finds intersecting nodes between two linked lists if any
import java.util.HashSet;
public class IntersectingNodes {
HashSet<NodeCustom> hs = new HashSet<>();
NodeCustom head1;
NodeCustom head2;
public IntersectingNodes(NodeCustom n1, NodeCustom n2){
this.head1 = n1;
this.head2 = n2;
@montycheese
montycheese / SortStack.java
Created September 10, 2015 20:05
No data structures except an extra stack can be used to sort our original stack. smallest elements on top.
//No data structures except an extra stack can be used to sort our original stack. smallest elements on top.
import java.util.Arrays;
import java.util.Stack;
public class SortStack<T extends Comparable> {
private Stack<T> main, storage;
private T temp;
public SortStack(Stack<T> s){
main = s;
@montycheese
montycheese / CheckSubTree.java
Created October 9, 2015 15:12
check if a binary tree is the subtree of another. (Cannot assume that they are BST, and duplicates may be present, on either side of the original.)
public class CheckSubTre {
TreeNode<Integer> t1, t2;
//t2 might be subset of t1
public CheckSubTre(TreeNode<Integer> t1, TreeNode<Integer> t2){
this.t1 = t1;this.t2=t2;
}
public boolean check(TreeNode<Integer> t1, TreeNode<Integer> t2){
if(t1 == t2){
@montycheese
montycheese / word_ladder.py
Created October 20, 2015 13:47
solves a word ladder given a dictionary
from string import ascii_lowercase as alphabet
d = {"dog","dig", "din", "dag","fog",
"god", "fig", "ape", "dot", "got",
"bog", "bag","bat","cat"
}
def transform(w1, w2):
frontier = [[w1]]
explored = set()
@montycheese
montycheese / str.asm
Created November 3, 2015 14:32
map a char freq in MIPS assembly lang
.data
str1: .asciiz "Enter String: "
#.align 2
getc: .asciiz "Enter char: "
#.align 2
ret: .asciiz "Character <ch> occurs in string <string> <n> times"
buffer: .space 100 #space to store our string
L1: .space 2 #space for our char
L2: .space 4 #space for our char’s frequency in string
.data
str1: .asciiz "Enter input string: "
getc1: .asciiz "Enter char to replace: "
getc2: .asciiz "Enter char to replace with: "
ret1: .asciiz "Original string: "
ret2: .asciiz "Substitute "
ret3: .asciiz "Result String: "
arrow: .asciiz " -> "
@montycheese
montycheese / redirect.html
Created January 31, 2016 00:03
redirect to page via javascript
<html>
<head>
</head>
<body>
<h1>Website currently down...</h1>
<h2>You will be redirected to my GitHub page</h2>
</body>
</html>
<script>