Skip to content

Instantly share code, notes, and snippets.

@rsayers
rsayers / numname.py
Created January 21, 2014 20:13
After searching for a library to spell out the names of integers, I decided to roll my own. Its far easier than I expected
def name(n):
onesteens = ['one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
tens = ['twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
if n < 20:
return onesteens[n-1]
if n > 19 and n < 100:
dm = divmod(n,10)
program test1
integer :: a,b,c
do a=1,1000
do b=a+1,1000
do c=b+1,1000
if(a*a+b*b==c*c) then
if (a+b+c==1000) then
write(*,*) a,b,c
stop
#include <stdio.h>
int main(){
int a,b,c;
for (a=0;a<10000;a++){
for(b=a+1;b<10000;b++){
for(c=b+1;c<10000;c++){
if (a*a+b*b==c*c){
if (a+b+c==1000){
@rsayers
rsayers / gist:8409139
Created January 13, 2014 22:12
Base256 encoding, again from the wikipedia example
# -*- coding: utf-8 -*-
class Base256
def initialize
@list = [['aardvark','adroitness'],['absurd','adviser'],['accrue','aftermath'],['acme','aggregate'],['adrift','alkali'],['adult','almighty'],['afflict','amulet'],['ahead','amusement'],['aimless','antenna'],['Algol','applicant'],['allow','Apollo'],['alone','armistice'],['ammo','article'],['ancient','asteroid'],['apple','Atlantic'],['artist','atmosphere'],['assume','autopsy'],['Athens','Babylon'],['atlas','backwater'],['Aztec','barbecue'],['baboon','belowground'],['backfield','bifocals'],['backward','bodyguard'],['banjo','bookseller'],['beaming','borderline'],['bedlamp','bottomless'],['beehive','Bradbury'],['beeswax','bravado'],['befriend','Brazilian'],['Belfast','breakaway'],['berserk','Burlington'],['billiard','businessman'],['bison','butterfat'],['blackjack','Camelot'],['blockade','candidate'],['blowtorch','cannonball'],['bluebird','Capricorn'],['bombast','caravan'],['bookshelf','caretaker'],['brackish','celebrate'],['breadline','cellulose'],['br
@rsayers
rsayers / gist:8408198
Created January 13, 2014 21:09
Rough implementation of Base64 done to understand it a bit better
# This doesnt handle padding or non ascii, but its a rough implementation based on the wikipedia description
module RobBase64
def self.encodebyte(byte)
case byte
when 0..25 then (byte+65).chr
when 26..51 then (byte-26+97).chr
when 52..61 then (byte-4).chr
when 62 then "+"
when 63 then "/"
@rsayers
rsayers / spam.sh
Created July 18, 2013 17:14
I have a vps that does nothing but handle mail for 1 account, I needed something to block spam, but running spamd was too heavy for my vps. I run this every minute. it simply finds every mail <2 minutes old and moves it if spam assassin claims it is spam. It's low tech, but also low requirements.
#!/bin/sh
INBOX="/path/to/Maildir/cur"
SPAM="/path/to/Maildir/.spam/cur"
cd $INBOX
for i in `find ./ -type f -mmin -2`
do
echo "Testing $i"
if (cat $i | spamassassin | grep 'X-Spam-Flag..YES')
then echo "$i is spam"
mv $i $SPAM
@rsayers
rsayers / hiragana.rb
Created June 24, 2013 22:25
Simple console based hiragana drill tool
# -*- coding: utf-8 -*-
pairs = [["あ","a"],
["か","ka"],
["さ","sa"],
["た","ta"],
["な","na"],
["は","ha"],
["ま","ma"],
["や","ya"],
@rsayers
rsayers / lit.rb
Created September 6, 2012 17:47
Literate programming in Ruby
def lit(src)
eval src.split("\n").delete_if { |l| l[0]!='>' }.map {|l| l[1,l.length] }.join("\n")
end
lit <<src
This is a big wall of text with no Ruby code...
Does this work?
1 1 + .
I bet it didn't... maybe the following works:
#include <stdio.h>
#include <string.h>
#include "mongoose.h"
#include <chibi/eval.h>
static void *callback(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info) {
@rsayers
rsayers / watchmaker.rb
Created December 16, 2011 00:12
String evolution as described in Richard Dawkins: "The Blind Watchmaker"
# from: http://www.informit.com/articles/article.aspx?p=683059&seqNum=36
class String
def levenshtein(other, ins=2, del=2, sub=1)
# ins, del, sub are weighted costs
return nil if self.nil?
return nil if other.nil?
dm = [] # distance matrix
# Initialize first row values
dm[0] = (0..self.length).collect { |i| i * ins }