Skip to content

Instantly share code, notes, and snippets.

View jbranchaud's full-sized avatar

Josh Branchaud jbranchaud

View GitHub Profile
@jbranchaud
jbranchaud / listless.exs
Last active February 5, 2016 21:36
playing with arrays in elixir
defmodule Listless do
def first([]), do: nil
def first([head|_]) do
head
end
def last([]), do: first []
def last([head|[]]), do: head
def last(list) do
[_|tail] = list
NUMBER_OF_BLOCKS = 20
GRID_SIZE = NUMBER_OF_BLOCKS + 1
previous_row = Array.new(GRID_SIZE, 1)
(GRID_SIZE-1).times do |_|
previous_row =
previous_row.each_with_object([]) do |item,current_row|
current_row << item + (current_row.last || 0)
end
!! EASY
! Using only * and +, how would you calculate 3^2 + 4^2 with Factor?
3 3 * 4 4 * +
clear
! Enter USE: math.functions in the Listener. Now, with sq and sqrt,
! calculate the square root of 3^2 + 4^2.
USE: math.functions
3 sq 4 sq + sqrt

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.

@jbranchaud
jbranchaud / rails_slice.rb
Created August 13, 2015 21:45
slice and slice! in Rails
> {a: 1, b: 2, c: 3}.slice(:a)
=> {:a=>1}
> {a: 1, b: 2, c: 3}.slice!(:a)
=> {:b=>2, :c=>3}
# with postgres DDL
execute SQL<<-
create table users(
...
created_at timestamptz not null,
updated_at timestamptz not null
);
SQL
# with Rails DSL
puts '====='
puts 'defining the Parent class with #poop'
class Parent
def poop
puts "💩 💩"
end
end
puts 'defining the Child < Parent class with #poop'
@jbranchaud
jbranchaud / gist:bccead47f0e75e145db8
Last active January 25, 2024 21:28
Working With Buffers

Working With Buffers

Me

  • I'm Josh Branchaud
  • I work at Hashrocket
  • Twitter: @jbrancha
  • Github: @jbranchaud

Why talk about buffers?