Skip to content

Instantly share code, notes, and snippets.

@jbrechtel
jbrechtel / game_of_life.markdown
Last active June 19, 2017 01:35
Conway's Game of Life

Conway's Game of Life

Conway's Game of Life is a set of rules which define a cellular automata. Read more about it in Wikipedia. Your task is to write a Conway's Game of Life (GOL) simulation.

GOL consists of a two-dimensional grid of cells. Each cell is either alive or dead. Your simulation should calculate the grid over a user-specified number of generations. To calculate the next generation you apply the following rules to each cell in the grid and the resulting grid is the next generation.

Rules

  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with two or three live neighbours lives on to the next generation.
@jbrechtel
jbrechtel / foo.clj
Last active December 12, 2015 02:49
clojure example
(defn add-prefix [prefix string]
"Prepends prefix to string separated by a space"
(str prefix " " string))
(def formalize-male
"Returns a formal version of a male name"
(partial add-prefix "Mr"))
(defn parenthesize [string]
"Wraps names in parentheses"
@jbrechtel
jbrechtel / deck_of_cards_2.markdown
Last active June 19, 2017 01:34
Dealing cards continued

Dealing cards to players

Using the classes built in the previous dealing cards problem, we're going to deal cards to players now.

Your program will deal cards to a user-specified number of players.

The program should ask for user input and respond to the following 3 commands

  • add_player
  • deal_cards
@jbrechtel
jbrechtel / deck_of_cards_1.markdown
Created January 31, 2013 14:57
Deck of cards problem part 1

Dealing cards

A deck comprises 52 cards of 4 suits (spades, hearts, clubs, and diamonds). Each suit has 13 cards of values from 2-10, Jack, Queen, King, and Ace.

Write a class to represent a card. The class should have a suit and value property indicating its suit and value.

Write a class that has a draw method.

  • The draw method should accept an integer as an argument.
  • The draw method should return an array of card objects.
@jbrechtel
jbrechtel / event_ticketing.markdown
Last active December 11, 2015 23:48
Event ticketing problem

You're building a system to process orders for seats to an event

You have two input files

The first is sections.txt. It contains a list of sections of seats. Each section has a name, a number of available seats and a cost per seat. It looks like this

Upper 30 50
Balcony 20 100
wget -O ~/deleting_code.mp3 https://dl.dropbox.com/u/2233/deleting_code.mp3
echo '#!/usr/bin/env ruby
deletions = `git diff HEAD --numstat`.lines.to_a.map do |l|
l.split(" ")
end.map do
|(add,del,_)| del.to_i - add.to_i
end.inject(:+).to_i
@jbrechtel
jbrechtel / outline.markdown
Last active December 11, 2015 09:38
Learning to program

Programming basics

Flow control

Variables

Methods

Classes

Scope

Algorithmic complexity (brief)

Ruby basics

Classes

Bundle 'Lokaltog/vim-powerline'
Bundle 'scrooloose/nerdtree'
Bundle 'wincent/Command-T'
Bundle 'benmills/vimux'
Bundle 'jbrechtel/vimux-ruby-test'
Bundle 'tpope/vim-endwise'
Bundle 'edsono/vim-matchit'
Bundle 'vim-ruby/vim-ruby'
Bundle 'mileszs/ack.vim'
Bundle 'desert-warm-256'
(defn cell-state [room x-pos y-pos]
(cond
(> 0 x-pos) :wall
(> 0 y-pos) :wall
(<= (count room) x-pos) :wall
(<= (count room) y-pos) :wall
:else ((room x-pos) y-pos)))
(defn agent-state [room, x-pos, y-pos]
(let [current (cell-state room x-pos y-pos)
(def possible-states [:clean :dirty :wall])
(def state-permutations
(for [north possible-states,
south possible-states,
east possible-states,
west possible-states,
curr possible-states]
{:north north :south south :east east :west west :current curr}))