Skip to content

Instantly share code, notes, and snippets.

View manuel-sugawara's full-sized avatar

Manuel Sugawara manuel-sugawara

  • Seattle, WA. United States
View GitHub Profile
@manuel-sugawara
manuel-sugawara / gist:9592308
Last active August 29, 2015 13:57
Write a function that returns whether a given binary tree is a valid binary search tree.
/**
* Returns {@code true} if the node is the root of a vaild binary
* search tree.
*/
public static boolean isBinarySearchTree(Node<T extends Comparable> node) {
if (node == null) {
return true;
}
if (node.left == null && node.right == null) {
@manuel-sugawara
manuel-sugawara / gist:9592704
Created March 17, 2014 02:03
Write a function to remove a single linked list's nth from last element.
/**
* Removes the list's nth from last element.
*/
public void removeNthFromLast(List list, int n) {
Node ahead = list.head;
for (int x = 0; x < n; x++) {
if (ahead.next == null) {
return null;
}
ahead = ahead.next;
@manuel-sugawara
manuel-sugawara / gist:9602702
Created March 17, 2014 16:26
Write a function that, given two nodes and a tree root, finds the two nodes' lowest common ancestor. That is, the function should find the ancestor that both nodes share that is furthest away from the root.
/**
* Given two nodes and the root of a tree find their lowest
* common ancestor.
*/
public Node findLowestCommonAncestor(Node root, Node a, Node b) {
// If either a or b are the root, then the root is the lowest common ancestor.
if (a == root || b == root) {
// Not sure if this is a correct answer. It depends on
// whether you allow a node to be ancestor of itself or
// not. If the former the answer is correct, else null
letme-get-help-for-that() {
cmd=$@
IFS=:
for x in `echo "$ ${cmd} --help" | perl -p -e 's/(.)/$1:/g'`; do printf "%s" "$x"; sleep 0.5; done
for ((y=0;y<6;y++)); do sleep 0.5; echo -n "\b"; done ;
tput smul; tput bold;
for x in `echo "--help" | perl -p -e 's/(.)/$1:/g'`; do sleep 0.5; printf "%s" "$x"; done;
tput rmul; tput sgr0;
echo ""
$cmd --help
@manuel-sugawara
manuel-sugawara / nosense.sh
Created July 19, 2017 18:07
cow says random fortunes just for you
#!/bin/bash
tput civis
while true; do
style=`cowsay -l | grep -v /usr/local | perl -p -e 's/ /\n/g' | egrep -v '(telebears|head-in|sodomized)' | gsort -R | head -1`
cookie=`mktemp`
fortune > $cookie
for x in `seq 5`; do
clear
cat $cookie | cowsay -f $style | lolcat;