Skip to content

Instantly share code, notes, and snippets.

@jwworth
jwworth / round-robin.lua
Last active January 10, 2016 01:36
Round Robin - Lua
--[[
Round Robin
Given 3 Teams (A, B, C), we want to organize a tournament schedule such that every team plays every other team exactly once. Here is a valid schedule for these 3 teams:
A - B
B - C
A - C
How about if we have N teams? Devise a general purpose algorithm that generates tournament schedules for N teams.
--]]
@jwworth
jwworth / from_one_end_to_the_other.elm
Last active January 29, 2016 23:03
From One End to the Other (Elm)
import Graphics.Element exposing (show)
import Array exposing (..)
import String exposing (..)
magicNumber : Array Int
magicNumber =
let
isNumber number =
String.right 1 (toString(number)) == "6" &&
(String.append "6" (String.dropRight 1 (toString(number))))
@jwworth
jwworth / led.rb
Created February 11, 2016 03:04
LED (Ruby Solution)
# LED Clock: You are (voluntarily) in a room that is completely dark except for
# the light coming from an old LED digital alarm clock. This is one of those
# clocks with 4 seven segment displays using an HH:MM time format. The clock is
# configured to display time in a 24 hour format and the leading digit will be
# blank if not used. What is the period of time between when the room is at its
# darkest to when it is at its lightest?
def compute_brightness(units)
cells_per_number = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 ]
@jwworth
jwworth / from_one_end_to_the_other.rb
Last active March 6, 2016 23:49
From One End to the Other
# From One End to the Other: Find the smallest possible
# (positive) integer that ends in a six such that if that six is removed and
# placed in front of the remaining digits of the number, the resulting number
# will be four times as large as the original.
i = 6
loop do
digits = i.to_s.split('')
if digits.last == '6' && digits.rotate(-1).join.to_i == (i * 4)
puts i
@jwworth
jwworth / from_one_end_to_the_other.lua
Created January 25, 2016 23:47
From One End To The Other (Lua)
-- Week 4: From One End to the Other: Find the smallest possible (positive)
-- integer that ends in a six such that if that six is removed and placed in front
-- of the remaining digits of the number, the resulting number will be four times
-- as large as the original.
function swap_first_and_last(t, n)
table.insert(t, 1, n)
return tonumber(table.concat(t))
end
@jwworth
jwworth / vim_buffer.rb
Created January 11, 2016 18:22
Vim Buffer
# Week 2 - Vim Buffer: I open up a new Vim buffer and type all the
# numbers 1 to 10,000, separated by spaces. Then, my cat walks on the keyboard
# and somehow activates a substitution command that replaces all the '0’ digits
# (zeros) with spaces. If I now sum up all the numbers in the buffer, as
# delineated by spaces, what is the total?
def vim_buffer(start = 1, limit)
(start..limit).flat_map { |num| num.to_s.gsub('0', ' ').split }.map(&:to_i).reduce(:+)
end
@jwworth
jwworth / logrotate.conf
Created March 10, 2017 16:56
Sample Logrotater for a Linux Server running Ruby on Rails
# Logrotater:
# - Daily
# - Timestamped
# - Doesn't error on missing log files
# - Keeps seven copies
# - Truncates log files (allows Rails to start writing to the new log file without
# a restart)
/var/app/current/log/production.log {
daily
@jwworth
jwworth / phoenix_1_3.projections.json
Last active August 10, 2017 04:54
Phoenix 1.3 Vim Projections
{
"lib/tilex_web/models": {
"type": "model"
},
"lib/tilex_web/models/*.ex": {
"type": "model",
"alternate": "test/models/{}_test.exs",
"template": [
"defmodule {project|camelcase|capitalize|basename}.{camelcase|capitalize|dot} do",
" use {project|camelcase|capitalize|basename}Web, :model",
@jwworth
jwworth / page_objects.projections.json
Last active August 10, 2017 05:02
RSpec Page Objects Projections
{
"spec/support/pages/*_page.rb": {
"command": "page",
"template": [
"module Pages",
" class {camelcase|capitalize}Page < Page",
" end",
"end",
]
},
@jwworth
jwworth / react_on_rails.projections.json
Last active August 10, 2017 05:06
'React On Rails' Vim Projections
{
"client/app/actions/*.js": {
"command": "actions"
},
"client/app/containers/*.js": {
"command": "container"
},
"client/app/components/*.jsx": {
"command": "component"
},