Skip to content

Instantly share code, notes, and snippets.

@kevbuchanan
kevbuchanan / playlist_creator.rb
Last active December 17, 2015 03:38
From Pine's Learn to Program. This program creates a music playlist in the specified directory.
directory = '/Users/kevinbuchanan/Music'
Dir.chdir directory
playlist = {}
while true
puts 'Enter a song name to add to the playlist: (Press enter when done)'
song = gets.chomp
@kevbuchanan
kevbuchanan / lcd_numbers.rb
Last active December 17, 2015 23:49
From Best of Ruby Quiz by James Edward Gray II.
# This program dispalys LCD-style numbers in terminal.
# Digits to be displayed are passed as an argument from the command line.
# Size can be controled with the command-line option -s followed by a positive integer.
# example: $ ruby lcd_numbers.rb -s 5 1234
class LCDNumber
def initialize (digits, size = 2, h_char = '-', v_char = '|' )
@digits = digits.split(//)
@size = size
@h_char = h_char
@kevbuchanan
kevbuchanan / secret_santa.rb
Last active December 17, 2015 23:49
From Best of Ruby Quiz by James Edward Gray II.
# Randomly assigns secret santas. Will not match up two people with the same last name.
class SecretSanta
attr_accessor :names
def initialize(names_array)
@names = names_array.map { |name| name.split(' ') }
raise "Odd number of names!" if @names.size.odd?
@assignments = {}
end
class Asteroid
attr_accessor :x, :y, :z, :connections
LIMIT = 150
def initialize(x, y, z, vx, vy, vz)
@x, @y, @z = x, y, z
@vx, @vy, @vz = vx, vy, vz
end
class Vehicle
attr_reader :wheels, :color, :gas_mileage
attr_accessor :status, :speed
def initialize(args)
@wheels = args[:wheels]
@color = args[:color]
@status = :stopped
@speed = 0
@gas_mileage = [true, false].sample
end
@kevbuchanan
kevbuchanan / binary_tree.rb
Last active December 19, 2015 20:58
Binary Search Tree
class Node
attr_accessor :value, :left, :right
def initialize(value, left = nil, right = nil)
@value, @left, @right = value, left, right
end
def delete(node)
if @left.value == node.value
@left = nil
@kevbuchanan
kevbuchanan / zoo.js
Last active December 20, 2015 05:39 — forked from dbc-challenges/zoo.js
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
var Zoo = {
init : function(animals){
this.animals = animals
},
bipeds : function(){
return this.animals.filter(function(element){
return (element.legs == 2);
@kevbuchanan
kevbuchanan / numbers_in_words.rb
Created August 27, 2013 19:29
Number in Words
VALUES = { 1000000000000 => 'trillion ', 1000000000 => 'billion ', 1000000 => 'million ', 1000 => 'thousand ', 1 => '' }
TENS = ['', '', 'twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ']
ONES = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ']
TEENS = ['ten ', 'eleven ', 'twelve ', 'thirteen ', 'fourteen ', 'fifteen ', 'sixteen ', 'seventeen ', 'eighteen ', 'nineteen ']
def to_word(number)
power = 1000**(Math.log10(number).floor / 3)
to_write = number / power
number_as_word = get_hundreds(to_write) + get_tens(to_write) + get_ones(to_write) + VALUES[power]
number_as_word << to_word(number % power) unless number % power == 0
class Frame
attr_reader :rolls
def initialize(rolls)
@rolls = rolls.first == 10 ? [rolls.first] : rolls
end
def roll_score
@rolls.inject(:+)
end
function Frame(rolls) {
if (rolls[0] == 10) {
this.rolls = [rolls[0]]
}
else {
this.rolls = rolls
}
}
Frame.prototype.rollScore = function() {