Skip to content

Instantly share code, notes, and snippets.

@angusb
angusb / gist:529832458f32d5999406
Created April 23, 2015 00:38
Scala Publisher & Subscriber Example
class MyPub extends Publisher[Int] {
override type Pub = Publisher[Int]
// def publish() { publish(5) }
}
class MySub(pub: MyPub) extends MyPub#Sub {
pub.subscribe(this)
var eventCount: Int = 0
override def notify(pub: MyPub#Pub, event: Int): Unit = eventCount += 1
}
@angusb
angusb / gist:0b87943eaa76b4ff1dd8
Created October 15, 2014 20:17
Java Introspection (dump)
import java.lang.reflect.Array;
import java.lang.reflect.Field;
// http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful
public static String dump(Object o, int callCount) {
callCount++;
StringBuffer tabs = new StringBuffer();
for (int k = 0; k < callCount; k++) {
tabs.append("\t");
# In a game of rock-paper-scissors (RPS), each player chooses to play Rock (R), Paper (P), or Scissors (S). The rules are: R beats S; S beats P; and P beats R. We will encode a rock-paper-scissors game as a list, where the elements are themselves 2-element lists that encode a player's name and a player's selected move, as shown below:
# [ ["Armando", "P"], ["Dave", "S"] ] # Dave would win since S > P
# Part A: Write a method rps_game_winner that takes a two-element list and behaves as follows:
# If the number of players is not equal to 2, raise WrongNumberOfPlayersError.
# If either player's strategy is something other than "R", "P" or "S" (case-insensitive), raise NoSuchStrategyError.
# Otherwise, return the name and move of the winning player. If both players play the same move, the first player is the winner.
# We'll get you started:
def str_to_int(input):
base = ord('0')
def char_to_int(c, d):
n = ord(d) - base
if not (0 <= n <= 9):
raise ValueError('Bad input')
return 10 * c + n
if input[0] == '-':
#include <iostream>
#include <algorithm>
#include <map>
#include <bitset>
#include <vector>
#include <cstdlib>
#include <inttypes.h>
#include <stdio.h>
#include <time.h>
"""Python is not optimized for tail recursion. As a result, a trampoline
can be used to circumvent this pitfall. The following is an attempt to
implement a trampoline."""
# **A function call is said to be tail recursive if there is
# nothing to do after the function returns except return its value.**
# Demonstration of call stack explosion
def naive_factorial(n):
@angusb
angusb / gist:4172194
Created November 29, 2012 22:00
circular python imports
# a.py
print '1a'
import b
print '2a'
x = b.x + 1
# b.py
print '1b'
import a
print '2b'
byte_field = [ord(b) for b in '\x7f\xff\xff\xff\xf5\xf7}\xfdw\xff\xff\xff\xff`']
for byte_idx, byte in enumerate(byte_field):
for i in xrange(8):
has_piece = (byte >> i) & 1
if has_piece:
print (byte_idx * 8 + i)
@angusb
angusb / gist:4120327
Created November 20, 2012 19:12
stylistic python?
msgs = []
for begin, length in piece.empty_blocks():
msgs.append(Msg('request',
index=piece_num_to_fetch,
begin=begin,
length=length))
return msgs
@angusb
angusb / gist:3901024
Created October 16, 2012 18:16
asanaapi
class AsanaResource(object):
def __init__(self):
# stuff
def get(self, endpoint=""):
"""Submits a get to the Asana API and returns the result.
Returns:
dict: json response from Asana
"""