Skip to content

Instantly share code, notes, and snippets.

@elreimundo
elreimundo / eightball.js
Created August 11, 2014 17:29
EightBall - intro to JS from an object-oriented perspective
// we're starting this function with an IIFE http://benalman.com/news/2010/11/immediately-invoked-function-expression/
// it's important to start this with a semicolon because otherwise any previously loaded JavaScript might try and invoke
// its last line using the anonymous function that we define here as its argument, and then invoke the resulting return value
// on line 63. That would be silly, yo. So we explicitly end the previous thought with a semicolon.
;(function () {
// we define an array of all of the possible predictions that our eight ball can make.
// unlike in Ruby, the use of all caps is purely conventional; JavaScript has no opinions about
// what capitalized variables mean.
var PREDICTIONS = [
// we're starting this function with an IIFE http://benalman.com/news/2010/11/immediately-invoked-function-expression/
// it's important to start this with a semicolon because otherwise any previously loaded JavaScript might try and invoke
// its last line using the anonymous function that we define here as its argument, and then invoke the resulting return value
// on line 63. That would be silly, yo. So we explicitly end the previous thought with a semicolon.
;(function () {
// CLOSURES
function captureWhatNumberIsRightNow (number) {
return function () {
@elreimundo
elreimundo / picker.css
Created March 2, 2014 21:01
A website to help demonstrate object-oriented design, created as part of a workshop I led for students at Dev Bootcamp
body {
background-image: url('http://pcmreviews.com/news/wp-content/uploads/2012/01/Red-carpet.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
color: white;
font-family: 'Parisienne', cursive;
}
.header{
@elreimundo
elreimundo / cipher.rb
Last active August 29, 2015 13:56
A solution to a Caesar cipher challenge in which only letters are offset and in which spaces are a handful of special characters; the original challenge featured only lowercase letters and an offset of four, but this is intended to scale easily.
def north_korean_cipher message, offset = 4
unexaggerate message.chars.map{|char| decode char, offset}.join('')
end
def unexaggerate message
message.gsub(/\d+/) { |num| num.to_i / 100 }
end
def decode char, offset
case
@elreimundo
elreimundo / hundred_pairs.rb
Last active January 1, 2016 06:39
A program to determine all pairs of integers in a given array of integers that add to 100. The algorithm is destructive on the original array. This particular algorithm runs in N lg N time and constant space if the sorting algorithm is efficient; after the sort, only one additional pass through the array is required.
def hundred_pairs(array, target = 100)
array.sort!
results = []
min, max = array.shift, array.pop
while min && max
while min + max > target
max = array.pop
end
while min && max && min + max < target
min = array.shift
TIGERS = {1 => 'Morgan, Kiera, John', 2 => 'Dave, Bruno, Max',
3 => 'David, Nick, Lionel', 4 => 'Elmer, Juke, Robert',
5 => 'Tom, Tyler, Gary', 6 => 'Sunny, Daniel, Nathan',
7 => 'Doug, Rao, Nishant'}
class Console
def initialize
@gophers = {}
puts "Welcome to GroupMatcher(tm)!"
puts "When you've finished all groups, type 'Match!'"
@elreimundo
elreimundo / roman_to_integer.rb
Last active December 20, 2015 01:49
Here's code that will convert any (properly ordered) roman numeral to a number. It uses a hash, which is something I'm pretty excited about since I don't feel like I totally understand hashes.
def roman_to_integer string
string.upcase!
raise ArgumentError if /[MDCLXVI]*/.match(string).to_s != string
letter_values = {'M' => 1000,
'D' => 500,
'C' => 100,
'L' => 50,
'X' => 10,
'V' => 5,
'I' => 1}
@elreimundo
elreimundo / trianglenumdivisors.rb
Created July 20, 2013 21:22
I wrote this to solve another Project Euler problem (the number of divisors for a given triangular number). To find the number of divisors that a number has, the quickest way is to take the prime factorization of the number. Next, add one to each of the exponents of the prime factors, and find the product of these new exponents. There's your num…
whichtrinum = 1
while true
trinum = (whichtrinum * (whichtrinum + 1)) / 2
numtofactor = trinum
factors = []
factor = 2
while numtofactor != 1
if numtofactor % factor == 0
factors << factor
numtofactor = numtofactor / factor
@elreimundo
elreimundo / fibonaccicheck.rb
Last active December 20, 2015 01:08
Determines if a number is a Fibonacci number by a) determining element in the Fibonacci sequence it would be, based on its size, and b) comparing it to that actual element in the Fibonacci sequence. Couldn't have done this without a recursive formula courtesy of the late Edsgar Dijkstra, http://www.cs.utexas.edu/users/EWD/ewd06xx/EWD654.PDF foun…
def is_fibonacci?(num)
phi = (1 + 5.0**0.5)/2
which_fib = ((Math.log(num)+Math.log(5.0**0.5)) / Math.log(phi)).round
num == fibonacci(which_fib)
end
def fibonacci(n)
if n==0
0
@elreimundo
elreimundo / largestproduct.rb
Created July 20, 2013 20:34
This is the main method for a ProjectEuler problem that provides a grid of numbers (copied and pasted as a string) and wants to find the maximum product of any four consecutive elements horizontally, vertically, or diagonally. I probably could have written each of the if/then statements in a single line, but executing two lines of code actually …
def largestproduct(nums_as_a_string)
currentmax = 0
arry = nums_as_a_string.split("\n")
arry.map! { |row_as_a_string| row_as_a_string.split.map! { |digit| digit.to_i } }
0.upto( (arry.length - 1).to_i ) do |row|
0.upto( (arry[row].length - 1).to_i ) do |col|
if col <= (arry[row].length - 4)
rowprod = arry[row][col]*arry[row][col+1]*arry[row][col+2]*arry[row][col+3]
currentmax = [currentmax,rowprod].max
end