This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] == '-': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <map> | |
#include <bitset> | |
#include <vector> | |
#include <cstdlib> | |
#include <inttypes.h> | |
#include <stdio.h> | |
#include <time.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a.py | |
print '1a' | |
import b | |
print '2a' | |
x = b.x + 1 | |
# b.py | |
print '1b' | |
import a | |
print '2b' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
msgs = [] | |
for begin, length in piece.empty_blocks(): | |
msgs.append(Msg('request', | |
index=piece_num_to_fetch, | |
begin=begin, | |
length=length)) | |
return msgs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
""" |
NewerOlder