Skip to content

Instantly share code, notes, and snippets.

View elliotchance's full-sized avatar
🤓
Building awesome stuff with V

Elliot Chance elliotchance

🤓
Building awesome stuff with V
View GitHub Profile
@elliotchance
elliotchance / README.md
Last active December 31, 2021 05:17
Kata: Human readable duration format

Your task in order to complete this Kata is to write a function which formats a duration, given as a number of seconds, in a human-friendly way.

The function must accept a non-negative integer. If it is zero, it just returns "now". Otherwise, the duration is expressed as a combination of years, days, hours, minutes and seconds.

It is much easier to understand with an example:

  format_duration(62)    # returns "1 minute and 2 seconds"
  format_duration(3662)  # returns "1 hour, 1 minute and 2 seconds"
@elliotchance
elliotchance / elevator.py
Created September 10, 2016 08:36
The Elevator Game
import random
import time
class Person:
WAITING = 0
IN_ELEVATOR = 1
DONE = 2
def __init__(self, from_floor, to_floor):
self.from_floor = from_floor
from math import sqrt
import pprint
import random
def read_file(file_path):
ratings = {}
with open(file_path) as f:
lines = f.readlines()
@elliotchance
elliotchance / script.sh
Last active May 7, 2016 02:23
Post Build
PLIST="${PROJECT_DIR}/${INFOPLIST_FILE}"
PLB=/usr/libexec/PlistBuddy
LAST_NUMBER=$($PLB -c "Print CFBundleVersion" "$PLIST")
NEW_VERSION=$(($LAST_NUMBER + 1))
$PLB -c "Set :CFBundleVersion $NEW_VERSION" "$PLIST"
@elliotchance
elliotchance / bowling.py
Created May 3, 2016 11:39
Kata: The Bowling Game
import pytest
class Game:
def __init__(self):
self.frames = [[]]
self.allow_bonus_roll = False
@property
def last_frame(self):
return self.frames[-1]
@elliotchance
elliotchance / mapreduce.py
Created February 27, 2016 23:00
Simple MapReduce example
import re
def mapper(emit, text):
words = re.split('[^\w]', text.lower())
for word in words:
if word:
emit(word, 1)
def reducer(key, values):
@elliotchance
elliotchance / mymod.js
Last active February 17, 2016 02:34
Pythonic style modules in JavaScript
// 1. Modules are always lowercase, one word.
// 2. They can be nested in any level of directories, but the name of the module
// always reflects the file name (not including the path).
// Private
let counter = 0;
function addition(a, b) {
return a + b;
}
@elliotchance
elliotchance / roman.py
Created February 13, 2016 05:25
Convert roman numerals to a number
import unittest
import re
class RomanToNumberConverter:
# Validate that the input may even be processed.
def validate(self, roman):
if type(roman) is not str:
raise ValueError("You must provide a string.")
if roman == '':
raise ValueError("An empty string was provided.")
@elliotchance
elliotchance / example.go
Last active October 22, 2024 12:46
Capturing grouping for regex function replace in Go
func main() {
str := "abc foo:bar def baz:qux ghi"
re := regexp.MustCompile("([a-z]+):([a-z]+)")
result := ReplaceAllStringSubmatchFunc(re, str, func(groups []string) string {
return groups[1] + "." + groups[2]
})
fmt.Printf("'%s'\n", result)
}
@elliotchance
elliotchance / brainfuck.py
Created January 19, 2016 11:52
Write Your Own Brainfuck Compiler
import sys
import ply.yacc as yacc
import ply.lex as lex
tokens = (
'INCREMENT',
'DECREMENT',
'SHIFT_LEFT',
'SHIFT_RIGHT',
'OUTPUT',