Skip to content

Instantly share code, notes, and snippets.

Fiduciary License Agreement 2.0
based on the
Individual Contributor exclusive License Agreement
(including the TRADITIONAL PATENT LICENSE OPTION)
Thank you for your interest in contributing to Think Code Learn Ltd t/a Revelation Games's Tiny King ("We" or "Us").
The purpose of this contributor agreement ("Agreement") is to clarify and document the rights granted by contributors to Us. To make this document effective, please follow the instructions at ____________________.
0. Preamble
def assert_equal(expected, actual)
print expected == actual ? "." : "Expected #{expected}, but got #{actual}\n"
end
class PriceRule
def initialize(code, price)
@code = code
@price = price
end
@chrismdp
chrismdp / plus.rb
Created October 3, 2018 14:34
Plus / Sandwich solution
def plus(num)
(1..num).to_a.map {|x| yield(x) }.join
end
p plus(3) { "ding" }
p plus(3) { |x| "...#{x}" }
p plus(5) { |x| (x + 2).to_s }
def sandwich(layers = 1, &block)
@chrismdp
chrismdp / -
Created December 5, 2017 11:28
def assert_equal(expected, actual)
print expected == actual ? "." : "Expected #{expected.inspect} but got #{actual.inspect}\n"
end
Discount = Struct.new(:amount, :value) do
def apply(basket, item)
(basket.count(item)/amount) * value
end
end
@chrismdp
chrismdp / achievements.sh
Last active June 7, 2017 15:39
ImagicMagick + Shell script to generate my Kickstarter reward chart on this campaign: https://www.kickstarter.com/projects/chrismdp/ealdorlight?ref=2q0zxb
#!/bin/bash
TITLE_BG=title-bg.png
EL_BG=el-circle.png
OUT=achievements.png
# A colon-seperated rewards list.
# First column is the type of reward:
# <text> - a text number.
# Columns are alpha divisor (eg 4 is 25%), percentage complete, big number, smaller label and text description.
def assert_equal(expected, actual)
print expected == actual ? "." : "Expected #{expected.inspect} but got #{actual.inspect}\n"
end
class SummingRule
def initialize(item, price)
@item = item
@price = price
end
class Checkout
def initialize
@total = 0
end
PRICES = { "Apple" => 30, "Banana" => 50 }
def scan(item)
@total += PRICES[item]
end
@chrismdp
chrismdp / s3.sh
Last active September 13, 2024 12:53
Uploading to S3 in 18 lines of Shell (used to upload builds for http://soltrader.net)
# You don't need Fog in Ruby or some other library to upload to S3 -- shell works perfectly fine
# This is how I upload my new Sol Trader builds (http://soltrader.net)
# Based on a modified script from here: http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
S3KEY="my aws key"
S3SECRET="my aws secret" # pass these in
function putS3
{
path=$1
@chrismdp
chrismdp / unsplash.sh
Last active December 28, 2015 17:17
Script to grab one of the 10 more recent images from Unsplash, and tint and blur it for use as an iTerm background using Solarized Dark
#!/bin/bash
# NOTE: requires ImageMagick
# NOTE: will tint to the Solarized Dark default background scheme, as the iTerm background blend option didn't lower the contrast enough for me
# NOTE: Optimised for a 1280x800 point screen (my 13" Retina MBP)
set -e
index=$[ 1 + $[ RANDOM % 10 ]]
img=`curl https://unsplash.com/rss/ | xmllint --xpath '/rss/channel/item['$index']/image/url/text()' -`
curl "$img" > ~/unsplash-latest.jpg &&
@chrismdp
chrismdp / Blob.cpp
Created September 3, 2014 09:36
A reasonably naïve Blob implementation in C++, complete with Array and Hash packed data types.
#include "Blob.h"
namespace sol {
unsigned Object::used() const {
BlobType type = (BlobType)_blob.getByte(0);
switch (type) {
case BL_INT:
return Integer(_blob).used();
case BL_ARRAY:
return Array::wrap(_blob).used();