This file contains 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 CacheCall | |
# Calls a function that accepts a hash as its only parameter UNLESS | |
# the result has been recently cached. Cache keys are essentially a | |
# URL of the hash keys and values. | |
# | |
# ====Parameters | |
# | |
# +method_obj+:: | |
# A Ruby method object that supports call() and .name | |
# |
This file contains 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
require 'pp' | |
def succ(a, n) | |
# a is an array of integers <= n | |
# see tests | |
return nil if n == 0 | |
a = Array.new(a) | |
x = a.pop | |
if x + 1 < n | |
return a + [x + 1] |
This file contains 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
require 'pp' | |
def succ(a) | |
# destructive to a | |
n = a.size | |
k = (n-2).downto(0) do |k| | |
break k if a[k] < a[k+1] | |
end | |
return nil if a[k] >= a[k+1] | |
l = (n-1).downto(0) do |l| |
This file contains 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
require 'pp' | |
def chop(key, a) | |
low = 0 | |
high = a.size - 1 | |
while low < high do | |
mid = (low + high) / 2 | |
val = a[mid] | |
return mid if val == key | |
if val > key |
This file contains 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
require 'pp' | |
def lines(fn) | |
File.open(fn) do |f| | |
return f.readlines | |
end | |
end | |
def data | |
lines('weather.dat').map do |line| |
This file contains 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
require 'pp' | |
HEIGHT = 37 | |
WIDTH = 17 | |
def alive(game_board, x, y) | |
x %= WIDTH | |
y %= HEIGHT | |
coords = "#{x},#{y}" | |
game_board[coords] |
This file contains 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
assert = (cond) -> | |
throw("error") unless cond | |
max_run = (a) -> | |
m = 0 | |
c = 0 | |
for n in a | |
c += n | |
c = 0 if c < 0 | |
m = c if m < c |
This file contains 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
<head> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> | |
</head> | |
<h1>SOME APP</h1> |
This file contains 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
# This extremely dubious code shows how to simulate | |
# a custom control structure in CS. bizz_buzz does | |
# a highly configurable loop through the integers. | |
bizz_buzz = (config) -> | |
i = 0 | |
while true | |
i += 1 | |
break if config.quit(i) | |
if config.bizz(i) && config.buzz(i) | |
console.log "bizzbuzz" |
This file contains 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
handle_data = (data) -> | |
for line in data.split('\n') | |
regex = /(\w+)/g | |
while true | |
matches = regex.exec(line) | |
if matches | |
for word in matches[1..matches.length-1] | |
console.log word | |
else | |
break |
OlderNewer