Skip to content

Instantly share code, notes, and snippets.

View craigjbass's full-sized avatar
😁

Craig J. Bass craigjbass

😁
View GitHub Profile
class RomanNumerals
def self.to_roman(num)
('I' * num)
.gsub('IIIII', 'V')
.gsub('IIII', 'IV')
.gsub('VV', 'X')
.gsub('VIV', 'IX')
.gsub('XXXXX', 'L')
.gsub('XXXX', 'XL')
.gsub('LL', 'C')
require 'csv'
expectations = []
CSV.foreach("#{__dir__}/expectations2.csv") do |line|
expectations << {
expectation: line[0],
roles: line[4]&.split(','),
description: line[1]
}
class Tennis
def initialize
@player1_score = 'Zero'
@player2_score = 'Zero'
end
def won_point(player)
if player == :player1
@player1_score = next_score_for(@player1_score)
else
class HTTPProductGateway
def find_by(id:)
@id = id
product_for(decode(fetch_product_from_api))
end
private
def product_for(product_payload)

Create three implementations of a simple list data structure:

  • A singly linked list
  • A doubly linked list
  • A list with no form of linking

#Each node has:

Metadata: e.g. link to next node (Metadata can contain anything that you need to make the list work)

Data: a string payload (Data contains only the string payload)

@craigjbass
craigjbass / LukeAndCraig.kt
Last active August 26, 2016 10:42
Code Kata: WordWrap
package uk.co.devls.coffeebook.test.unit
import org.amshove.kluent.`should equal`
import kotlin.test.*
import org.jetbrains.spek.api.Spek
class CodeDojoTest : Spek({
describe("Wrapper") {
it("should return empty string when provided empty string") {
Wrapper().wrap("", 0).`should equal`("")
@craigjbass
craigjbass / Email_Service_Premailer.php
Last active January 2, 2016 00:18
Email_Service_Premailer
<?php
class Email_Service_Premailer {
/**
* Process using premailer
*
* requires:
* yum -y install ruby rubygems ruby-devel
* gem install premailer
* gem install hpricot
@craigjbass
craigjbass / ProjectEuler14.hs
Last active December 30, 2015 10:29
Longest Collatz sequence (Problem 14) http://projecteuler.net/problem=14
module Main (
main
) where
import Data.List (maximumBy)
import Data.Function (on)
collatz 1 = [1]
collatz i = i : (if odd i then collatz $ 3*i + 1 else collatz $ i `div` 2)
main = putStrLn $ show $ maximumBy (compare `on` length) $ map collatz [1..1000000]
@craigjbass
craigjbass / ImpureDatabaseWrapper.php
Last active December 26, 2015 17:39
"Monadic" Models for improved unit-testability. (Pure - Impure separation of control)
<?php
class ImpureDatabaseWrapper {
private $sql;
private $pureBinding;
/**
* @var \Zend_Db_Select $sql
* @var \Closure $pureBinding (Closure that contains "pure" code - i.e. it doesn't cause any side-effects!)
*/
@craigjbass
craigjbass / constraint_permutations.hs
Last active December 26, 2015 17:09
Give all possible permutations given a list of lists such that each list defines the set of characters for that particular index of the key. Whereby the top level list is the same length as the overall key.
-- The following holds true permuteAll [ [ 'a','b' ], [ 'c','d' ] ] => [ ['a','c'],['a','d'], ['b','c'], ['b','d'] ]
permuteAll :: [[a]] -> [[a]]
permuteAll [] = []
permuteAll (x:[]) = splitEvery 1 x
permuteAll (x:xs) = foldr (++) [] $ map f x
where
f :: a -> [[a]]
f x' = map ( \y -> (x':y) ) (permuteAll xs)