Skip to content

Instantly share code, notes, and snippets.

View jbranchaud's full-sized avatar

Josh Branchaud jbranchaud

View GitHub Profile

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.

!! 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
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
@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
@jbranchaud
jbranchaud / vim-rails-migration-edit.rb
Created April 2, 2016 19:22
Error in timezone offset calculation
require 'date'
require 'minitest/autorun'
class TestMeme < MiniTest::Unit::TestCase
def time_to_timestamp(now:)
hours_in_seconds = now.strftime('%H').to_i * 3600
minutes_in_seconds = now.strftime('%M').to_i * 60
seconds = now.strftime('%S').to_i
# Sorting Rows By Index
Welcome back to pgcasts, my name is Josh Branchaud. In this episode I am going to talk about a handy shortcut we can use when ordering rows in a select statement.
Let's say we have a users table:
```sql
create table users (
id serial primary key,
first varchar not null,
@jbranchaud
jbranchaud / elixir_string_graphemes.ex
Created September 14, 2016 19:02
Elixir String Graphemes, Codepoints, Bytes
> string1 = "\u0065\u0301"
"é"
> string2 = "é"
"é"
> string1 == string2
false
> String.codepoints(string1)
["e", "́"]
> String.codepoints(string2)
["é"]
@jbranchaud
jbranchaud / pipe_inspect.ex
Last active March 11, 2017 21:59
easily inspecting a thing that is being piped through functions in Elixir
defmodule PipeInspect do
#
# Usage:
#
# Instead of temporarily breaking up a series of pipes to inspect the values:
# ```
# partial_result =
# 1..100_000
# |> Enum.map(&(&1 * 3))
#
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
var picArray [][]uint8
picArray = make([][]uint8, 0, dy)
for i := 0; i < dy; i++ {
var currX []uint8
currX = make([]uint8, 0, dx)