Skip to content

Instantly share code, notes, and snippets.

View wffurr's full-sized avatar

William Furr wffurr

  • Google
  • Boston, MA
View GitHub Profile
@wffurr
wffurr / is_balanced.py
Created May 11, 2014 00:39
Is tree balanced?
def isBalanced(root):
""" returns true if the tree is balanced, i.e. the depths of the children vary by no more than one """
heights = set()
getHeights(root, 0, heights)
return not heights or (max(heights) - min(heights) <= 1)
def getHeights(node, h, heights):
""" add the height of this node's children to heights """
if node is None:
return
@wffurr
wffurr / DecimalToBinary.java
Created May 4, 2014 00:43
Convert String decimal to String binary
public class DecimalToBinary {
/** Given a decimal number as a string, returns the binary representation as a string or "ERROR" if it can't be converted to binary */
public static String decimalToBinary(String decimal) {
int dec = 0;
try {
dec = Integer.parseInt(decimal);
} catch(NumberFormatException ex) {
return "ERROR";
}
if(dec == 0) return "0";
@wffurr
wffurr / setbits.c
Created May 3, 2014 23:48
Set bits i through j of tgt to match src
#include <stdlib.h>
#include <stdio.h>
/* sets bits i through j in tgt to match src */
void setBits(int *tgt, int src, short i, short j) {
int mask = (((int)pow(2, j - i + 1)) - 1) << i;
*tgt = (src & mask) | (*tgt & ~mask);
}
#include <math.h>
@wffurr
wffurr / zipit.java
Created May 2, 2014 21:38
Zip two maps into a map of K to Tuple<V1, Optional<V2>>
/**
* Combines two maps with the same key into a map of key to tuple(value 1, optional value 2)
*/
public static <K, V1, V2> Map<K, Tuple<V1, Optional<V2>>>
zip(Map<K, V1> map1, Map<K, V2> map2)
{
Map<K, Tuple<V1, Optional<V2>>> results = new HashMap<>();
for (K key : map1.keySet())
{
V1 val1 = map1.get(key);
@wffurr
wffurr / countbits.c
Last active August 29, 2015 14:00
Count bits in a 32-bit integer
#include<stdio.h>
#include<limits.h>
#include<math.h>
/** returns the number of 1 bits in x */
short countBits(int x) {
short count = 0;
int mask = 1;
for(short i = 0; i < sizeof(x); i++) {
if((mask & x) == mask) {
@wffurr
wffurr / staqueue.py
Created March 11, 2014 02:26
Queue implementation using two stacks
"""
Implement a queue with two stacks
Interview practice from coding for interviews - March 2014
"""
class Staqueue(object):
""" Implements a queue with two stacks
One stack is used for enqueing, and the other dequeing. If an enqueue is requested
@wffurr
wffurr / kf_jb_logcat.log
Created July 22, 2012 23:18
Kindle Fire Jandycane 1.5 logcat - runaway sysinit/LocationManager
--------- beginning of /dev/log/system
I/LocationManagerService( 261): remove passive (pid 1430), next minTime = 0
I/LocationManagerService( 261): request passive (pid 1430) 0 0
I/LocationManagerService( 261): remove passive (pid 1430), next minTime = 0
I/LocationManagerService( 261): request passive (pid 1430) 0 0
I/LocationManagerService( 261): remove passive (pid 1430), next minTime = 0
I/LocationManagerService( 261): request passive (pid 1430) 0 0
I/LocationManagerService( 261): remove passive (pid 1430), next minTime = 0
I/LocationManagerService( 261): request passive (pid 1430) 0 0
I/LocationManagerService( 261): request passive (pid 14979) 0 0
@wffurr
wffurr / kf_jb_dmesg.log
Created July 22, 2012 22:55
Kindle Fire Jandycane 1.5 dmesg - runaway sysinit
<6>[ 0.000000] Initializing cgroup subsys cpu
<5>[ 0.000000] Linux version 3.0.31+ (hashcode@hashcode-UB) (gcc version 4.4.3 (GCC) ) #2 SMP PREEMPT Mon Jul 2 00:43:34 PDT 2012
<4>[ 0.000000] CPU: ARMv7 Processor [411fc093] revision 3 (ARMv7), cr=10c5387d
<4>[ 0.000000] CPU: VIPT nonaliasing data cache, VIPT aliasing instruction cache
<4>[ 0.000000] Machine: OMAP4430
<4>[ 0.000000] Ignoring tag cmdline (using the default kernel command line)
<6>[ 0.000000] Reserving 16777216 bytes SDRAM for VRAM
<4>[ 0.000000] Memory policy: ECC disabled, Data cache writealloc
<6>[ 0.000000] ***********************
<6>[ 0.000000] OMAP4430 ES2.3 type(GP)