Skip to content

Instantly share code, notes, and snippets.

@perspectivezoom
perspectivezoom / pgessays.py
Created Nov 19, 2012 — forked from olasitarska/pgessays.py
Builds epub book out of Paul Graham's essays.
View pgessays.py
# -*- coding: utf-8 -*-
"""
Builds epub book out of Paul Graham's essays: http://paulgraham.com/articles.html
Author: Ola Sitarska <ola@sitarska.com>
This script requires python-epub-library: http://code.google.com/p/python-epub-builder/
"""
import re, ez_epub, urllib2, genshi
View game_of_life.rb
class Position
attr_reader :x, :y
@cache_of_positions = {}
def self.new_with_memo(x,y)
@cache_of_positions[[x,y]] || Position.new(x,y).tap do |instance|
@cache_of_positions[[x,y]] = instance
end
end
View task_spec.rb
require 'rspec'
require 'simplecov'
SimpleCov.start
require './task.rb'
describe Todo::Task do
before(:each) do
@time = Time.now
Time.stub(:now).and_return(@time)
View list_spec.rb
require 'rspec'
require 'simplecov'
require './task.rb'
SimpleCov.start
require './list.rb'
describe Todo::List do
before(:each) do
@file_path = './spec_todo.txt'
View sudoku_guess_and_check.rb
class SudokuBoard
def initialize(board_str)
@board = board_str.split("").map { |value| Cell.new(value.to_i) }
@groups = []
(generate_rows + generate_cols + generate_grids).each do |group_member_indices_ar|
group_cell_members = group_member_indices_ar.map { |index| @board[index] }
@groups << Group.new(group_cell_members)
end
puts to_s
View array_pad.rb
class Array
def pad!(min_size, value = nil)
self.concat( Array.new(min_size - self.length, value)) if self.length < min_size
self
end
def pad(min_size, value = nil)
self.dup.pad!(min_size,value)
end
end
View anagram.rb
module Anagram
attr_reader :word_dict
def anagrams
@word_dict = {}
self.each do |str|
puts str
sorted_str = str.chars.sort.join
if @word_dict[sorted_str] == nil
@word_dict[sorted_str] = [str]
else
View jstwitter.rb
require 'jumpstart_auth'
require 'bitly'
class JSTwitter
attr_reader :client
def initialize
puts "Initializing"
@client = JumpstartAuth.twitter
end
View rock_paper_scissors.rb
def game(ar)
throw1 = ar[0][1].downcase
throw2 = ar[1][1].downcase
if %w{ rs pr sp }.include?(throw1 + throw2)
return ar[0]
elsif %w{ sr rp ps}.include?(throw1 + throw2)
return ar[1]
else
"It's a Tie!"
View mark_twain.rb
module Mark_twain
def mark_twain
mark_twain_helper(self, [])
end
def mark_twain_helper(str, accum_ar)
if str.split.length == 1
accum_ar << "Final draft: #{str}."
accum_ar
else